1
Showing
1 changed file
with
289 additions
and
0 deletions
src/components/LbTable/index.vue
0 → 100644
| 1 | /* | ||
| 2 | * FileName: lb-table.vue | ||
| 3 | * Remark: element table | ||
| 4 | * Project: lb-element-table | ||
| 5 | * Author: 任超 | ||
| 6 | * File Created: Tuesday, 19th March 2019 9:55:27 am | ||
| 7 | * Last Modified: Tuesday, 19th March 2019 9:55:34 am | ||
| 8 | * Modified By: 任超 | ||
| 9 | */ | ||
| 10 | |||
| 11 | <template> | ||
| 12 | <div :class="['lb-table', customClass]"> | ||
| 13 | <el-table v-if="!heightNumSetting" class="table-fixed" :row-style="{ height: '50px' }" ref="elTable" :border='border' | ||
| 14 | :row-class-name="tableRowClassName" :show-header='showHeader' @row-click="singleElection" v-bind="$attrs" | ||
| 15 | :height="tableHeight" v-on="$listeners" :data="data" style="width: 100%" | ||
| 16 | :span-method="this.merge ? this.mergeMethod : this.spanMethod"> | ||
| 17 | <el-table-column width="45" align="center" v-if="isRadio"> | ||
| 18 | <template slot-scope="scope"> | ||
| 19 | <el-radio v-model="selected" :label="scope.$index"></el-radio> | ||
| 20 | </template> | ||
| 21 | </el-table-column> | ||
| 22 | |||
| 23 | <lb-column v-bind="$attrs" v-for="(item, index) in column" :key="index" :column="item"> | ||
| 24 | </lb-column> | ||
| 25 | </el-table> | ||
| 26 | |||
| 27 | <el-table v-else ref="elTable" id="heightNumSetting" class="table-fixed" :row-style="{ height: '50px' }" | ||
| 28 | :border='border' :row-class-name="tableRowClassName" :show-header='showHeader' v-bind="$attrs" | ||
| 29 | :max-height="maxHeight" :height="tableHeight" v-on="$listeners" :data="data" style="width: 100%" | ||
| 30 | :span-method="this.merge ? this.mergeMethod : this.spanMethod"> | ||
| 31 | |||
| 32 | <el-table-column width="45" align="center" v-if="isRadio"> | ||
| 33 | <template slot-scope="scope"> | ||
| 34 | <el-radio v-model="selected" :label="scope.$index"></el-radio> | ||
| 35 | </template> | ||
| 36 | </el-table-column> | ||
| 37 | <lb-column v-bind="$attrs" v-for="(item, index) in column" :key="index" :column="item"> | ||
| 38 | </lb-column> | ||
| 39 | </el-table> | ||
| 40 | |||
| 41 | <br> | ||
| 42 | <el-pagination class="lb-table-pagination" v-if="pagination" v-bind="$attrs" v-on="$listeners" background | ||
| 43 | layout="total, prev, pager, next" @current-change="paginationCurrentChange" | ||
| 44 | :style="{ 'margin-top': paginationTop, 'text-align': paginationAlign }"> | ||
| 45 | </el-pagination> | ||
| 46 | </div> | ||
| 47 | </template> | ||
| 48 | |||
| 49 | <script> | ||
| 50 | import LbColumn from './LbColumn' | ||
| 51 | export default { | ||
| 52 | props: { | ||
| 53 | column: Array, | ||
| 54 | data: Array, | ||
| 55 | spanMethod: Function, | ||
| 56 | pagination: { | ||
| 57 | type: Boolean, | ||
| 58 | default: true, | ||
| 59 | }, | ||
| 60 | isRadio: { | ||
| 61 | type: Boolean, | ||
| 62 | default: false, | ||
| 63 | }, | ||
| 64 | border: { | ||
| 65 | type: Boolean, | ||
| 66 | default: false, | ||
| 67 | }, | ||
| 68 | showHeader: { | ||
| 69 | type: Boolean, | ||
| 70 | default: true, | ||
| 71 | }, | ||
| 72 | paginationTop: { | ||
| 73 | type: String, | ||
| 74 | default: '0', | ||
| 75 | }, | ||
| 76 | heightNum: { | ||
| 77 | type: Number, | ||
| 78 | default: 355, | ||
| 79 | }, | ||
| 80 | maxHeight: { | ||
| 81 | type: Number, | ||
| 82 | default: 500 | ||
| 83 | }, | ||
| 84 | minHeight: { | ||
| 85 | type: Number, | ||
| 86 | default: undefined | ||
| 87 | }, | ||
| 88 | heightNumSetting: { | ||
| 89 | type: Boolean, | ||
| 90 | default: false, | ||
| 91 | }, | ||
| 92 | customClass: { | ||
| 93 | type: String, | ||
| 94 | default: '', | ||
| 95 | }, | ||
| 96 | paginationAlign: { | ||
| 97 | type: String, | ||
| 98 | default: 'left', | ||
| 99 | }, | ||
| 100 | calcHeight: { | ||
| 101 | type: Number, | ||
| 102 | default: 170 | ||
| 103 | }, | ||
| 104 | merge: Array, | ||
| 105 | }, | ||
| 106 | components: { | ||
| 107 | LbColumn, | ||
| 108 | }, | ||
| 109 | data () { | ||
| 110 | return { | ||
| 111 | tableHeight: 'auto', | ||
| 112 | mergeLine: {}, | ||
| 113 | mergeIndex: {}, | ||
| 114 | selected: '' | ||
| 115 | } | ||
| 116 | }, | ||
| 117 | created () { | ||
| 118 | this.getMergeArr(this.data, this.merge) | ||
| 119 | this.getHeight() | ||
| 120 | }, | ||
| 121 | computed: { | ||
| 122 | dataLength () { | ||
| 123 | return [] || this.data.length | ||
| 124 | }, | ||
| 125 | }, | ||
| 126 | methods: { | ||
| 127 | // 单选 | ||
| 128 | singleElection (row) { | ||
| 129 | this.selected = this.data.indexOf(row); | ||
| 130 | }, | ||
| 131 | |||
| 132 | tableRowClassName ({ row, rowIndex }) { | ||
| 133 | if (rowIndex % 2 === 1) { | ||
| 134 | return 'interlaced'; | ||
| 135 | } | ||
| 136 | }, | ||
| 137 | getHeight () { | ||
| 138 | if (!this.heightNumSetting) { | ||
| 139 | let _this = this | ||
| 140 | if (this.heightNum) { | ||
| 141 | _this.$nextTick(() => { | ||
| 142 | if (document.querySelector(".tags-view-container")) { | ||
| 143 | window.addEventListener('resize', () => { | ||
| 144 | if (_this.calcHeight == 230) { | ||
| 145 | _this.tableHeight = _this.calcHeightx(192) | ||
| 146 | } else { | ||
| 147 | _this.tableHeight = _this.calcHeightx(_this.calcHeight) | ||
| 148 | } | ||
| 149 | }); | ||
| 150 | if (_this.calcHeight == 230) { | ||
| 151 | _this.tableHeight = _this.calcHeightx(192) | ||
| 152 | } else { | ||
| 153 | _this.tableHeight = _this.calcHeightx(_this.calcHeight) | ||
| 154 | } | ||
| 155 | } else { | ||
| 156 | window.addEventListener('resize', () => { | ||
| 157 | _this.tableHeight = _this.calcHeightx(_this.calcHeight) | ||
| 158 | }); | ||
| 159 | _this.tableHeight = _this.calcHeightx(_this.calcHeight) | ||
| 160 | } | ||
| 161 | }) | ||
| 162 | } else { | ||
| 163 | _this.tableHeight = window.innerHeight - _this.heightNum | ||
| 164 | } | ||
| 165 | } else { | ||
| 166 | this.tableHeight = this.heightNum | ||
| 167 | this.$nextTick(() => { | ||
| 168 | this.minHeight && (document.getElementById('heightNumSetting').style.minHeight = this.minHeight + 'px') | ||
| 169 | }) | ||
| 170 | } | ||
| 171 | }, | ||
| 172 | calcHeightx (value, wappered = true) { | ||
| 173 | //项目自定义的公共header部分的高度,可忽略 | ||
| 174 | let header = document.querySelector(".from-clues-header").offsetHeight; | ||
| 175 | |||
| 176 | //value为动态计算table界面高度时,减去的其他空白部分,需自行在调试找到临界值,剩下的就是table表格的高度(包含header+body部分) | ||
| 177 | value = value == undefined ? 100 : value; | ||
| 178 | if (document.querySelector(".tags-view-container")) { | ||
| 179 | let tagsView = document.querySelector(".tags-view-container").offsetHeight; | ||
| 180 | var res = window.innerHeight - parseInt(header) - value - parseInt(tagsView); | ||
| 181 | } else { | ||
| 182 | var res = window.innerHeight - parseInt(header) - value; | ||
| 183 | } | ||
| 184 | if (wappered) { | ||
| 185 | //通过原生方法,获取dom节点的高度------获取element-ui table表格body的元素 | ||
| 186 | let wapper = window.document.getElementsByClassName('el-table__body-wrapper'); | ||
| 187 | //通过原生方法,获取dom节点的高度------获取element-ui table表格header的元素 | ||
| 188 | let header = window.document.getElementsByClassName('el-table__header-wrapper'); | ||
| 189 | //必须加延时,要不然赋不上去值 | ||
| 190 | setTimeout(() => { | ||
| 191 | //通过上边计算得到的table高度的value值,减去table表格的header高度,剩下的通过dom节点直接强行赋给table表格的body | ||
| 192 | wapper[0].style.height = (value - header[0].clientHeight) | ||
| 193 | }, 100) | ||
| 194 | } | ||
| 195 | return res; | ||
| 196 | }, | ||
| 197 | clearSelection () { | ||
| 198 | this.$refs.elTable.clearSelection() | ||
| 199 | }, | ||
| 200 | toggleRowSelection (row, selected) { | ||
| 201 | this.$refs.elTable.toggleRowSelection(row, selected) | ||
| 202 | }, | ||
| 203 | toggleAllSelection () { | ||
| 204 | this.$refs.elTable.toggleAllSelection() | ||
| 205 | }, | ||
| 206 | toggleRowExpansion (row, expanded) { | ||
| 207 | this.$refs.elTable.toggleRowExpansion(row, expanded) | ||
| 208 | }, | ||
| 209 | setCurrentRow (row) { | ||
| 210 | this.$refs.elTable.setCurrentRow(row) | ||
| 211 | }, | ||
| 212 | clearSort () { | ||
| 213 | this.$refs.elTable.clearSort() | ||
| 214 | }, | ||
| 215 | clearFilter (columnKey) { | ||
| 216 | this.$refs.elTable.clearFilter(columnKey) | ||
| 217 | }, | ||
| 218 | doLayout () { | ||
| 219 | this.$refs.elTable.doLayout() | ||
| 220 | }, | ||
| 221 | sort (prop, order) { | ||
| 222 | this.$refs.elTable.sort(prop, order) | ||
| 223 | }, | ||
| 224 | paginationCurrentChange (val) { | ||
| 225 | this.$emit('p-current-change', val) | ||
| 226 | }, | ||
| 227 | getMergeArr (tableData, merge) { | ||
| 228 | if (!merge) return | ||
| 229 | this.mergeLine = {} | ||
| 230 | this.mergeIndex = {} | ||
| 231 | merge.forEach((item, k) => { | ||
| 232 | tableData.forEach((data, i) => { | ||
| 233 | if (i === 0) { | ||
| 234 | this.mergeIndex[item] = this.mergeIndex[item] || [] | ||
| 235 | this.mergeIndex[item].push(1) | ||
| 236 | this.mergeLine[item] = 0 | ||
| 237 | } else { | ||
| 238 | if (data[item] === tableData[i - 1][item]) { | ||
| 239 | this.mergeIndex[item][this.mergeLine[item]] += 1 | ||
| 240 | this.mergeIndex[item].push(0) | ||
| 241 | } else { | ||
| 242 | this.mergeIndex[item].push(1) | ||
| 243 | this.mergeLine[item] = i | ||
| 244 | } | ||
| 245 | } | ||
| 246 | }) | ||
| 247 | }) | ||
| 248 | }, | ||
| 249 | mergeMethod ({ row, column, rowIndex, columnIndex }) { | ||
| 250 | const index = this.merge.indexOf(column.property) | ||
| 251 | if (index > -1) { | ||
| 252 | const _row = this.mergeIndex[this.merge[index]][rowIndex] | ||
| 253 | const _col = _row > 0 ? 1 : 0 | ||
| 254 | return { | ||
| 255 | rowspan: _row, | ||
| 256 | colspan: _col, | ||
| 257 | } | ||
| 258 | } | ||
| 259 | }, | ||
| 260 | }, | ||
| 261 | watch: { | ||
| 262 | merge () { | ||
| 263 | this.getMergeArr(this.data, this.merge) | ||
| 264 | }, | ||
| 265 | dataLength () { | ||
| 266 | this.getMergeArr(this.data, this.merge) | ||
| 267 | } | ||
| 268 | }, | ||
| 269 | } | ||
| 270 | </script> | ||
| 271 | <style rel="stylesheet/scss" scoped lang="scss"> | ||
| 272 | .lb-table { | ||
| 273 | margin-top: 1px; | ||
| 274 | |||
| 275 | .interlaced { | ||
| 276 | background: #fafcff; | ||
| 277 | border: 1px solid #ebf2fa; | ||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 281 | /deep/.el-table .cell { | ||
| 282 | padding-left: 3px; | ||
| 283 | padding-right: 3px; | ||
| 284 | } | ||
| 285 | |||
| 286 | /deep/.el-radio__label { | ||
| 287 | display: none; | ||
| 288 | } | ||
| 289 | </style> | 
- 
Please register or sign in to post a comment