zhuoyuan.wang
2024-06-19 15ebe96f28cadec6a726c5324593a40bbf56205f
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
<template>
  <div class="app-table-pro">
    <div class="header">
      <div class="title">
        {{ $props.title }}
        <el-popover
          placement="bottom"
          title="多选列表"
          :width="'60%'"
          trigger="click"
          v-if="selection"
        >
          <el-table
            show-overflow-tooltip
            :data="selectedList"
            border
            :height="300"
          >
            <template v-for="e in groupColumns" :key="e.id">
              <el-table-column v-if="e.group.indexOf('$') === -1" :label="e.group">
                <template v-for="column in e.columns" :key="column.id">
                  <table-column :columns="[column]">
                    <template v-for="slot in Object.keys($slots)" #[slot]="scope">
                      <slot :name="slot" v-bind="scope"/>
                    </template>
                  </table-column>
                </template>
              </el-table-column>
              <table-column :columns="e.columns">
                <template v-for="slot in Object.keys($slots)" #[slot]="scope">
                  <slot :name="slot" v-bind="scope"/>
                </template>
              </table-column>
              <el-table-column
                label="操作"
                width="70"
                fixed
              >
                <template #default="scope">
                  <el-button
                    style="padding: 5px 10px;"
                    type="danger"
                    icon="Delete"
                    @click="onSelectedDel(scope.row)"
                  >
                  </el-button>
                </template>
              </el-table-column>
            </template>
          </el-table>
          <template #reference>
            <el-badge :value="selectedList.length">
              <el-button icon="Tickets" link class="icon">
              </el-button>
            </el-badge>
          </template>
        </el-popover>
      </div>
      <div class="button">
        <div>
          <slot name="header-button"/>
        </div>
      </div>
    </div>
    <el-table
      ref='tableRef'
      v-bind="$props"
      show-overflow-tooltip
      :highlight-current-row="!selection"
      :height="complementHeight === -1 ? 'inherit' : height"
      :data="tableData"
      :border="border"
      :row-key="rowKey"
      :cell-style="cellStyle"
      :expand-row-keys="expandRowKeys"
      @current-change="onTableCurrent"
      @select="selectionChange"
      @select-all="selectionChange"
    >
      <el-table-column type="expand" v-if="expand">
        <template #default="scope">
          <slot name="table-expand" v-bind="scope"/>
        </template>
      </el-table-column>
 
      <el-table-column type="selection" v-if="selection" width="40">
      </el-table-column>
 
      <template v-for="e in groupColumns" :key="e.id">
 
        <el-table-column v-if="e.group.indexOf('$') === -1" :label="e.group">
          <template v-for="column in e.columns" :key="column.id">
            <table-column :columns="[column]">
              <template v-for="slot in Object.keys($slots)" #[slot]="scope">
                <slot :name="slot" v-bind="scope"/>
              </template>
            </table-column>
          </template>
        </el-table-column>
        <table-column :columns="e.columns">
          <template v-for="slot in Object.keys($slots)" #[slot]="scope">
            <slot :name="slot" v-bind="scope"/>
          </template>
        </table-column>
      </template>
      <!-- 插入表格最后一行之后的插槽 -->
      <template #append>
        <slot name="append"/>
      </template>
      <!-- 无数据 -->
      <template #empty>
        <el-empty/>
      </template>
    </el-table>
  </div>
</template>
 
<script setup>
 
import * as tools from '@/utils/tools.js'
 
import {useSettingStore} from '@/store/modules';
 
const settingsStore = useSettingStore();
 
import {table, context} from '@/hooks';
 
const {useForceUpdate} = context;
import TableColumn from "@/components/table/TablePro/TableColumn/index.vue";
 
const {useSelection, useExpand} = table;
 
const tableRef = ref();
 
const props = defineProps({
  columns: {
    type: Array,
    default: []
  },
  border: {
    type: Boolean,
    default: true
  },
  title: {
    type: String,
    default: ''
  },
  rowKey: {
    type: String,
    default: 'id'
  },
  expand: {
    type: Boolean,
    default: false
  },
  selection: {
    type: Boolean,
    default: false
  },
  subCurrent: {
    type: Function,
    default: () => {
 
    }
  },
  sortable: {
    type: Object,
    default: {}
  },
  complementHeight: {
    type: Number,
    default: 0
  },
  subSelection: {
    type: Function,
    default: () => {
 
    }
  }
});
 
 
const [$props, forceUpdate] = useForceUpdate(props);
 
