Blame view

src/components/LbTable/lb-table.vue 11.3 KB
1  
renchao@pashanhoo.com committed
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * FileName: lb-table.vue
 * Remark: element table
 * Project: lb-element-table
 * Author: 任超
 * File Created: Tuesday, 19th March 2019 9:55:27 am
 * Last Modified: Tuesday, 19th March 2019 9:55:34 am
 * Modified By: 任超
 */

<template>
  <div :class="['lb-table', customClass]">
13
    <el-table v-if="!heightNumSetting" class="table-fixed" :row-style="{ height: rowStyleHeight+'px' }" ref="elTable" :border='border'
14
      :row-class-name="tableRowClassName" :show-header='showHeader' @row-click="singleElection" v-bind="$attrs"
1  
renchao@pashanhoo.com committed
15 16 17 18 19 20 21 22 23 24 25 26
      :height="tableHeight" v-on="$listeners" :data="data" style="width: 100%"
      :span-method="this.merge ? this.mergeMethod : this.spanMethod">
      <el-table-column width="45" align="center" v-if="isRadio">
        <template slot-scope="scope">
          <el-radio v-model="selected" :label="scope.$index"></el-radio>
        </template>
      </el-table-column>

      <lb-column v-bind="$attrs" v-for="(item, index) in column" :key="index" :column="item">
      </lb-column>
    </el-table>

27
    <el-table v-else ref="elTable" class="table-fixed heightNumSetting" :row-style="{ height: rowStyleHeight+'px'  }"
28
      :border='border' :row-class-name="tableRowClassName" :show-header='showHeader' @row-click="singleElection" v-bind="$attrs"
1  
renchao@pashanhoo.com committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42
      :max-height="maxHeight" :height="tableHeight" v-on="$listeners" :data="data" style="width: 100%"
      :span-method="this.merge ? this.mergeMethod : this.spanMethod">

      <el-table-column width="45" align="center" v-if="isRadio">
        <template slot-scope="scope">
          <el-radio v-model="selected" :label="scope.$index"></el-radio>
        </template>
      </el-table-column>
      <lb-column v-bind="$attrs" v-for="(item, index) in column" :key="index" :column="item">
      </lb-column>
    </el-table>

    <br>
    <el-pagination class="lb-table-pagination" v-if="pagination" v-bind="$attrs" v-on="$listeners" background
renchao@pashanhoo.com committed
43
      layout="total, sizes, prev, pager, next" @current-change="paginationCurrentChange" @size-change="handleSizeChange"
1  
renchao@pashanhoo.com committed
44 45 46 47 48 49
      :style="{ 'margin-top': paginationTop, 'text-align': paginationAlign }">
    </el-pagination>
  </div>
</template>

