fuliqi
2024-01-24 29c1e7eb5ac16e90d8991a86c1c071bc312ec8d9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
<template>
  <div class="listCon-style">
    <common-form :form="form" ref="form" @handle-Value="handleFormValue" :formLabel="formLabel"
                 :isExistBtn="isExistBtn" :showFormItem="showFormItem" :labelWidth="labelWidth"
                 :defaultPadding="defaultPadding" @handle-Label-Value="handleLabelValue">
      <template slot="topForm" slot-scope="scope">
        <slot name="topForm" :data="scope.data">
        </slot>
      </template>
      <template slot="otherElement" slot-scope="scope">
        <slot name="otherElement" :data="scope.data">
        </slot>
      </template>
    </common-form>
    <div class="main-b-style">
      <div class="operationSection">
        <slot name="operationSection"></slot>
      </div>
      <div class="tabItemSel">
        <slot name="tabItem"></slot>
      </div>
      <el-table :data="tableData" border ref="table" v-on="$listeners" :row-key="dataKey"
                @selection-change="handleSelectionChange" @select="handleSelect"
                :row-class-name="rowClassName" :row-style="setRowStyle" class="tableStyle"
                :expand-row-keys="entexpands" @expand-change="historicalEvaluation"
                :header-cell-class-name="headerCellClassName" @select-all="handleSelectAll">
        <el-table-column type="selection" v-if="multipleSelected" :reserve-selection="true"
                         label="全选" width="50" :selectable="selectable"></el-table-column>
        <el-table-column v-if="indexColumn" type="index" label="序号" width="50" align="center">
        </el-table-column>
        <slot name="columns"></slot>
      </el-table>
      <div ref="pageStyle" v-if="isShowPage" class="pageStyle">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                       :current-page="pageInfo.pageNum" :page-sizes="sizes"
                       :page-size="pageInfo.pageSize"
                       layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
      <custom-scroll :visible="shouldShowScroll" v-if="isDisplayScroll" :boxWidth="boxWidth"
                     :contentWidth="contentWidth" :scrollLeft.sync="scrollLeft"></custom-scroll>
    </div>
  </div>
</template>
<script>
import commonForm from '@/components/formTemplate/commonForm.vue'
import { mapGetters } from 'vuex'
import customScroll from '@/components/scrollComponent/customScroll.vue'
 
