|
29 | 29 | /> |
30 | 30 | </div> |
31 | 31 | <el-table |
| 32 | + ref="tableRef" |
32 | 33 | :data="pagedTableData" |
33 | 34 | :span-method="spanMethod" |
34 | 35 | v-loading="loading" |
|
130 | 131 | </template> |
131 | 132 |
|
132 | 133 | <script setup lang="ts"> |
133 | | -import { computed, ref } from 'vue' |
| 134 | +import { computed, ref, nextTick } from 'vue' |
134 | 135 | import { useRoute } from 'vue-router' |
135 | 136 | import { loadSharedApi } from '@/utils/dynamics-api/shared-api.ts' |
136 | 137 | import CreateTagDialog from './CreateTagDialog.vue' |
@@ -235,8 +236,42 @@ const spanMethod = ({ row, columnIndex }: any) => { |
235 | 236 | } |
236 | 237 |
|
237 | 238 | const multipleSelection = ref<any[]>([]) |
238 | | -const handleSelectionChange = (val: any[]) => { |
239 | | - multipleSelection.value = val |
| 239 | +const tableRef = ref<any>(null) |
| 240 | +const syncingSelection = ref(false) |
| 241 | +
|
| 242 | +const handleSelectionChange = async (val: any[]) => { |
| 243 | + if (syncingSelection.value) return |
| 244 | +
|
| 245 | + // 当前已选中的 id 集合(用于判断哪些行刚刚被取消) |
| 246 | + const selectedIds = new Set(val.map((r) => r.id)) |
| 247 | +
|
| 248 | + // 找出“刚被取消选中的行” |
| 249 | + const deselectedRows = multipleSelection.value.filter((r) => !selectedIds.has(r.id)) |
| 250 | + if (deselectedRows.length === 0) { |
| 251 | + multipleSelection.value = val |
| 252 | + return |
| 253 | + } |
| 254 | +
|
| 255 | + // 取消选中时:把同 key 分组里其它行也一并取消 |
| 256 | + syncingSelection.value = true |
| 257 | + await nextTick() |
| 258 | +
|
| 259 | + for (const dr of deselectedRows) { |
| 260 | + const sameGroupRows = pagedTableData.value.filter((r) => r.key === dr.key) |
| 261 | + for (const r of sameGroupRows) { |
| 262 | + if (!selectedIds.has(r.id)) continue |
| 263 | + tableRef.value?.toggleRowSelection?.(r, false) |
| 264 | + } |
| 265 | + } |
| 266 | +
|
| 267 | + await nextTick() |
| 268 | + syncingSelection.value = false |
| 269 | +
|
| 270 | + // 以表格最终状态为准更新缓存(这里直接用传入 val 可能已过期) |
| 271 | + // 简化:重新从表格取 selection(Element Plus 有 store,没暴露就用 val\+补丁) |
| 272 | + multipleSelection.value = pagedTableData.value.filter((r) => |
| 273 | + tableRef.value?.getSelectionRows ? tableRef.value.getSelectionRows().some((s: any) => s.id === r.id) : selectedIds.has(r.id) |
| 274 | + ) |
240 | 275 | } |
241 | 276 |
|
242 | 277 | const createTagDialogRef = ref() |
|
0 commit comments