diff --git a/src/views/equipmentRepair/repairApply/SelectToolDialog.vue b/src/views/equipmentRepair/repairApply/SelectToolDialog.vue index e47cbd2d..eae30a27 100644 --- a/src/views/equipmentRepair/repairApply/SelectToolDialog.vue +++ b/src/views/equipmentRepair/repairApply/SelectToolDialog.vue @@ -87,7 +87,7 @@ export default { typeName: null, code: null, }, - + externalSelectedRows:[], tableList: [], // selectedMap key 改为 `${id}-${type}` selectedMap: new Map(), @@ -95,19 +95,9 @@ export default { }, methods: { - // 在 SelectToolDialog.vue 中修改 openDialog 方法 openDialog(applyId, externalSelectedRows = []) { - this.applyId = applyId this.visible = true - - // 接收外部传入的已选数据,同步 selectedMap - this.selectedMap.clear() - externalSelectedRows.forEach(row => { - if (row.keyId) { - this.selectedMap.set(row.keyId, { ...row }) - } - }) - + this.externalSelectedRows=externalSelectedRows this.getList() }, @@ -140,31 +130,33 @@ export default { try { const res = await getToBeRepairList({ ...this.queryParams }) const rows = res.data || [] + const externalMap = new Map( + this.externalSelectedRows.map(item => [item.keyId, item]) + ) + // 初始化 selectedMap + this.selectedMap = this.selectedMap || new Map() - this.tableList = rows.map(item => { - const key = item.keyId - const selected = this.selectedMap.get(key) + // 遍历 res.data,将匹配的数据放入 selectedMap + rows.forEach(item => { + const external = externalMap.get(item.keyId) const isCodeManage = item.manageMode === "编码管理" + if (external) { + // ⭐ 如果 externalSelectedRows 中有相同 keyId + // 用 external 的 repairNum 覆盖 row 的 repairNum + item.repairNum = external.repairNum - return { - ...item, - keyId: key, - repairNum: selected ? selected.repairNum : (isCodeManage ? 1 : "") + // 将最新的 item 放入 selectedMap + this.selectedMap.set(item.keyId, { ...item }) + } else { + // ⭐ 如果 external 中没有该 keyId,使用默认规则 + item.repairNum = isCodeManage ? 1 : "" } }) - - // 清理 selectedMap 中不存在于当前 tableList 的数据 - const tableKeys = new Set(this.tableList.map(r => r.keyId)) - Array.from(this.selectedMap.keys()).forEach(key => { - if (!tableKeys.has(key)) { - this.selectedMap.delete(key) - } - }) - + console.log(12345,this.selectedMap) + this.tableList = rows this.$nextTick(() => { - this.restoreSelection() + if (this.restoreSelection) this.restoreSelection() }) - } finally { this.loading = false } @@ -174,19 +166,25 @@ export default { restoreSelection() { if (!this.$refs.multipleTable) return - // 先清除所有选择 + this.isRestoring = true + this.$refs.multipleTable.clearSelection() - // 然后重新选择 selectedMap 中的行 - this.tableList.forEach(row => { - if (this.selectedMap.has(row.keyId)) { - this.$nextTick(() => { + this.$nextTick(() => { + this.tableList.forEach(row => { + if (this.selectedMap.has(row.keyId)) { this.$refs.multipleTable.toggleRowSelection(row, true) - }) - } + } + }) + + this.$nextTick(() => { + this.isRestoring = false + }) }) }, + + handleQuery() { this.getList() }, @@ -197,19 +195,23 @@ export default { }, handleSelectionChange(selection) { + if (this.isRestoring) return + // 1. 新增选中的(selection 中有,但 selectedMap 没有) selection.forEach(row => { if (row.keyId && !this.selectedMap.has(row.keyId)) { this.selectedMap.set(row.keyId, { ...row }) } - }) - - this.tableList.forEach(row => { - if (row.keyId && !selection.some(s => s.keyId === row.keyId)) { - this.selectedMap.delete(row.keyId) - } }) + + // 2. 删除取消选中的(selectedMap 中有,但 selection 中没有) + for (const [keyId] of this.selectedMap) { + if (!selection.some(s => s.keyId === keyId)) { + this.selectedMap.delete(keyId) + } + } }, + handleConfirm() { if (this.selectedMap.size === 0) return this.$message.warning("请至少选择一条数据") @@ -222,12 +224,14 @@ export default { } this.$emit("confirm", [...this.selectedMap.values()]) + this.selectedMap.clear() this.visible = false }, // 在弹窗组件中修改 handleClose 方法 handleClose() { // 不要清空 selectedMap,保留选中状态以便下次打开时使用 + this.selectedMap.clear() this.visible = false } }