Vue.js 部门选择组件:实现上级部门下拉菜单显示父级和兄弟级
Vue.js 部门选择组件:实现上级部门下拉菜单显示父级和兄弟级
根据代码中的部分片段,可以看出'上级部门'的数据来源是filteredDeptList。在filterDeptList方法中,根据parentId进行了部门列表的过滤。
当前的问题是'上级部门'只显示了自己的父级,没有显示兄弟级。这是因为在过滤部门列表时,使用了deptPath.startsWith(parentDept.deptPath)来判断是否为子集部门,但这样的判断条件只允许显示父级和子集,而没有包含兄弟级。
为了解决这个问题,可以调整filterDeptList方法的逻辑,使用部门的deptPath进行判断。以下是修改后的代码:
const filterDeptList = (parentId) => {
if (!parentId) {
return deptList.value; // 如果 parentId 为空,显示完整的部门列表
} else {
const parentDept = deptList.value.find((dept) => dept.deptId === parentId);
if (parentDept) {
const parentDeptPath = parentDept.deptPath; // 获取父级部门的deptPath
return deptList.value.filter((dept) => {
if (dept.deptId === parentId) {
return true; // 显示父级部门
} else if (parentDeptPath && dept.deptPath) {
return !dept.deptPath.startsWith(parentDeptPath) && dept.deptPath !== parentDeptPath; // 显示兄弟级部门
} else {
return false; // 其他情况不显示
}
});
} else {
return deptList.value; // 如果找不到父级部门,显示完整的部门列表
}
}
};
以上修改后的代码会在过滤部门列表时显示父级部门和兄弟级部门,同时排除了自己和自己的子集。
原文地址: https://www.cveoy.top/t/topic/hNcH 著作权归作者所有。请勿转载和采集!