schangxiang@126.com
2025-05-07 cace264ad9d86a7831099810b079da1141957add
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
import { defineComponent, ref, reactive, computed, onMounted, watch } from 'vue'
import type { PropType } from 'vue'
import styles from './CanvasTableS2.module.scss'
import { PivotSheet, S2DataConfig, TableSheet, type S2Options } from '@antv/s2'
import { _t } from '@/libs/Language/Language'
 
export default defineComponent({
  name: 'CanvasTableS2',
  props: {
    tableHeader: {
      type: Array as PropType<any[]>,
      required: true,
    },
    dataList: {
      type: Array as PropType<any[]>,
      required: true,
    },
    fixedColumn: {
      type: Number,
      default: 0,
    },
    saveConfig: {
      type: Function,
      default: () => {},
    },
    isExtendedSummaryTable: {
      type: Boolean,
      default: false,
    },
    summaryDataStructure: {
      type: Array as PropType<any[]>,
      default: () => [],
    },
    tableColumns: {
      type: Array as PropType<any[]>,
      default: () => [],
    },
    onSortChange: {
      type: Function,
      default: () => {},
    },
  },
  setup(props, { emit, slots, expose }) {
    const tableRef = ref<HTMLDivElement>()
    const tableHeight = ref(0)
    const tableWidth = ref(0)
 
    /**
     * 处理扩展总表和总表和原数据
     */
    const data = computed(() => {
      return props.dataList
    })
 
    const findSeq = (structure: any[], seq: string) => {
      return structure.find((item) => {
        return Object.entries(item).find(([key, value]) => {
          if (key === 'Seq' && value[0].data) {
            return value[0].data == seq
          }
        })
      })
    }
 
    const summaryDataStructureComputed = computed(() => {
      const structure = props.summaryDataStructure
      const data = props.dataList
      const result = []
      data.forEach((item, index: number) => {
        if (!Object.keys(structure).length) return
        const findSeqItem = findSeq(structure, item.Seq)
        // 需要判断seq是否存在,如果存在,将item设置为{}
        if (findSeqItem) {
          if (result.find((resultItem) => resultItem.seq == item.Seq)) {
            result.push({
              item: {},
              seq: item.Seq,
            })
          } else {
            result.push({
              item: findSeqItem,
              seq: item.Seq,
            })
          }
        }
      })
 
      return result.map((item) => {
        if (item.item) {
          return item.item
        } else {
          return {}
        }
      })
    })
 
    /**
     * 合并单元格
     */
    const mergeCells = computed<any>(() => {
      const mergeCells = []
      // if (!props.isExtendedSummaryTable) {
      //   return mergeCells
      // }
      const mergeCellsMap = {}
      const mergeCellArr = []
      summaryDataStructureComputed.value.forEach((item, index: number) => {
        Object.entries(item).forEach(([key, arr]: [string, any[]]) => {
          const column = columnMap.value[key]
          arr.forEach((row, i) => {
            // mergeCellsMap[column?.index] = mergeCellsMap[column?.index] || []
            // mergeCellsMap[column?.index].push({
            //   colIndex: column?.index,
            //   rowIndex: index + row.rowSpan,
            //   showText: false,
            // })
            mergeCells.push({
              row: index,
              col: column?.index,
              rowspan: row.rowSpan,
              colspan: 1,
              key: key,
              name: column?.name,
            })
          })
        })
      })
      const filterMergeCells = mergeCells.filter(
        (item) =>
          (item.rowspan > 1 || item.colspan > 1) && item.col !== undefined
      )
      filterMergeCells.forEach((item) => {
        const cells = []
        if (item.rowspan > 1) {
          for (let index = 0; index < item.rowspan; index++) {
            cells.push({
              colIndex: item.col,
              rowIndex: item.row + index,
              showText: index == 0 ? true : false,
            })
          }
        }
        if (cells.length) {
          mergeCellArr.push(cells)
        }
      })
      // console.log(mergeCellArr, 'mergeCellArr')
      // Object.values(mergeCellsMap).forEach((arr: any[]) => {
      //   arr.forEach((item,index) => {
      //     if(index == 0) {
      //       item.showText = true
      //       item.
      //     } else {
 
      //     }
      //   })
      // })
 
      return mergeCellArr
    })
 
    const columnMap = computed(() => {
      const map = {}
      props.tableColumns.forEach((item, index) => {
        map[item.key] = {
          ...item,
          index: index,
        }
      })
      return map
    })
    /**
     * 表头数据
     */
    const columns = computed(() => {
      return props.tableHeader.map((item) => {
        return {
          title: item.name,
          field: item.key,
          name: item.name,
          width: item.width,
          children: item.childs.map((child) => {
            return {
              title: child.name,
              name: child.name,
              field: child.key,
              width: child.width,
            }
          }),
        }
      })
    })
 
    watch(
      () => props.dataList,
      () => {
        if (Array.isArray(props.dataList) && data.value.length) {
          tableHeight.value = tableRef.value?.clientHeight
          tableWidth.value = tableRef.value?.clientWidth
          if (tableWidth.value && tableHeight.value) {
            render(tableWidth.value, tableHeight.value)
          }
        }
      }
    )
 
    const render = async (width, height) => {
      const filedWidthMap = {}
      props.tableColumns.forEach((column) => {
        filedWidthMap[column.key] = column.width || 200
      })
      const s2Options: S2Options = {
        width,
        height,
        hierarchyType: 'grid',
        mergedCellsInfo: mergeCells.value,
        style: {
          colCell: {
            widthByField: filedWidthMap,
          },
        },
        // 提高滚动性能
        transformCanvasConfig(renderer) {
          renderer.setConfig({
            enableCulling: true,
            enableRenderingOptimization: true,
          })
        },
      }
      const s2DataConfig: S2DataConfig = {
        fields: {
          columns: columns.value,
        },
        data: data.value,
      }
      const container = document.querySelector('#s2_table')
 
      const s2 = new TableSheet(container, s2DataConfig, s2Options)
      s2.setTheme({
        dataCell: {
          bolderText: {
            textAlign: 'center',
            textBaseline: 'middle',
          },
          text: {
            textAlign: 'center',
            textBaseline: 'middle',
          },
        },
      })
      await s2.render()
    }
    // onMounted(async () => {
    //   // 获取表格的高度
    //   const t = setTimeout(() => {
    //     tableHeight.value = tableRef.value?.clientHeight
    //     tableWidth.value = tableRef.value?.clientWidth
    //     render(tableWidth.value, tableHeight.value)
    //     clearTimeout(t)
    //   }, 0)
    // })
    return () => {
      return (
        <div
          class={styles.extendedSummaryTable}
          id="s2_table"
          ref={tableRef}
        ></div>
      )
    }
  },
})