智慧园区前端大屏
shuishen
2024-12-18 9a37c5e1b2190c3f6ec71483923922189091dd52
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
<template>
  <div class="h100 flex f-d-c cur-table-box" v-loading="loading" element-loading-background="rgba(46, 81, 136, 0.9)">
    <div class="h0 flex-1 table-content" ref="TableContent">
      <el-table empty-text="暂无数据" ref="tableData" :height="tableHeight" :row-key="rowKey" :border="border"
        :size="tableSize" :data="data" @selection-change="handleSelectionChange"
        @current-change="handleTableCurrentChange" @row-click="handleTableRowClick" @sort-change="handleSortChange"
        v-bind="otherConfig">
        <template v-for="(item, index) in columns">
          <!-- 选择框 -->
          <el-table-column v-if="item.selection" type="selection" width="36" :fixed="item.fixed"
            :reserve-selection="reserveSelection" align="center" header-align="center"
            :key="`selection_${index}`"></el-table-column>
          <!-- 自定义单选框 -->
          <el-table-column v-else-if="item.singleSelection" width="50" :fixed="item.fixed" align="center"
            header-align="center" :key="`singleSelection_${index}`">
            <template slot-scope="scope">
              <el-checkbox v-model="scope.row.isSingleSelected"
                @change="handleSingleSelectionChange(scope.row)"></el-checkbox>
            </template>
          </el-table-column>
          <!-- 序号 -->
          <el-table-column v-else-if="item.index" align="center" type="index" width="48" :fixed="item.fixed" label="序号"
            :index="item.indexMethod || indexMethod" :key="`index_${index}`"></el-table-column>
          <!-- 多级表头 -->
          <el-table-column v-else-if="item.multi" align="center" :label="item.label" :key="`multi_${index}`">
            <el-table-column v-for="(child, childIndex) in item.children" :key="`child_${index}_${childIndex}`"
              v-bind="child">
            </el-table-column>
          </el-table-column>
          <!-- 自定义内容 -->
          <slot v-else-if="item.slot" :name="item.slot"></slot>
 
          <el-table-column v-else-if="item.sortable" sortable="custom" align="center" show-overflow-tooltip
            v-bind="item" :fixed="item.fixed" :width="item.width" :key="`normal_sort${index}`"
            :render-header="tableRenderHeader"></el-table-column>
          <!-- 常规字段 -->
          <el-table-column v-else align="center" show-overflow-tooltip v-bind="item" :fixed="item.fixed"
            :width="item.width" :key="`normal_${index}`" :render-header="tableRenderHeader"></el-table-column>
        </template>
      </el-table>
    </div>
 
    <div v-show="isPaginationShow && pagination.total" class="pagination-content">
      <el-pagination background :page-sizes="[10, 20, 50, 100]" layout="prev, pager, next" :pager-count="5"
        :current-page.sync="pagination.current" :page-size="pagination.size" :total="pagination.total"
        @size-change="handleSizeChange" @current-change="handleCurrentChange">
      </el-pagination>
    </div>
    <!-- 自定义内容 -->
    <slot name="custom-content"></slot>
  </div>
</template>
 
 
<script setup>
import { onMounted, onUnmounted } from 'vue'
const emit = defineEmits(['getData', 'changeSelection', 'changeCurrent', 'changeSingleSelection', 'rowClick', 'changeSort'])
 
const { columns, data, pagination, } = defineProps({
  columns: {
    type: Array,
    default: () => [],
  },
  data: {
    type: Array,
    default: () => [],
  },
  pagination: {
    type: Object,
    default: () => ({
      current: 1,
      size: 10,
      total: 0,
    }),
  },
  isPaginationShow: {
    type: Boolean,
    default: true,
  },
  tableSize: {
    type: String,
    default: 'small',
  },
  border: {
    type: Boolean,
    default: false
  },
  otherConfig: {
    type: Object,
    default: () => { },
  },
  loading: {
    type: Boolean,
    default: false,
  },
  // 支持跨页选择的两个配置项
  rowKey: {
    type: String,
    default: ''
  },
  reserveSelection: {
    type: Boolean,
    default: false
  }
})
 
const tableHeight = ref(0)
const TableContent = ref(null)
const tableData = ref(null)
 
onMounted(() => {
  window.addEventListener('resize', this.handleWindowResize)
})
 
onUnmounted(() => {
  window.removeEventListener('resize', this.handleWindowResize)
})
 
