|
@@ -207,20 +207,36 @@ export default {
|
|
|
immediate: true,
|
|
|
handler(newData) {
|
|
|
this.processRawData(newData);
|
|
|
- this.updateCurrentSelectedNodesFromKeys(this.value, true);
|
|
|
+ // 过滤掉父节点,只保留子节点
|
|
|
+ const leafOnlyKeys = this.value.filter(key => {
|
|
|
+ const node = this.nodeMap[key];
|
|
|
+ return node && !node.isParent;
|
|
|
+ });
|
|
|
+ this.updateCurrentSelectedNodesFromKeys(leafOnlyKeys, true);
|
|
|
}
|
|
|
},
|
|
|
value: {
|
|
|
handler(newVal) {
|
|
|
- const valStr = JSON.stringify(newVal.sort());
|
|
|
- const currentValStr = JSON.stringify(this.currentSelectedNodes.map(n => n[this.nodeKey]).sort());
|
|
|
+ // 过滤掉父节点,只保留子节点
|
|
|
+ const leafOnlyKeys = newVal.filter(key => {
|
|
|
+ const node = this.nodeMap[key];
|
|
|
+ return node && !node.isParent;
|
|
|
+ });
|
|
|
|
|
|
- if (valStr !== currentValStr) {
|
|
|
- this.tempCheckedKeys = [...newVal];
|
|
|
- this.updateCurrentSelectedNodesFromKeys(newVal, true);
|
|
|
- if (this.$refs.taskTree) {
|
|
|
- this.$refs.taskTree.setCheckedKeys([...newVal]);
|
|
|
- }
|
|
|
+ const validKeysFromNewVal = leafOnlyKeys.filter(key => this.nodeMap[key] !== undefined);
|
|
|
+ const sortedNewValStr = JSON.stringify([...leafOnlyKeys].sort());
|
|
|
+ const sortedValidKeysFromNewValStr = JSON.stringify([...validKeysFromNewVal].sort());
|
|
|
+ const currentInternalNodeKeys = this.currentSelectedNodes.map(n => n[this.nodeKey]);
|
|
|
+ const sortedCurrentInternalNodeKeysStr = JSON.stringify([...currentInternalNodeKeys].sort());
|
|
|
+
|
|
|
+ if (sortedNewValStr !== sortedValidKeysFromNewValStr) {
|
|
|
+ this.$emit('input', [...validKeysFromNewVal]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (sortedNewValStr !== sortedCurrentInternalNodeKeysStr) {
|
|
|
+ this.tempCheckedKeys = [...leafOnlyKeys];
|
|
|
+ this.updateCurrentSelectedNodesFromKeys(validKeysFromNewVal, true);
|
|
|
}
|
|
|
},
|
|
|
deep: true
|
|
@@ -247,7 +263,12 @@ export default {
|
|
|
}
|
|
|
},
|
|
|
mounted() {
|
|
|
- this.updateCurrentSelectedNodesFromKeys(this.value, true);
|
|
|
+ // 过滤掉父节点,只保留子节点
|
|
|
+ const leafOnlyKeys = this.value.filter(key => {
|
|
|
+ const node = this.nodeMap[key];
|
|
|
+ return node && !node.isParent;
|
|
|
+ });
|
|
|
+ this.updateCurrentSelectedNodesFromKeys(leafOnlyKeys, true);
|
|
|
},
|
|
|
methods: {
|
|
|
processRawData(data) {
|
|
@@ -270,6 +291,7 @@ export default {
|
|
|
[this.treeProps.label]: `${detail.qwUserId || '未命名子任务'}`,
|
|
|
isParent: false,
|
|
|
[this.treeProps.isLeaf]: true,
|
|
|
+ disabled: false, // 子节点可以选择
|
|
|
originalData: { ...detail, parentTaskId: mainTask.taskId }
|
|
|
}));
|
|
|
|
|
@@ -278,6 +300,7 @@ export default {
|
|
|
[this.treeProps.label]: mainTask.taskName,
|
|
|
isParent: true,
|
|
|
[this.treeProps.isLeaf]: children.length === 0,
|
|
|
+ disabled: true, // 父节点不可选择
|
|
|
[this.treeProps.children]: children,
|
|
|
originalData: { taskId: mainTask.taskId, taskName: mainTask.taskName }
|
|
|
};
|
|
@@ -286,18 +309,25 @@ export default {
|
|
|
});
|
|
|
this.nodeMap = newMap;
|
|
|
},
|
|
|
- updateCurrentSelectedNodesFromKeys(keys, isConfirmation = false) {
|
|
|
+ updateCurrentSelectedNodesFromKeys(keys, updateTreeVisualState = false) {
|
|
|
const newSelectedNodes = [];
|
|
|
- if (keys && keys.length > 0) {
|
|
|
+ const actualFoundKeys = [];
|
|
|
+
|
|
|
+ if (keys && Array.isArray(keys)) {
|
|
|
keys.forEach(key => {
|
|
|
- if (this.nodeMap[key]) {
|
|
|
- newSelectedNodes.push(this.nodeMap[key]);
|
|
|
+ const node = this.nodeMap[key];
|
|
|
+ // 只添加子节点到选中列表
|
|
|
+ if (node && !node.isParent) {
|
|
|
+ newSelectedNodes.push(node);
|
|
|
+ actualFoundKeys.push(key);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
this.currentSelectedNodes = newSelectedNodes;
|
|
|
- if(isConfirmation && this.$refs.taskTree) {
|
|
|
- this.$refs.taskTree.setCheckedKeys(keys);
|
|
|
+
|
|
|
+ if (updateTreeVisualState && this.$refs.taskTree) {
|
|
|
+ this.$refs.taskTree.setCheckedKeys([...actualFoundKeys]);
|
|
|
}
|
|
|
},
|
|
|
filterNode(value, data) {
|
|
@@ -305,6 +335,13 @@ export default {
|
|
|
return data[this.treeProps.label].toLowerCase().indexOf(value.toLowerCase()) !== -1;
|
|
|
},
|
|
|
handleTreeCheck(nodeData, checkStatus) {
|
|
|
+ // 如果是父节点,不允许选择
|
|
|
+ if (nodeData.isParent) {
|
|
|
+ // 恢复之前的选中状态
|
|
|
+ this.$refs.taskTree.setCheckedKeys(this.tempCheckedKeys);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
if (!this.multiple) {
|
|
|
const isNodeChecked = checkStatus.checkedKeys.includes(nodeData[this.nodeKey]);
|
|
|
if (isNodeChecked) {
|
|
@@ -314,7 +351,14 @@ export default {
|
|
|
this.tempCheckedKeys = [];
|
|
|
}
|
|
|
} else {
|
|
|
- this.tempCheckedKeys = checkStatus.checkedKeys;
|
|
|
+ // 只保留子节点的选中状态
|
|
|
+ const leafKeys = checkStatus.checkedKeys.filter(key => {
|
|
|
+ const node = this.nodeMap[key];
|
|
|
+ return node && !node.isParent;
|
|
|
+ });
|
|
|
+ this.tempCheckedKeys = leafKeys;
|
|
|
+ // 更新树的选中状态,确保只显示子节点被选中
|
|
|
+ this.$refs.taskTree.setCheckedKeys(leafKeys);
|
|
|
}
|
|
|
},
|
|
|
handleConfirm() {
|
|
@@ -322,12 +366,19 @@ export default {
|
|
|
let finalSelectedNodes = [];
|
|
|
|
|
|
if (this.$refs.taskTree) {
|
|
|
- const leafOnlyForSelection = this.multiple && !this.checkStrictly && this.returnLeafOnly;
|
|
|
- finalSelectedNodes = this.$refs.taskTree.getCheckedNodes(leafOnlyForSelection);
|
|
|
+ // 获取所有选中的节点
|
|
|
+ const allCheckedNodes = this.$refs.taskTree.getCheckedNodes();
|
|
|
+ // 只保留子节点(叶子节点)
|
|
|
+ finalSelectedNodes = allCheckedNodes.filter(node => !node.isParent);
|
|
|
finalSelectedKeys = finalSelectedNodes.map(node => node[this.nodeKey]);
|
|
|
|
|
|
if(!this.multiple) {
|
|
|
- finalSelectedKeys = this.tempCheckedKeys;
|
|
|
+ // 单选模式下,确保只选择子节点
|
|
|
+ const leafKeys = this.tempCheckedKeys.filter(key => {
|
|
|
+ const node = this.nodeMap[key];
|
|
|
+ return node && !node.isParent;
|
|
|
+ });
|
|
|
+ finalSelectedKeys = leafKeys;
|
|
|
finalSelectedNodes = finalSelectedKeys.map(key => this.nodeMap[key]).filter(Boolean);
|
|
|
}
|
|
|
}
|