Blame view

src/components/DownLbTable/index.vue 8.48 KB
yuanbo committed
1
/*
yuanbo committed
2 3 4 5 6 7 8 9
* 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: 任超
*/
yuanbo committed
10 11 12 13

<template>
  <div :class="['lb-table', customClass]">
    <el-table v-if="!heightNumSetting" class="table-fixed" :row-style="{ height: '50px' }" ref="elTable" :border='border'
yuanbo committed
14 15 16
              :row-class-name="tableRowClassName" :show-header='showHeader' @row-click="singleElection" v-bind="$attrs"
              :height="tableHeight" v-on="$listeners" :data="data" style="width: 100%"
              :span-method="this.merge ? this.mergeMethod : this.spanMethod">
yuanbo committed
17 18 19 20 21 22 23 24 25 26 27 28 29
      <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>

      <el-table-column :label="downTitle" align="center">
        <lb-column v-bind="$attrs" v-for="(item, index) in column" :key="index" :column="item">
        </lb-column>
      </el-table-column>
    </el-table>
    <br>
    <el-pagination class="lb-table-pagination" v-if="pagination" v-bind="$attrs" v-on="$listeners" background
yuanbo committed
30 31
                   layout="total,  prev, pager, next" @current-change="paginationCurrentChange"
                   :style="{ 'margin-top': paginationTop, 'text-align': paginationAlign }">
yuanbo committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    </el-pagination>
  </div>
</template>

