Skip to content

Commit 3360d9e

Browse files
perf: Label management performance optimization
1 parent 7e5f6fe commit 3360d9e

1 file changed

Lines changed: 12 additions & 34 deletions

File tree

ui/src/views/document/tag/TagDrawer.vue

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
</div>
3131
<el-table
3232
ref="tableRef"
33-
:data="pagedTableData"
33+
:data="pagedGroups"
3434
:span-method="spanMethod"
3535
v-loading="loading"
3636
:max-height="tableMaxHeight"
@@ -39,14 +39,7 @@
3939
@cell-mouse-leave="cellMouseLeave"
4040
>
4141
<el-table-column type="selection" width="55" />
42-
<el-table-column
43-
prop="key"
44-
:label="
45-
multipleSelection.length === 0
46-
? $t('views.document.tag.key')
47-
: `${$t('common.selected')} ${multipleSelection.length} ${$t('views.document.items')}`
48-
"
49-
>
42+
<el-table-column prop="key" :label="$t('views.document.tag.key')">
5043
<template #default="{ row }">
5144
<div class="flex-between">
5245
{{ row.key }}
@@ -134,8 +127,8 @@
134127
<el-pagination
135128
v-model:current-page="pageNum"
136129
v-model:page-size="pageSize"
137-
:total="groupedByKey.length"
138-
layout="total, prev, pager, next, sizes"
130+
:total="tableData.length"
131+
layout="prev, pager, next, sizes"
139132
:page-sizes="[10, 20, 50, 100]"
140133
/>
141134
</div>
@@ -228,36 +221,21 @@ const tableData = computed(() => {
228221
return result
229222
})
230223
231-
// 2) 按 key 分组(保持 key 的出现顺序)
232-
const groupedByKey = computed(() => {
233-
const map = new Map<string, any[]>()
234-
for (const row of tableData.value) {
235-
if (!map.has(row.key)) map.set(row.key, [])
236-
map.get(row.key)!.push(row)
237-
}
238-
// 每个元素代表一个 key 分组
239-
return Array.from(map.entries()).map(([key, rows]) => ({ key, rows }))
240-
})
241-
242-
// 3) 按“key 分组”做分页:每页 pageSize 个 key
224+
// 2) 按“key 分组”做分页:每页 pageSize 个 key
243225
const pagedGroups = computed(() => {
244226
const start = (pageNum.value - 1) * pageSize.value
245227
const end = start + pageSize.value
246-
return groupedByKey.value.slice(start, end)
247-
})
248-
249-
// 4) 当前页表格数据:把当前页的若干个 key 分组展开为行
250-
const pagedTableData = computed(() => {
251-
return pagedGroups.value.flatMap((g) => g.rows)
228+
return tableData.value.slice(start, end)
252229
})
253230
254231
// 5) 合并单元格:只在当前页内合并,同一个 key 的第一行 rowspan=该 key 在当前页的行数
255232
const spanMethod = ({ row, columnIndex }: any) => {
256233
// 注意:你现在有 selection 列,所以 key 列索引是 1;如需同时合并 value 列按需调整
257234
if (columnIndex === 0 || columnIndex === 1) {
258-
if (row.keyIndex === 0) {
259-
const sameKeyCount = pagedTableData.value.filter((item) => item.key === row.key).length
260-
return { rowspan: sameKeyCount, colspan: 1 }
235+
const sameKeyItems = pagedGroups.value.filter((item) => item.key === row.key)
236+
const isFirstItem = sameKeyItems.length > 0 && sameKeyItems[0].id === row.id
237+
if (isFirstItem) {
238+
return { rowspan: sameKeyItems.length, colspan: 1 }
261239
}
262240
return { rowspan: 0, colspan: 0 }
263241
}
@@ -285,7 +263,7 @@ const handleSelectionChange = async (val: any[]) => {
285263
await nextTick()
286264
287265
for (const dr of deselectedRows) {
288-
const sameGroupRows = pagedTableData.value.filter((r) => r.key === dr.key)
266+
const sameGroupRows = pagedGroups.value.filter((r) => r.key === dr.key)
289267
for (const r of sameGroupRows) {
290268
if (!selectedIds.has(r.id)) continue
291269
tableRef.value?.toggleRowSelection?.(r, false)
@@ -297,7 +275,7 @@ const handleSelectionChange = async (val: any[]) => {
297275
298276
// 以表格最终状态为准更新缓存(这里直接用传入 val 可能已过期)
299277
// 简化:重新从表格取 selection(Element Plus 有 store,没暴露就用 val\+补丁)
300-
multipleSelection.value = pagedTableData.value.filter((r) =>
278+
multipleSelection.value = pagedGroups.value.filter((r) =>
301279
tableRef.value?.getSelectionRows
302280
? tableRef.value.getSelectionRows().some((s: any) => s.id === r.id)
303281
: selectedIds.has(r.id),

0 commit comments

Comments
 (0)