master
lion 9 months ago
parent 18ba345185
commit 86faa3be9b

@ -28,6 +28,7 @@
clearable clearable
placeholder="选择预算类型" placeholder="选择预算类型"
style="width: 130px" style="width: 130px"
popper-class="budget-type-tree-select"
@change="changeType" @change="changeType"
> >
<el-option disabled value="" style="height:150px;overflow: scroll;"> <el-option disabled value="" style="height:150px;overflow: scroll;">
@ -36,7 +37,7 @@
style="width:300px" style="width:300px"
default-expand-all default-expand-all
:default-checked-keys="checkArr" :default-checked-keys="checkArr"
:check-strictly="true" :check-strictly="false"
:data="types" :data="types"
show-checkbox show-checkbox
node-key="id" node-key="id"
@ -921,29 +922,85 @@ export default {
if (!e) { if (!e) {
this.select.type = '' this.select.type = ''
this.select.typeName = '' this.select.typeName = ''
this.checkArr = []
if (this.$refs.tree1) {
this.$refs.tree1.setCheckedKeys([])
}
} }
}, },
getSelectedNodes(data, node, ref) { // 递归获取所有子节点的 id(不包含节点本身)
const ref_data = ref || 'tree' getAllChildrenIds(node) {
this.$refs[ref_data].setCheckedKeys([]) let ids = []
this.checkArr = [] if (node.children && node.children.length > 0) {
if (data.children && data.children.length > 0) { node.children.forEach(child => {
this.$Message.warning('当前节点不可选择') ids.push(child.id) // 添加直接子节点 id
this.form.type = '' ids = ids.concat(this.getAllChildrenIds(child)) // 递归获取子节点的子节点
this.select.type = '' })
this.editorForm.type = ''
this.$refs[ref_data].setCheckedKeys([])
return
} }
this.$refs[ref_data].setCheckedKeys([]) // 删除所有选中节点 return ids
this.$refs[ref_data].setCheckedNodes([data]) // 选中已选中节点 },
if (this.isShowAdd) { getSelectedNodes(data, node, ref) {
this.form.type = data.id const ref_data = ref || 'tree1'
} else if (this.isShowEditor) {
this.editorForm.type = data.id // 如果是列表筛选模式(tree1),使用多选逻辑
if (ref_data === 'tree1') {
// 获取所有选中的节点(当 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)
}
})
// 去重
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(', ')
} else { } else {
this.select.type = data.id // 新增和编辑模式保持单选逻辑
this.select.typeName = data.name this.$refs[ref_data].setCheckedKeys([])
this.checkArr = []
if (data.children && data.children.length > 0) {
this.$Message.warning('当前节点不可选择')
this.form.type = ''
this.select.type = ''
this.editorForm.type = ''
this.$refs[ref_data].setCheckedKeys([])
return
}
this.$refs[ref_data].setCheckedKeys([]) // 删除所有选中节点
this.$refs[ref_data].setCheckedNodes([data]) // 选中已选中节点
if (this.isShowAdd) {
this.form.type = data.id
} else if (this.isShowEditor) {
this.editorForm.type = data.id
} else {
this.select.type = data.id
this.select.typeName = data.name
}
} }
console.log('checkarr', this.select.typeName, this.$refs[ref_data].getCheckedNodes()) console.log('checkarr', this.select.typeName, this.$refs[ref_data].getCheckedNodes())
this.$forceUpdate() this.$forceUpdate()
@ -1196,7 +1253,9 @@ export default {
} }
} }
</script> </script>
<!-- ::v-deep .el-checkbox__input .el-checkbox__inner {
border-radius: 50%;
} -->
<style lang="scss" scoped> <style lang="scss" scoped>
.xy-table-item-label { .xy-table-item-label {
width: 160px; width: 160px;
@ -1219,7 +1278,5 @@ export default {
z-index: 2; z-index: 2;
} }
} }
::v-deep .el-checkbox__input .el-checkbox__inner {
border-radius: 50%;
}
</style> </style>