<script>
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  import LbColumn from './lb-column'
  export default {
    props: {
      column: Array,
      data: Array,
      spanMethod: Function,
      pagination: {
        type: Boolean,
        default: true,
      },
      isRadio: {
        type: Boolean,
        default: false,
      },
      border: {
        type: Boolean,
        default: true,
      },
68 69 70 71
      rowStyleHeight: {
        type: Number,
        default: 45,
      },
72 73 74 75 76 77 78 79 80 81
      showHeader: {
        type: Boolean,
        default: true,
      },
      paginationTop: {
        type: String,
        default: '0',
      },
      heightNum: {
        type: Number,
1  
renchao@pashanhoo.com committed
82
        default: 0,
83 84 85 86 87 88 89
      },
      maxHeight: {
        type: Number,
        default: 500
      },
      minHeight: {
        type: Number,
90
        default: 160
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
      },
      heightNumSetting: {
        type: Boolean,
        default: false,
      },
      customClass: {
        type: String,
        default: '',
      },
      paginationAlign: {
        type: String,
        default: 'left',
      },
      calcHeight: {
        type: Number,
        default: 170
      },
      merge: Array,
1  
renchao@pashanhoo.com committed
109
    },
110 111
    components: {
      LbColumn,
1  
renchao@pashanhoo.com committed
112
    },
yuanbo committed
113 114 115 116 117

    /**
     * @description: data
     * @author: renchao
     */
118 119 120 121 122 123 124
    data () {
      return {
        tableHeight: 'auto',
        mergeLine: {},
        mergeIndex: {},
        selected: ''
      }
1  
renchao@pashanhoo.com committed
125
    },
yuanbo committed
126 127 128 129
    /**
     * @description: created
     * @author: renchao
     */
130 131 132
    created () {
      this.getMergeArr(this.data, this.merge)
      this.getHeight()
1  
renchao@pashanhoo.com committed
133
    },
134 135 136 137
    computed: {
      dataLength () {
        return [] || this.data.length
      },
1  
renchao@pashanhoo.com committed
138
    },
139
    methods: {
yuanbo committed
140 141 142 143 144
      /**
       * @description: 单选
       * @param {*} row
       * @author: renchao
       */
145 146
      singleElection (row) {
        this.selected = this.data.indexOf(row);
147
        // this.$emit('row-click', row)
148
      },
1  
renchao@pashanhoo.com committed
149

yuanbo committed
150 151 152 153
      /**
       * @description: tableRowClassName
       * @author: renchao
       */
154 155 156 157 158
      tableRowClassName ({ row, rowIndex }) {
        if (rowIndex % 2 === 1) {
          return 'interlaced';
        }
      },
yuanbo committed
159 160 161 162
      /**
       * @description: getHeight
       * @author: renchao
       */
163 164 165
      getHeight () {
        if (!this.heightNumSetting) {
          let _this = this
1  
renchao@pashanhoo.com committed
166
          if (this.heightNum == 0) {
167 168 169 170 171 172 173 174 175
            _this.$nextTick(() => {
              if (document.querySelector(".tags-view-container")) {
                window.addEventListener('resize', () => {
                  if (_this.calcHeight == 170) {
                    _this.tableHeight = _this.calcHeightx(170)
                  } else {
                    _this.tableHeight = _this.calcHeightx(_this.calcHeight)
                  }
                });
176 177
                if (_this.calcHeight == 170) {
                  _this.tableHeight = _this.calcHeightx(170)
1  
renchao@pashanhoo.com committed
178 179 180 181
                } else {
                  _this.tableHeight = _this.calcHeightx(_this.calcHeight)
                }
              } else {
182 183 184
                window.addEventListener('resize', () => {
                  _this.tableHeight = _this.calcHeightx(_this.calcHeight)
                });
1  
renchao@pashanhoo.com committed
185 186
                _this.tableHeight = _this.calcHeightx(_this.calcHeight)
              }
187
              console.log(_this.tableHeight, '_this.tableHeight');
188 189 190 191
            })
          } else {
            _this.tableHeight = window.innerHeight - _this.heightNum
          }
1  
renchao@pashanhoo.com committed
192
        } else {
193
          this.$nextTick(() => {
194 195 196 197
            let arr = document.getElementsByClassName('heightNumSetting');
            [...arr].forEach(item => {
              this.minHeight && (item.style.minHeight = this.minHeight + 'px')
            })
198
          })
1  
renchao@pashanhoo.com committed
199
        }
200
      },
yuanbo committed
201 202 203 204 205 206
      /**
       * @description: calcHeightx
       * @param {*} value
       * @param {*} wappered
       * @author: renchao
       */
207 208 209
      calcHeightx (value, wappered = true) {
        //项目自定义的公共header部分的高度,可忽略
        let header = document.querySelector(".from-clues-header").offsetHeight;
1  
renchao@pashanhoo.com committed
210

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
        //value为动态计算table界面高度时,减去的其他空白部分,需自行在调试找到临界值,剩下的就是table表格的高度(包含header+body部分)
        value = value == undefined ? 100 : value;
        if (document.querySelector(".tags-view-container")) {
          let tagsView = document.querySelector(".tags-view-container").offsetHeight;
          var res = window.innerHeight - parseInt(header) - value - parseInt(tagsView);
        } else {
          var res = window.innerHeight - parseInt(header) - value;
        }
        if (wappered) {
          //通过原生方法,获取dom节点的高度------获取element-ui table表格body的元素
          let wapper = window.document.getElementsByClassName('el-table__body-wrapper');
          //通过原生方法,获取dom节点的高度------获取element-ui table表格header的元素
          let header = window.document.getElementsByClassName('el-table__header-wrapper');
          //必须加延时,要不然赋不上去值
          setTimeout(() => {
            //通过上边计算得到的table高度的value值,减去table表格的header高度,剩下的通过dom节点直接强行赋给table表格的body
            wapper[0].style.height = (value - header[0].clientHeight)
          }, 100)
        }
        return res;
      },
yuanbo committed
232 233 234 235
      /**
       * @description: clearSelection
       * @author: renchao
       */
236 237 238
      clearSelection () {
        this.$refs.elTable.clearSelection()
      },
yuanbo committed
239 240 241 242
      /**
       * @description: toggleRowSelection
       * @author: renchao
       */
243 244 245
      toggleRowSelection (row, selected) {
        this.$refs.elTable.toggleRowSelection(row, selected)
      },
yuanbo committed
246 247 248 249
      /**
       * @description: toggleAllSelection
       * @author: renchao
       */
250 251 252
      toggleAllSelection () {
        this.$refs.elTable.toggleAllSelection()
      },
yuanbo committed
253 254 255 256 257 258
      /**
       * @description: toggleRowExpansion
       * @param {*} row
       * @param {*} bsm
       * @author: renchao
       */
259 260 261
      toggleRowExpansion (row, expanded) {
        this.$refs.elTable.toggleRowExpansion(row, expanded)
      },
yuanbo committed
262 263 264 265 266
      /**
       * @description: setCurrentRow
       * @param {*} row
       * @author: renchao
       */
267 268 269
      setCurrentRow (row) {
        this.$refs.elTable.setCurrentRow(row)
      },
yuanbo committed
270 271 272 273
      /**
       * @description: setCurrentRow
       * @author: renchao
       */
274 275 276
      clearSort () {
        this.$refs.elTable.clearSort()
      },
yuanbo committed
277 278 279 280 281
      /**
       * @description: clearFilter
       * @param {*} columnKey
       * @author: renchao
       */
282 283 284
      clearFilter (columnKey) {
        this.$refs.elTable.clearFilter(columnKey)
      },
yuanbo committed
285 286 287 288
      /**
       * @description: doLayout
       * @author: renchao
       */
289 290 291
      doLayout () {
        this.$refs.elTable.doLayout()
      },
yuanbo committed
292 293 294 295 296 297
      /**
       * @description: sort
       * @param {*} prop
       * @param {*} order
       * @author: renchao
       */
298 299 300
      sort (prop, order) {
        this.$refs.elTable.sort(prop, order)
      },
yuanbo committed
301 302 303 304 305
      /**
       * @description: paginationCurrentChange
       * @param {*} val
       * @author: renchao
       */
306 307
      paginationCurrentChange (val) {
        this.$emit('p-current-change', val)
renchao@pashanhoo.com committed
308 309 310
      },
      handleSizeChange (val) {
        this.$emit('size-change', val)
311
      },
yuanbo committed
312 313 314 315 316 317
      /**
       * @description: getMergeArr
       * @param {*} tableData
       * @param {*} merge
       * @author: renchao
       */
318 319 320 321 322 323 324 325
      getMergeArr (tableData, merge) {
        if (!merge) return
        this.mergeLine = {}
        this.mergeIndex = {}
        merge.forEach((item, k) => {
          tableData.forEach((data, i) => {
            if (i === 0) {
              this.mergeIndex[item] = this.mergeIndex[item] || []
1  
renchao@pashanhoo.com committed
326
              this.mergeIndex[item].push(1)
327 328 329 330 331 332 333 334 335
              this.mergeLine[item] = 0
            } else {
              if (data[item] === tableData[i - 1][item]) {
                this.mergeIndex[item][this.mergeLine[item]] += 1
                this.mergeIndex[item].push(0)
              } else {
                this.mergeIndex[item].push(1)
                this.mergeLine[item] = i
              }
1  
renchao@pashanhoo.com committed
336
            }
337
          })
1  
renchao@pashanhoo.com committed
338
        })
339
      },
yuanbo committed
340 341 342 343
      /**
       * @description: mergeMethod
       * @author: renchao
       */
344 345 346 347 348 349 350 351 352
      mergeMethod ({ row, column, rowIndex, columnIndex }) {
        const index = this.merge.indexOf(column.property)
        if (index > -1) {
          const _row = this.mergeIndex[this.merge[index]][rowIndex]
          const _col = _row > 0 ? 1 : 0
          return {
            rowspan: _row,
            colspan: _col,
          }
1  
renchao@pashanhoo.com committed
353
        }
354
      },
1  
renchao@pashanhoo.com committed
355
    },
356
    watch: {
yuanbo committed
357 358 359 360
      /**
       * @description: merge
       * @author: renchao
       */
361 362 363
      merge () {
        this.getMergeArr(this.data, this.merge)
      },
yuanbo committed
364 365 366 367
      /**
       * @description: dataLength
       * @author: renchao
       */
368 369 370
      dataLength () {
        this.getMergeArr(this.data, this.merge)
      }
1  
renchao@pashanhoo.com committed
371
    },
372
  }
1  
renchao@pashanhoo.com committed
373 374
</script>
<style  rel="stylesheet/scss" scoped lang="scss">
375 376
  .lb-table {
    margin-top: 1px;
1  
renchao@pashanhoo.com committed
377

378 379 380 381
    .interlaced {
      background: #fafcff;
      border: 1px solid #ebf2fa;
    }
1  
renchao@pashanhoo.com committed
382 383
  }

384 385 386 387
  /deep/.el-table .cell {
    padding-left: 3px;
    padding-right: 3px;
  }
1  
renchao@pashanhoo.com committed
388

389 390 391
  /deep/.el-radio__label {
    display: none;
  }
392 393 394
  /deep/.el-table .cell {
    line-height: 16px;
  }
1  
renchao@pashanhoo.com committed
395
</style>