Blame view

src/components/lbTable/lb-table.vue 5.98 KB
任超 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]">
renchao@pashanhoo.com committed
13 14
    <el-table v-if="!heightNumSetting" class="table-fixed" :row-style="{ height: '50px' }" ref="elTable"
      :border='border' :row-class-name="tableRowClassName" :show-header='showHeader' @row-click="singleElection"
任超 committed
15
      :header-cell-style="{ background: 'rgb(236, 245, 255)' }" v-bind="$attrs" :height="tableHeight" v-on="$listeners"
任超 committed
16
      :data="data" style="width: 100%" :span-method="this.merge ? this.mergeMethod : this.spanMethod">
renchao@pashanhoo.com committed
17
      <el-table-column width="45" align="center" v-if="isRadio">
任超 committed
18
        <template slot-scope="scope">
任超 committed
19
          <el-radio v-model="selected" :label="scope.$index" class="table-radio"></el-radio>
任超 committed
20 21 22
        </template>
      </el-table-column>

任超 committed
23 24 25 26
      <lb-column v-bind="$attrs" v-for="(item, index) in column" :key="index" :column="item">
      </lb-column>
    </el-table>

任超 committed
27 28
    <el-table v-else ref="elTable" class="table-fixed" :row-style="{ height: '50px' }" :border='border'
      :row-class-name="tableRowClassName" :show-header='showHeader'
renchao@pashanhoo.com committed
29 30
      :header-cell-style="{ background: 'rgb(236, 245, 255)' }" v-bind="$attrs" :max-height="maxHeight"
      v-on="$listeners" :data="data" style="width: 100%" :span-method="this.merge ? this.mergeMethod : this.spanMethod">
任超 committed
31

renchao@pashanhoo.com committed
32
      <el-table-column width="45" align="center" v-if="isRadio">
任超 committed
33
        <template slot-scope="scope">
任超 committed
34
          <el-radio v-model="selected" :label="scope.$index" class="table-radio"></el-radio>
任超 committed
35 36
        </template>
      </el-table-column>
任超 committed
37 38 39
      <lb-column v-bind="$attrs" v-for="(item, index) in column" :key="index" :column="item">
      </lb-column>
    </el-table>
任超 committed
40

任超 committed
41
    <br>
任超 committed
42
    <el-pagination class="lb-table-pagination" v-if="pagination" v-bind="$attrs" v-on="$listeners" background
任超 committed
43
      :page-sizes="[10, 20, 50, 100]" layout="total, sizes, prev, pager, next" @current-change="paginationCurrentChange"
任超 committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
      :style="{ 'margin-top': paginationTop, 'text-align': paginationAlign }">
    </el-pagination>
  </div>
</template>

<script>
import LbColumn from './lb-column'
export default {
  props: {
    column: Array,
    data: Array,
    spanMethod: Function,
    pagination: {
      type: Boolean,
      default: true,
    },
任超 committed
60 61
    isRadio: {
      type: Boolean,
任超 committed
62
      default: false,
任超 committed
63
    },
任超 committed
64 65
    border: {
      type: Boolean,
任超 committed
66
      default: true,
任超 committed
67 68 69 70 71 72 73 74 75 76 77
    },
    showHeader: {
      type: Boolean,
      default: true,
    },
    paginationTop: {
      type: String,
      default: '0',
    },
    heightNum: {
      type: Number,
任超 committed
78
      default: 265,
任超 committed
79
    },
任超 committed
80
    maxHeight: {
任超 committed
81 82
      type: Number,
      default: 500
任超 committed
83
    },
任超 committed
84 85 86 87 88 89 90 91 92 93
    heightNumSetting: {
      type: Boolean,
      default: false,
    },
    customClass: {
      type: String,
      default: '',
    },
    paginationAlign: {
      type: String,
任超 committed
94
      default: 'left',
任超 committed
95 96 97 98 99 100 101 102
    },
    merge: Array,
  },
  components: {
    LbColumn,
  },
  data () {
    return {
任超 committed
103
      tableHeight: '',
任超 committed
104 105
      mergeLine: {},
      mergeIndex: {},
任超 committed
106
      selected: ''
任超 committed
107 108 109 110 111 112 113 114 115 116 117 118
    }
  },
  created () {
    this.getMergeArr(this.data, this.merge)
    this.getHeight()
  },
  computed: {
    dataLength () {
      return [] || this.data.length
    },
  },
  methods: {
任超 committed
119 120 121 122 123
    // 单选
    singleElection (row) {
      this.selected = this.data.indexOf(row);
    },

任超 committed
124 125 126 127 128 129
    tableRowClassName ({ row, rowIndex }) {
      if (rowIndex % 2 === 1) {
        return 'interlaced';
      }
    },
    getHeight () {
任超 committed
130 131
      if (!this.heightNumSetting) {
        this.tableHeight = window.innerHeight - this.heightNum
任超 committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
      }
    },
    clearSelection () {
      this.$refs.elTable.clearSelection()
    },
    toggleRowSelection (row, selected) {
      this.$refs.elTable.toggleRowSelection(row, selected)
    },
    toggleAllSelection () {
      this.$refs.elTable.toggleAllSelection()
    },
    toggleRowExpansion (row, expanded) {
      this.$refs.elTable.toggleRowExpansion(row, expanded)
    },
    setCurrentRow (row) {
      this.$refs.elTable.setCurrentRow(row)
    },
    clearSort () {
      this.$refs.elTable.clearSort()
    },
    clearFilter (columnKey) {
      this.$refs.elTable.clearFilter(columnKey)
    },
    doLayout () {
      this.$refs.elTable.doLayout()
    },
    sort (prop, order) {
      this.$refs.elTable.sort(prop, order)
    },
    paginationCurrentChange (val) {
      this.$emit('p-current-change', val)
    },
    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
            }
          }
        })
      })
    },
    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>
任超 committed
208
<style  rel="stylesheet/scss" scoped lang="scss">
任超 committed
209
.lb-table {
任超 committed
210 211
  margin-top: 1px;

任超 committed
212
  .interlaced {
任超 committed
213 214
    background: #fafcff;
    border: 1px solid #ebf2fa;
任超 committed
215 216
  }
}
任超 committed
217 218

/deep/.el-table .cell {
liangyifan committed
219 220 221
  padding-left: 3px;
  padding-right: 3px;
}
任超 committed
222

任超 committed
223 224 225 226 227 228 229 230
.table-radio {
  /deep/.el-radio__label {
    display: none;
  }
}

/deep/.el-radio {
  margin-right: 5px !important;
任超 committed
231
}
任超 committed
232
</style>