<script>
import LbColumn from './LbColumn'
export default {
  props: {
    column: Array,
    data: Array,
    spanMethod: Function,
    pagination: {
      type: Boolean,
      default: true,
    },
    downExcel: {
      type: Boolean,
      default: false,
    },
    downTitle: {
      type: String,
      default: '标题'
    },
    isRadio: {
      type: Boolean,
      default: false,
    },
    border: {
      type: Boolean,
      default: false,
    },
    showHeader: {
      type: Boolean,
      default: true,
    },
    paginationTop: {
      type: String,
      default: '0',
    },
    heightNum: {
      type: Number,
      default: 355,
    },
    maxHeight: {
      type: Number,
      default: 500
    },
    heightNumSetting: {
      type: Boolean,
      default: false,
    },
    customClass: {
      type: String,
      default: '',
    },
    paginationAlign: {
      type: String,
      default: 'left',
    },
    merge: Array,
  },
  components: {
    LbColumn,
  },
  data () {
    return {
      tableHeight: 'auto',
      mergeLine: {},
      mergeIndex: {},
      selected: ''
    }
  },
  created () {
    this.getMergeArr(this.data, this.merge)
    this.getHeight()
  },
  computed: {
    dataLength () {
      return [] || this.data.length
    },
  },
  methods: {
    // 单选
yuanbo committed
115 116 117 118 119
    /**
     * @description: 单选
     * @param {*} row
     * @author: renchao
     */
yuanbo committed
120 121 122 123
    singleElection (row) {
      this.selected = this.data.indexOf(row);
    },

yuanbo committed
124 125 126 127 128 129
    /**
     * @description: tableRowClassName
     * @param {*} row
     * @param {*} rowIndex
     * @author: renchao
     */
yuanbo committed
130 131 132 133 134
    tableRowClassName ({ row, rowIndex }) {
      if (rowIndex % 2 === 1) {
        return 'interlaced';
      }
    },
yuanbo committed
135 136 137 138
    /**
     * @description: getHeight
     * @author: renchao
     */
yuanbo committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    getHeight () {
      if (!this.heightNumSetting) {
        let _this = this
        if (this.heightNum) {
          _this.$nextTick(() => {

            window.addEventListener('resize', () => {
              _this.tableHeight = _this.calcHeightx(230)
            });
            _this.tableHeight = _this.calcHeightx(230)
          })
        } else {
          _this.tableHeight = window.innerHeight - _this.heightNum
        }
      }
    },
yuanbo committed
155 156 157 158 159 160
    /**
     * @description: calcHeightx
     * @param {*} value
     * @param {*} wappered
     * @author: renchao
     */
yuanbo committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    calcHeightx (value, wappered = true) {
      //项目自定义的公共header部分的高度,可忽略
      let header = document.querySelector(".from-clues-header").offsetHeight;
      //value为动态计算table界面高度时,减去的其他空白部分,需自行在调试找到临界值,剩下的就是table表格的高度(包含header+body部分)
      value = value == undefined ? 100 : value;
      let 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
180 181 182 183
    /**
     * @description: clearSelection
     * @author: renchao
     */
yuanbo committed
184 185 186
    clearSelection () {
      this.$refs.elTable.clearSelection()
    },
yuanbo committed
187 188 189 190 191 192
    /**
     * @description: toggleRowSelection
     * @param {*} row
     * @param {*} selected
     * @author: renchao
     */
yuanbo committed
193 194 195
    toggleRowSelection (row, selected) {
      this.$refs.elTable.toggleRowSelection(row, selected)
    },
yuanbo committed
196 197 198 199
    /**
     * @description: toggleAllSelection
     * @author: renchao
     */
yuanbo committed
200 201 202
    toggleAllSelection () {
      this.$refs.elTable.toggleAllSelection()
    },
yuanbo committed
203 204 205 206 207 208
    /**
     * @description: toggleRowExpansion
     * @param {*} row
     * @param {*} expanded
     * @author: renchao
     */
yuanbo committed
209 210 211
    toggleRowExpansion (row, expanded) {
      this.$refs.elTable.toggleRowExpansion(row, expanded)
    },
yuanbo committed
212 213 214 215 216
    /**
     * @description: setCurrentRow
     * @param {*} row
     * @author: renchao
     */
yuanbo committed
217 218 219
    setCurrentRow (row) {
      this.$refs.elTable.setCurrentRow(row)
    },
yuanbo committed
220 221 222 223
    /**
     * @description: clearSort
     * @author: renchao
     */
yuanbo committed
224 225 226
    clearSort () {
      this.$refs.elTable.clearSort()
    },
yuanbo committed
227 228 229 230 231
    /**
     * @description: clearFilter
     * @param {*} columnKey
     * @author: renchao
     */
yuanbo committed
232 233 234
    clearFilter (columnKey) {
      this.$refs.elTable.clearFilter(columnKey)
    },
yuanbo committed
235 236 237 238
    /**
     * @description: doLayout
     * @author: renchao
     */
yuanbo committed
239 240 241
    doLayout () {
      this.$refs.elTable.doLayout()
    },
yuanbo committed
242 243 244 245 246 247
    /**
     * @description: sort
     * @param {*} prop
     * @param {*} order
     * @author: renchao
     */
yuanbo committed
248 249 250
    sort (prop, order) {
      this.$refs.elTable.sort(prop, order)
    },
yuanbo committed
251 252 253 254 255
    /**
     * @description: paginationCurrentChange
     * @param {*} val
     * @author: renchao
     */
yuanbo committed
256 257 258
    paginationCurrentChange (val) {
      this.$emit('p-current-change', val)
    },
yuanbo committed
259 260 261 262 263 264
    /**
     * @description: getMergeArr
     * @param {*} tableData
     * @param {*} merge
     * @author: renchao
     */
yuanbo committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
    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] || []
            this.mergeIndex[item].push(1)
            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
            }
          }
        })
      })
    },
yuanbo committed
287 288 289 290 291 292 293 294
    /**
     * @description: mergeMethod
     * @param {*} row
     * @param {*} column
     * @param {*} rowIndex
     * @param {*} columnIndex
     * @author: renchao
     */
yuanbo committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
    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,
        }
      }
    },
  },
  watch: {
    merge () {
      this.getMergeArr(this.data, this.merge)
    },
    dataLength () {
      this.getMergeArr(this.data, this.merge)
    }
  },
}
</script>
<style  rel="stylesheet/scss" scoped lang="scss">
.lb-table {
  margin-top: 1px;

  .interlaced {
    background: #fafcff;
    border: 1px solid #ebf2fa;
  }
}

/deep/.el-table .cell {
  padding-left: 3px;
  padding-right: 3px;
}

/deep/.el-radio__label {
  display: none;
}
</style>
yuanbo committed
336