export default {
  components: {
    commonForm, customScroll
  },
  props: {
    /**
       * 表单数据
       */
    form: {
      type: Object,
      default: function() {
        return {}
      }
    },
    /**
     *标签数据
     */
    formLabel: {
      type: Array,
      default: function() {
        return []
      }
    },
    /**
     * 列表数据
     */
    tableData: {
      type: Array,
      default: function() {
        return []
      }
    },
    /**
     * 是否包含索引列
     */
    indexColumn: {
      type: Boolean,
      default: true
    },
    /**
     * 数据总数
     */
    total: {
      type: Number,
      default: 0
    },
    /**
     * 是否多选
     */
    multipleSelected: {
      type: Boolean,
      default: false
    },
    isShowPage: {
      type: Boolean,
      default: true
    },
    /**
     * 行样式style
     */
    setRowStyle: {
      type: Function,
      default: null
    },
    sizes: {
      type: Array,
      default: function() {
        return [10, 20, 30, 50]
      }
    },
    /**
     * 全选按钮是否不显示
     */
    headerCellClassName: {
      type: Function,
      default: null
    },
    /**
     * 复选框是否可被点击
     */
    selectable: {
      type: Function,
      default: null
    },
    /**
     * 无表单查询条件的情况下---是否存在按钮插槽
     */
    isExistBtn: {
      type: Boolean,
      default: false
    },
    /**
     * 是否展示自定义滚动条(主要针对弹出框)
     */
    isDisplayScroll: {
      type: Boolean,
      default: true
    },
    /**
     * 表单label宽度
     */
    labelWidth: {
      type: String,
      default: '80px'
    },
    // 展开/收起控制
    showFormItem: {
      type: Boolean,
      default: false
    },
    /**
* 默认padding值
*/
    defaultPadding: {
      type: String,
      default: '40px 20px 12px'
    },
    /**
    * 点击表格行是否可展开
    */
    isClickRowExpand: {
      type: Boolean,
      default: false
    },
    /**
     *默认选中的值
    */
    defaultValue: {
      type: Array,
      default: function() {
        return []
      }
    },
    /**
     * 数据行的键
     */
    dataKey: {
      type: String,
      default: 'id'
    },
    /**
     *展开行---是否隐藏其他行
    */
    isHiddenRow: {
      type: Boolean,
      default: false
    },
    /**
    *查看条件变化,是否清空表格勾选项
   */
    isClearCheckItem: {
      type: Boolean,
      default: true
    },
    // 分页参数
    pageParams: {
      type: Object,
    },
  },
  data() {
    return {
      pageInfo: {
        pageNum: 1,
        pageSize: this.sizes[0]
      },
      tableHeight: 300,
      scrollWidth: 0,
      scrollLeft: 0,
      shouldShowScroll: true, // 是否展示滚动条,
      boxWidth: '',
      contentWidth: '',
      selecedRow: [],
      entexpands: [], // 展开行的 keys 数组
      // 表格全量选择
      indeterminate: false, // checkBox只负责样式控制
      allCheck: false // 全选所有
    }
  },
  computed: {
    ...mapGetters([
      'mainContainerHeight'
    ])
  },
  watch: {
    scrollLeft() {
      this.dom = this.$refs.table.bodyWrapper
      // 滚动距离
      this.dom.scrollLeft = this.scrollLeft
    },
    tableData() {
      this.$nextTick(this.updateCustomScroll)
    },
    '$refs.table.bodyWidth'() {
      this.updateCustomScroll()
    },
    pageParams: {
      handler() {
        this.pageInfo.pageNum = this.pageParams.pageNum;
        this.pageInfo.pageSize = this.pageParams.pageSize;
      },
      deep: true
    },
  },
  methods: {
    /**
  * 设置表格选中状态
  */
    setTableChecked(row, bool) {
      const _this = this
      _this.$nextTick(() => {
        if (row) {
          _this.$refs.table.toggleRowSelection(row, bool)
        }
      })
    },
    /**
     * 当表单值改变时
     */
    handleFormValue(data) {
      this.$emit('handle-form-Value', data)
    },
    /**
     * 当表单label值改变
     */
    handleLabelValue(data) {
      this.$emit('handle-Label-Value', data)
    },
    rowClassName({ row, rowIndex }) {
      if (JSON.stringify(this.selecedRow).indexOf(JSON.stringify(row)) !== -1) {
        return 'checkedTableRow'
      }
    },
    /**
     * 行选中事件处理
     * @param {*} rows 行数据
     */
    handleSelectionChange(rows) {
      this.$emit('selected', rows)
      this.selecedRow = rows
    },
    /**
     * 当用户手动勾选全选 Checkbox 时触发的事件
     * @param {*} rows 行数据
     */
    handleSelectAll(selection) {
      this.$emit('select-all', selection)
    },
    /**
     *当用户手动勾选数据行的 Checkbox 时触发的事件
     */
    handleSelect(selection, row) {
      this.$emit('handle-selected', selection, row)
    },
    /**
     *清空表格选择项
     */
    clearTableSelected() {
      if (this.multipleSelected) {
        this.$refs.table.clearSelection()
      }
    },
    /**
     * 一页显示数量
     */
    handleSizeChange(val) {
      this.pageInfo.pageSize = val
      this.onPageInfoChange()
    },
    /**
     *查询当前页
     */
    handleCurrentChange(val) {
      this.pageInfo.pageNum = val
      this.onPageInfoChange()
    },
    /**
     * 重置分页数据
     */
    resetTable() {
      this.entexpands = []
      this.pageInfo.pageNum = 1
      this.pageInfo.pageSize = this.sizes[0]
    },
    /**
     * 外部条件变化时处理
     */
    handleConditionChange() {
      this.resetTable()
      this.onPageInfoChange()
    },
    /**
     * 重置表单
     */
    resetForm() {
      this.$refs.form.resetForm()
    },
    /**
     * 重置条件
     */
    reloadCurrent() {
      this.resetForm()
      if (this.isClearCheckItem) {
        this.clearTableSelected() //  清空表格勾选项
      }
      this.handleConditionChange()
    },
    /**
     * 触发条件变化事件
     */
    changeCondition() {
      if (this.isClearCheckItem) {
        this.clearTableSelected() //  清空表格勾选项
      }
      this.handleConditionChange()
    },
    /**
     * 触发分页信息变化事件
     */
    onPageInfoChange() {
      this.$emit('page-info-change', this.pageInfo)
    },
    /**
     * 获取分页信息
     */
    getPageInfo() {
      return this.pageInfo
    },
    resize() {
      var that = this
      that.tableHeight = that.mainContainerHeight - this.$refs.form.$el.clientHeight - this.$refs.pageStyle.clientHeight - 210
    },
    /**
     * 初次加载以及滚动滚动条时判断是否展示滚动条
     */
    isShowScroll() {
      let rows = document.querySelectorAll('.el-table__row')
      if (rows.length > 0) {
        const rect = rows[rows.length - 1].getBoundingClientRect()
        const viewportHeight = window.innerHeight || document.documentElement.clientHeight
 
        this.shouldShowScroll = rect.bottom > viewportHeight
      } else {
        rows = document.querySelectorAll('.el-table__row')
        this.shouldShowScroll = false
      }
    },
    /**
     * 展开当前行,上一条数据隐藏
     */
    historicalEvaluation(row, expandedRows) {
      var that = this
      if (that.isHiddenRow) {
        if (expandedRows.length) {
          that.entexpands = []
          if (row) {
            that.entexpands.push(row[this.dataKey])
          }
        } else {
          that.entexpands = []
        }
      }
      this.$emit('expand-change', row, that.entexpands)
    },
    /**
     * 获取table节点
     */
    tableNode() {
      return this.$refs.table
    },
    /**
     * 监听最外面的滚动条(当滚动条滚动到底部,隐藏自定义滚动条)
     */
    outermostScroll() {
      const mainContainer = document.getElementById('main-container')
      if (mainContainer) {
        mainContainer.addEventListener('scroll', () => {
          this.isShowScroll()
        })
      }
    },
    updateCustomScroll() {
      setTimeout(() => {
        this.boxWidth = getComputedStyle(this.$refs.table.bodyWrapper).width
        this.contentWidth = getComputedStyle(this.$refs.table.bodyWrapper.querySelector('.el-table__body')).width
      }, 100)
    }
  },
  updated() {
    this.isShowScroll()
  },
  mounted() {
    const that = this
    that.dom = that.$refs.table.bodyWrapper
    window.addEventListener('resize', () => {
      // 监听浏览器窗口
      // 获取需要绑定的table
      that.$nextTick(() => {
        if (that.$refs.table) {
          that.$refs.table.$nextTick(() => {
            that.updateCustomScroll()
          })
        }
      })
    }, false)
    that.dom.addEventListener('scroll', () => {
      // 滚动距离
      that.scrollLeft = that.dom.scrollLeft
    })
    this.outermostScroll()
  }
}
</script>
<style lang="scss" scoped>
.operationSection {
  background-color: #fff;
  border-top-left-radius: 4px;
  border-top-right-radius: 4px;
}
.tableStyle {
  margin-top: 14px;
  /deep/.el-table__expanded-cell {
    .el-table__body-wrapper {
      overflow-x: hidden !important;
    }
  }
  /* 解决 elementUI 切换table后 el_table 固定列下方多了一条线 */
  /deep/ .el-table__fixed,
  /deep/ .el-table__fixed-right {
    height: 100% !important; //设置高优先,以覆盖内联样式
  }
}
/deep/.checkedTableRow {
  background: #fff3e0 !important;
}
.main-b-style {
  background-color: #fff;
  border-radius: 4px;
  padding: 20px 20px 76px;
}
</style>