watch(
  () => data,
  (newData) => {
    if (newData) {
      nextTick(() => {
        handleWindowResize()
      })
    }
  }
)
 
watch(
  () => columns,
  (newColumns) => {
    if (newColumns && newColumns.length) {
      nextTick(() => {
        tableData.value.doLayout()
      })
    }
  },
  { deep: true }
)
 
// 切换页码
const handleCurrentChange = () => {
  emit('getData')
},
// 切换每页条数
const handleSizeChange = (value) => {
  pagination.size = value
  emit('getData')
}
 
// 切换选择
const handleSelectionChange = (val) => {
  emit('changeSelection', val)
}
// 单选
const handleTableCurrentChange = (currentRow) => {
  emit('changeCurrent', currentRow)
}
// 自定义单选
const handleSingleSelectionChange = (currentRow) => {
  emit('changeSingleSelection', currentRow)
}
// 点击行
const handleTableRowClick = (currentRow) => {
  emit('rowClick', currentRow)
}
// 排序
const handleSortChange = (val) => {
  emit('changeSort', val)
}
// 自定义表头渲染方法
const tableRenderHeader = (h, { column }) => {
  const span = document.createElement('span')
  span.innerText = column.label
  document.body.appendChild(span)
  const realWidth = span.getBoundingClientRect().width
  let columnWidth = realWidth + 22 // 22: 左右padding 10*2 + border预留 2
  if (column.sortable === 'custom') columnWidth += 30
  column.minWidth = columnWidth
  document.body.removeChild(span)
  return h('span', column.label)
}
// 序号列自定义方法
const indexMethod = (index) => {
  const { current, size } = this.pagination
  return (current - 1) * size + index + 1
}
// 页面缩放重新计算 table高度
const handleWindowResize = () => {
  tableHeight = TableContent.offsetHeight
}
</script>
 
<script>
export default {
  name: 'GlobalTable'
}
</script>
 
<style lang="scss" scoped>
.cur-table-box {
  ::-webkit-scrollbar {
    width: 6px;
    height: 6px;
    background-color: none;
  }
 
  ::-webkit-scrollbar-thumb {
    background: #4ACFFF;
    border-radius: 3px;
  }
 
  ::-webkit-scrollbar-track {
    background: #1D486E;
  }
}
 
.table-content {
  ::v-deep(.el-table) {
    &::before {
      content: none;
    }
 
    background: transparent !important;
    border: none;
 
    .el-table__header-wrapper {
      tr {
        height: 38px;
        background: rgba(35, 53, 92, 0.8) !important;
        box-sizing: border-box;
 
        th {
          background: transparent !important;
          border-bottom: 1px solid rgba(103, 135, 214, 1);
 
          .cell {
            color: #AFBED8;
          }
        }
      }
    }
 
    .el-table__body-wrapper {
      table {
        border-collapse: separate !important;
        border-spacing: 0 10px !important;
      }
 
      tr {
        height: 38px;
        background: rgba(36, 57, 110, 0.2);
        box-sizing: border-box;
        cursor: pointer;
 
        &:hover {
          td {
            color: #44C1EF;
            background: transparent !important;
          }
        }
 
        td {
          padding: 0;
          height: 38px;
          line-height: 38px;
          color: #D4E8F8;
          border-top: 1px solid rgba(110, 135, 197, 0.32);
          border-bottom: 1px solid rgba(110, 135, 197, 0.32);
          box-sizing: border-box;
        }
 
        td:first-child {
          border-left: 1px solid rgba(110, 135, 197, 0.32);
        }
 
        td:last-child {
          border-right: 1px solid rgba(110, 135, 197, 0.32);
        }
      }
    }
 
    .el-table__empty-block {
      background: rgba(36, 57, 110, 0.2);
 
      .el-table__empty-text {
        color: #D4E8F8;
      }
    }
  }
}
 
.pagination-content {
  margin: 10px 0;
  color: #D4E8F8;
 
  ::v-deep(.el-pagination) {
    .el-pagination__total {
      color: #D4E8F8;
    }
 
    .btn-prev,
    .btn-next {
      color: rgba(216, 235, 238, 0.8) !important;
      background: rgba(68, 193, 239, 0.1);
    }
 
    .el-pager {
 
      .btn-quickprev,
      .btn-quicknext,
      .number {
        color: rgba(216, 235, 238, 0.8) !important;
        background: rgba(68, 193, 239, 0.1);
      }
 
      .active {
        color: #FFFFFF;
        background: #44C1EF !important;
        opacity: 0.8;
      }
    }
  }
}
</style>