const $forceUpdate = async (force) => {
  forceUpdate(force);
  onInit();
}
 
const {border, rowKey, expand, complementHeight} = $props;
 
const tableData = ref([]);
 
const [selectionChange, selectedList, selectedIds, toggleSelection] = useSelection(rowKey, tableRef, tableData);
 
const [expandRowKeys, clearExpand] = useExpand();
 
const cellStyle = ({row}) => ({
  backgroundColor: selectedIds.value.includes(row[rowKey]) ? 'var(--el-color-primary-light-6)' : ''
});
/**
 * 接收 columns 并设置为响应式
 * @type {UnwrapNestedRefs<any[]>}
 */
const useColumns = ref([]);
 
 
const onSelectedDel = (row) => {
 
  selectedList.value.forEach((e, i) => {
    if (row[rowKey] === e[rowKey]) {
      selectedList.value.splice(i, 1);
      toggleSelection();
    }
  });
}
 
const onInit = () => {
  if (JSON.stringify(useColumns.value) !== JSON.stringify($props.columns)) {
    const columns = $props.columns.map((e, index) => {
      e.group = e.group || `$${index}`;
      e.isShow = true;
      e.id = tools.uuid();
      e.table = e.table || {};
      e.table.sortable = false;
      return e;
    });
 
    useColumns.value = columns;
  }
 
}
 
watch(tableData, (val) => {
  clearExpand();
  if (Object.keys(dataForm.current).length > 0) {
    if (!val.find(e => dataForm.current[rowKey] === e[rowKey])) {
      onTableCurrent({});
    }
  }
});
 
watch(tableData, (val) => {
  if (props.sortable.sort) {
    val.sort(function (a, b) {
      return props.sortable.order === 'desc' ? a[props.sortable.sort] - b[props.sortable.sort] : b[props.sortable.sort] - a[props.sortable.sort];
    });
  }
});
 
const dataForm = reactive({
  current: {}
});
 
const setTableCurrent = (row = {}) => {
  dataForm.current = row;
  tableRef.value?.setCurrentRow(row);
}
 
watch(selectedList, (newValue) => {
  props.subSelection(newValue);
});
 
const onTableCurrent = (row = {}) => {
  setTableCurrent(row);
  props.subCurrent(row);
}
 
const groupColumns = computed(() => {
  const group = [...new Set(useColumns.value.map(column => column.group))];
  return group.map(e => {
    return {
      id: tools.uuid(),
      group: e,
      columns: useColumns.value.filter(column => column.group === e)
    }
  });
});
 
const getTableCurrent = () => {
  return dataForm.current;
}
 
const formatData = (format, data) => {
  return format ? format(data) : (data || '')
}
 
const height = computed(() => window.innerHeight - 332 - (settingsStore.tagsView ? 39 : 0) + complementHeight)
 
const getSelectedList = () => selectedList.value;
 
const getSelectedIds = () => selectedIds.value;
 
const setSelected = (selected) => {
  selectedList.value = selected;
}
 
 
onMounted(() => {
  onInit();
});
 
const setTableData = (list) => {
  tableData.value.splice(0, tableData.value.length)
  tableData.value = tableData.value.concat(list);
}
 
const getTableData = () => tableData.value;
/**
 * 暴露给父组件的参数和方法 (外部需要什么,都可以从这里暴露出去)
 */
defineExpose({
  /**
   * 初始化
   */
  getTableData,
  setTableData,
 
  setSelected,
  getTableCurrent,
  setTableCurrent,
  getSelectedList,
  getSelectedIds,
  $forceUpdate,
});
 
</script>
 
<style lang='scss'>
.app-table-pro {
  padding: 10px;
  background: #fff;
  border-radius: 5px;
  border: 1px solid var(--el-border-color-light);
  overflow: hidden;
 
  > .header {
    align-items: center;
    padding-bottom: 10px;
    display: flex;
    justify-content: space-between;
 
    .title {
      font-size: 14px;
      color: var(--el-text-color-primary);
      font-weight: 600;
      display: flex;
      align-items: center;
 
      .icon {
        margin-left: 3px;
 
        .el-icon {
          font-size: 17px;
        }
      }
    }
 
    .tools {
 
      button {
        margin-left: 10px;
      }
 
    }
 
    .button {
      flex: 1;
      overflow: auto;
      text-align: right;
      margin-left: 15px;
      margin-right: 15px;
 
      > div {
        display: inline-flex;
      }
    }
  }
}
</style>