@ -34,7 +34,7 @@
style="width:300px" style="width:300px"
default-expand-all default-expand-all
:default-checked-keys="checkArr" :default-checked-keys="checkArr"
:check-strictly="true" :check-strictly="false"
:data="types" :data="types"
show-checkbox show-checkbox
node-key="id" node-key="id"
@ -287,23 +287,56 @@ export default {
} }
} }
}, },
// 递归获取所有子节点的 id(不包含节点本身)
getAllChildrenIds(node) {
let ids = []
if (node.children && node.children.length > 0) {
node.children.forEach(child => {
ids.push(child.id) // 添加直接子节点 id
ids = ids.concat(this.getAllChildrenIds(child)) // 递归获取子节点的子节点
})
}
return ids
},
// 树形节点选择 // 树形节点选择
getSelectedNodes(data, node, ref) { getSelectedNodes(data, node, ref) {
const ref_data = ref || 'tree1' const ref_data = ref || 'tree1'
this.$refs[ref_data].setCheckedKeys([]) // 获取所有选中的节点(当 check-strictly="false" 时,选择父级会自动选中所有子级)
this.checkArr = [] const checkedNodes = this.$refs[ref_data].getCheckedNodes()
if (data.children && data.children.length > 0) {
this.$Message.warning('当前节点不可选择') // 收集所有选中的 id
this.select.type = '' // 规则:如果直接选择了父级,则收集父级id + 所有子级id
this.select.typeName = '' // 如果只选择了子级,则只收集子级id,不包含父级id
this.$refs[ref_data].setCheckedKeys([]) let allSelectedIds = []
return
} // 遍历所有选中的节点
this.$refs[ref_data].setCheckedKeys([]) // 删除所有选中节点 checkedNodes.forEach(checkedNode => {
this.$refs[ref_data].setCheckedNodes([data]) // 选中已选中节点 // 如果节点有子节点(是父级),说明用户直接选择了父级
this.select.type = data.id if (checkedNode.children && checkedNode.children.length > 0) {
this.select.typeName = data.name // 选择父级时:添加父级id + 所有子级id
this.checkArr = [data.id] allSelectedIds.push(checkedNode.id) // 添加父级 id
// 递归获取所有子节点的 id
const childrenIds = this.getAllChildrenIds(checkedNode)
allSelectedIds = allSelectedIds.concat(childrenIds)
} else {
// 叶子节点(子级),只添加该子级id,不包含父级
allSelectedIds.push(checkedNode.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() this.$forceUpdate()
}, },
// 获取预算类型 // 获取预算类型
@ -464,9 +497,7 @@ export default {
} }
} }
::v-deep .el-checkbox__input .el-checkbox__inner {
border-radius: 50%;
}
</style> </style>
<style lang="scss"> <style lang="scss">

@ -44,7 +44,7 @@
style="width:300px" style="width:300px"
default-expand-all default-expand-all
:default-checked-keys="checkArr" :default-checked-keys="checkArr"
:check-strictly="true" :check-strictly="false"
:data="types" :data="types"
show-checkbox show-checkbox
node-key="id" node-key="id"
@ -273,17 +273,50 @@ export default {
await this.getDepartment() await this.getDepartment()
this.select.department = Number(this.$route.query.departmentId) || '' this.select.department = Number(this.$route.query.departmentId) || ''
const routeType = Number(this.$route.query.type) || '' const routeType = this.$route.query.type || ''
if (routeType) { if (routeType) {
this.select.type = routeType // 支持多个 id,以逗号分隔
// 根据 type ID 查找对应的名称 const typeIds = routeType.split(',').map(id => Number(id.trim())).filter(id => !isNaN(id))
const typeItem = this.typesbefore.find(item => item.id === routeType) if (typeIds.length > 0) {
if (typeItem) { // 收集所有需要选中的 id(包括父级和子级)
this.select.typeName = typeItem.name let allSelectedIds = []
this.checkArr = [routeType] 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) {
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(() => { this.$nextTick(() => {
if (this.$refs.tree1) { 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) { getSelectedNodes(data, node, ref) {
const ref_data = ref || 'tree1' const ref_data = ref || 'tree1'
this.$refs[ref_data].setCheckedKeys([]) // 获取所有选中的节点(当 check-strictly="false" 时,选择父级会自动选中所有子级)
this.checkArr = [] const checkedNodes = this.$refs[ref_data].getCheckedNodes()
if (data.children && data.children.length > 0) {
this.$Message.warning('当前节点不可选择') // 收集所有选中的 id
this.select.type = '' // 规则:如果直接选择了父级,则收集父级id + 所有子级id
this.select.typeName = '' // 如果只选择了子级,则只收集子级id,不包含父级id
this.$refs[ref_data].setCheckedKeys([]) let allSelectedIds = []
return
} // 遍历所有选中的节点
this.$refs[ref_data].setCheckedKeys([]) // 删除所有选中节点 checkedNodes.forEach(checkedNode => {
this.$refs[ref_data].setCheckedNodes([data]) // 选中已选中节点 // 如果节点有子节点(是父级),说明用户直接选择了父级
this.select.type = data.id if (checkedNode.children && checkedNode.children.length > 0) {
this.select.typeName = data.name // 选择父级时:添加父级id + 所有子级id
this.checkArr = [data.id] allSelectedIds.push(checkedNode.id) // 添加父级 id
// 递归获取所有子节点的 id
const childrenIds = this.getAllChildrenIds(checkedNode)
allSelectedIds = allSelectedIds.concat(childrenIds)
} else {
// 叶子节点(子级),只添加该子级id,不包含父级
allSelectedIds.push(checkedNode.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() this.$forceUpdate()
}, },
resetSelect() { resetSelect() {
@ -519,9 +599,7 @@ export default {
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">
::v-deep .el-checkbox__input .el-checkbox__inner {
border-radius: 50%;
}
</style> </style>
<style lang="scss" scoped> <style lang="scss" scoped>

Loading…
Cancel
Save