|
|
|
|
@ -44,7 +44,7 @@
|
|
|
|
|
style="width:300px"
|
|
|
|
|
default-expand-all
|
|
|
|
|
:default-checked-keys="checkArr"
|
|
|
|
|
:check-strictly="true"
|
|
|
|
|
:check-strictly="false"
|
|
|
|
|
:data="types"
|
|
|
|
|
show-checkbox
|
|
|
|
|
node-key="id"
|
|
|
|
|
@ -273,17 +273,50 @@ export default {
|
|
|
|
|
await this.getDepartment()
|
|
|
|
|
|
|
|
|
|
this.select.department = Number(this.$route.query.departmentId) || ''
|
|
|
|
|
const routeType = Number(this.$route.query.type) || ''
|
|
|
|
|
const routeType = this.$route.query.type || ''
|
|
|
|
|
if (routeType) {
|
|
|
|
|
this.select.type = routeType
|
|
|
|
|
// 根据 type ID 查找对应的名称
|
|
|
|
|
const typeItem = this.typesbefore.find(item => item.id === routeType)
|
|
|
|
|
// 支持多个 id,以逗号分隔
|
|
|
|
|
const typeIds = routeType.split(',').map(id => Number(id.trim())).filter(id => !isNaN(id))
|
|
|
|
|
if (typeIds.length > 0) {
|
|
|
|
|
// 收集所有需要选中的 id(包括父级和子级)
|
|
|
|
|
let allSelectedIds = []
|
|
|
|
|
const selectedNames = []
|
|
|
|
|
|
|
|
|
|
// 在树形结构中查找每个 id 对应的节点
|
|
|
|
|
typeIds.forEach(id => {
|
|
|
|
|
const node = this.findNodeById(this.types, id)
|
|
|
|
|
if (node) {
|
|
|
|
|
selectedNames.push(node.name)
|
|
|
|
|
// 如果节点有子节点,收集所有子节点的 id
|
|
|
|
|
if (node.children && node.children.length > 0) {
|
|
|
|
|
allSelectedIds.push(node.id) // 添加父级 id
|
|
|
|
|
const childrenIds = this.getAllChildrenIds(node)
|
|
|
|
|
allSelectedIds = allSelectedIds.concat(childrenIds)
|
|
|
|
|
} else {
|
|
|
|
|
// 叶子节点,直接添加
|
|
|
|
|
allSelectedIds.push(node.id)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 如果在树形结构中找不到,尝试在扁平数组中查找
|
|
|
|
|
const typeItem = this.typesbefore.find(item => item.id === id)
|
|
|
|
|
if (typeItem) {
|
|
|
|
|
this.select.typeName = typeItem.name
|
|
|
|
|
this.checkArr = [routeType]
|
|
|
|
|
selectedNames.push(typeItem.name)
|
|
|
|
|
allSelectedIds.push(id)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 去重
|
|
|
|
|
allSelectedIds = [...new Set(allSelectedIds)]
|
|
|
|
|
|
|
|
|
|
// 将所有 id 以逗号分隔赋值给 this.select.type
|
|
|
|
|
this.select.type = allSelectedIds.join(',')
|
|
|
|
|
this.select.typeName = selectedNames.join(', ')
|
|
|
|
|
this.checkArr = allSelectedIds
|
|
|
|
|
|
|
|
|
|
this.$nextTick(() => {
|
|
|
|
|
if (this.$refs.tree1) {
|
|
|
|
|
this.$refs.tree1.setCheckedKeys([routeType])
|
|
|
|
|
this.$refs.tree1.setCheckedKeys(allSelectedIds)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
@ -386,22 +419,69 @@ export default {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
// 在树形结构中查找指定 id 的节点
|
|
|
|
|
findNodeById(nodes, id) {
|
|
|
|
|
if (!nodes || !Array.isArray(nodes)) return null
|
|
|
|
|
for (const node of nodes) {
|
|
|
|
|
if (node.id === id) {
|
|
|
|
|
return node
|
|
|
|
|
}
|
|
|
|
|
if (node.children && node.children.length > 0) {
|
|
|
|
|
const found = this.findNodeById(node.children, id)
|
|
|
|
|
if (found) return found
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null
|
|
|
|
|
},
|
|
|
|
|
// 递归获取所有子节点的 id
|
|
|
|
|
getAllChildrenIds(node) {
|
|
|
|
|
let ids = []
|
|
|
|
|
if (node.children && node.children.length > 0) {
|
|
|
|
|
node.children.forEach(child => {
|
|
|
|
|
ids.push(child.id)
|
|
|
|
|
ids = ids.concat(this.getAllChildrenIds(child))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return ids
|
|
|
|
|
},
|
|
|
|
|
getSelectedNodes(data, node, ref) {
|
|
|
|
|
const ref_data = ref || 'tree1'
|
|
|
|
|
this.$refs[ref_data].setCheckedKeys([])
|
|
|
|
|
this.checkArr = []
|
|
|
|
|
if (data.children && data.children.length > 0) {
|
|
|
|
|
this.$Message.warning('当前节点不可选择')
|
|
|
|
|
this.select.type = ''
|
|
|
|
|
this.select.typeName = ''
|
|
|
|
|
this.$refs[ref_data].setCheckedKeys([])
|
|
|
|
|
return
|
|
|
|
|
// 获取所有选中的节点(当 check-strictly="false" 时,选择父级会自动选中所有子级)
|
|
|
|
|
const checkedNodes = this.$refs[ref_data].getCheckedNodes()
|
|
|
|
|
|
|
|
|
|
// 收集所有选中的 id
|
|
|
|
|
// 规则:如果直接选择了父级,则收集父级id + 所有子级id
|
|
|
|
|
// 如果只选择了子级,则只收集子级id,不包含父级id
|
|
|
|
|
let allSelectedIds = []
|
|
|
|
|
|
|
|
|
|
// 遍历所有选中的节点
|
|
|
|
|
checkedNodes.forEach(checkedNode => {
|
|
|
|
|
// 如果节点有子节点(是父级),说明用户直接选择了父级
|
|
|
|
|
if (checkedNode.children && checkedNode.children.length > 0) {
|
|
|
|
|
// 选择父级时:添加父级id + 所有子级id
|
|
|
|
|
allSelectedIds.push(checkedNode.id) // 添加父级 id
|
|
|
|
|
// 递归获取所有子节点的 id
|
|
|
|
|
const childrenIds = this.getAllChildrenIds(checkedNode)
|
|
|
|
|
allSelectedIds = allSelectedIds.concat(childrenIds)
|
|
|
|
|
} else {
|
|
|
|
|
// 叶子节点(子级),只添加该子级id,不包含父级
|
|
|
|
|
allSelectedIds.push(checkedNode.id)
|
|
|
|
|
}
|
|
|
|
|
this.$refs[ref_data].setCheckedKeys([]) // 删除所有选中节点
|
|
|
|
|
this.$refs[ref_data].setCheckedNodes([data]) // 选中已选中节点
|
|
|
|
|
this.select.type = data.id
|
|
|
|
|
this.select.typeName = data.name
|
|
|
|
|
this.checkArr = [data.id]
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 去重
|
|
|
|
|
allSelectedIds = [...new Set(allSelectedIds)]
|
|
|
|
|
|
|
|
|
|
// 更新选中状态
|
|
|
|
|
this.checkArr = allSelectedIds
|
|
|
|
|
|
|
|
|
|
// 将选中的 id 以逗号分隔赋值给 this.select.type
|
|
|
|
|
this.select.type = allSelectedIds.join(',')
|
|
|
|
|
|
|
|
|
|
// 更新显示的名称(只显示直接选中的节点名称,不包括自动选中的子级)
|
|
|
|
|
const selectedNames = checkedNodes.map(n => n.name).filter((name, index, self) => self.indexOf(name) === index)
|
|
|
|
|
this.select.typeName = selectedNames.join(', ')
|
|
|
|
|
|
|
|
|
|
this.$forceUpdate()
|
|
|
|
|
},
|
|
|
|
|
resetSelect() {
|
|
|
|
|
@ -519,9 +599,7 @@ export default {
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style scoped lang="scss">
|
|
|
|
|
::v-deep .el-checkbox__input .el-checkbox__inner {
|
|
|
|
|
border-radius: 50%;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
</style>
|
|
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
|
|