南通网站建设方案外包,网站做不做账,云服务器建设简易网站,建设部网站阮建昌公示2024.1.11今天我学习了如何对多个el-tree树进行相同节点的联动效果#xff0c;如图#xff1a; 这边有两棵树#xff0c;我们发现第一个树和第二个树之间会有重复的指标#xff0c;当我们选中第一个树的指标#xff0c;我们希望第二个树如果也有重复的指标也能进行勾选上如图 这边有两棵树我们发现第一个树和第二个树之间会有重复的指标当我们选中第一个树的指标我们希望第二个树如果也有重复的指标也能进行勾选上反之也是。
一、难点
1要让父节点变成半选状态
这个组件比较复杂的地方是要通过选中的子节点拿到另外一个树对应的父节点直接通过getHalfCheckedKeys是不行的因为你当前不是在另外一个树做操作获取不到当前的父节点所以只能通过递归子节点拿到对应的父节点。
2要拿到选中的所有子节点数据
setCheckedKeysdata,check的data是拿到当前选中的子节点数据如果当前子节点数据含有children数据我们也需要通过递归的方法拿到所有的子节点数据。
二、重点方法
setCheckedKeysdata,check//data为当前选中节点的数据 check为所有选中节点的数据
三、关键步骤如下
1第一个树选中的节点数据赋值给第二个树回显。
2第二个树选中的节点拼接第一个树选中的节点并过滤第二个树去掉的节点。
四、完整代码如下
templatedivel-treestyleheight: 30vh;overflow-y: scrollnode-keyidreftree1:default-expand-alltrue:propsdefaultProps:datatree_demo1checktree_check1show-checkboxspan slot-scope{ node, data }【{{ data.id }}】{{ data.label }}/span/el-treehr/el-treenode-keyidreftree2styleheight: 30vh;overflow-y: scroll:default-expand-alltrue:propsdefaultProps:datatree_demo2checktree_check2show-checkboxspan slot-scope{ node, data }【{{ data.id }}】{{ data.label }}/span/el-tree/div
/template
script
export default {data() {return {tree_demo1: [],tree_demo2: [],default_data: [],defaultProps: {label: label,children: children}}},created() {this.getList()},methods: {getList() {let demo [{children: [{children: [{id: 001,label: 指标一,parent_id: 100,},{id: 002,label: 指标二,parent_id: 100,},{id: 003,label: 指标三,parent_id: 100,},{id: 004,label: 指标四,parent_id: 100,},{id: 005,label: 指标五,parent_id: 100,},{id: 006,label: 指标六,parent_id: 100,},{id: 007,label: 指标七,parent_id: 100,},{id: 008,label: 指标八,parent_id: 100,},{id: 009,label: 指标九,parent_id: 100,},{id: 010,label: 指标十,parent_id: 100,},{id: 011,label: 指标十一,parent_id: 100,},{id: 012,label: 指标十二,parent_id: 100,},{id: 013,label: 指标十三,parent_id: 100,},{id: 014,label: 指标十四,parent_id: 100,},{id: 015,label: 指标十五,parent_id: 100,},{id: 016,label: 指标十六,parent_id: 100,},{id: 017,label: 指标十七,parent_id: 100,},{id: 018,label: 指标十八,parent_id: 100,},{id: 019,label: 指标十九,parent_id: 100,},{id: 020,label: 指标二十,parent_id: 100,},{id: 021,label: 指标二十一,parent_id: 100,},{id: 022,label: 指标二十二,parent_id: 100,},{id: 023,label: 指标二十三,parent_id: 100,},{id: 024,label: 指标二十四,parent_id: 100,},{id: 025,label: 指标二十五,parent_id: 100,}],id: 100,label: 指标分类1-1,parent_id: 1,},{children: [{id: 026,label: 指标二十六,parent_id: 101,},{id: 027,label: 指标二十七,parent_id: 101,},{id: 028,label: 指标二十八,parent_id: 101,},{id: 029,label: 指标二十九,parent_id: 101,},{id: 030,label: 指标三十,parent_id: 101,},{id: 031,label: 指标三十一,parent_id: 101,},{id: 032,label: 指标三十二,parent_id: 101,},{id: 033,label: 指标三十三,parent_id: 101,},{id: 034,label: 指标三十四,parent_id: 101,},{id: 035,label: 指标三十五,parent_id: 101,},{id: 036,label: 指标三十六,parent_id: 101,},{id: 037,label: 指标三十七,parent_id: 101,},{id: 038,label: 指标三十八,parent_id: 101,},{id: 039,label: 指标三十九,parent_id: 101,},{id: 040,label: 指标四十,parent_id: 101,}],id: 101,label: 指标分类1-2,parent_id: 1,},],id: 1,label: 指标分类一,parent_id: 0,status: 1},{children: [{id: 005,label: 指标五},{id: 010,label: 指标十},{id: 011,label: 指标十一},{id: 016,label: 指标十六},{id: 020,label: 指标二十},{id: 030,label: 指标三十},{id: 034,label: 指标三十四},{id: 033,label: 指标三十三},{id: 031,label: 指标三十一},{id: 021,label: 指标二十一},{id: 050,label: 指标五十},{id: 060,label: 指标六十},{id: 066,label: 指标六十六},{id: 068,label: 指标六十八},{id: 070,label: 指标七十},],id: 2,label: 指标分类二}]this.tree_demo1 [demo[0]]//第一棵树数据this.tree_demo2 [demo[1]]//第二棵树数据},// 第一棵树选中节点数据tree_check1(data, check) {this.component_check_data_method(data, check, tree2, tree_demo2)},// 第二棵树选中节点数据tree_check2(data, check) {this.component_check_data_method(data, check, tree1, tree_demo1)},//递归拿到选中所有的子节点数据findChildrenIds(data) {let all_ids [data.id]if (data.children data.children.length 0) {for (let child of data.children) {all_ids all_ids.concat(this.findChildrenIds(child));//判断是否含有children数据如果有就递归继续拼接}}return all_ids},// 递归拿到对应所有的父节点数据findParentIds(id, data) {const parentIds [];function findNode(node, parentId) {if (node.id id) {parentIds.push(parentId);return true;}if (node.children) {for (const child of node.children) {if (findNode(child, node.id)) {parentIds.push(parentId);return true;}}}return false;}for (const node of data) {if (findNode(node, null)) {break;}}return parentIds.reverse(); // 反转数组让根节点id在最前面},// 通用选中节点获取数据的方法component_check_data_method(data, check, other_tree_ref, other_tree_data) {let first_data this.$refs[other_tree_ref].getCheckedKeys()//获取另外一棵树的节点数据let all_nodes []for (const nodeId of this.findChildrenIds(data)) {const parentIds this.findParentIds(nodeId, this[other_tree_data])all_nodes.push(...parentIds)//拿到当前子节点对应的所有父节点}const filteredArr [...new Set(all_nodes.filter(item item ! null))]//去重和过滤nulllet select_all_data [...filteredArr, ...this.findChildrenIds(data)]//拼接所有父节点和所有选中的子节点first_data first_data.filter(item !select_all_data.includes(item));//如果包含当前子节点就过滤this.$refs[other_tree_ref].setCheckedKeys([...first_data, ...check.checkedKeys])}}
}
/script可以直接复制demo查看效果。大家如果有不懂的地方或是更好的方法可以分享到评论区谢谢 文章转载自: http://www.morning.kqylg.cn.gov.cn.kqylg.cn http://www.morning.ynrzf.cn.gov.cn.ynrzf.cn http://www.morning.dtnyl.cn.gov.cn.dtnyl.cn http://www.morning.cfjyr.cn.gov.cn.cfjyr.cn http://www.morning.rcjwl.cn.gov.cn.rcjwl.cn http://www.morning.ljjmr.cn.gov.cn.ljjmr.cn http://www.morning.qgmwt.cn.gov.cn.qgmwt.cn http://www.morning.gnjkn.cn.gov.cn.gnjkn.cn http://www.morning.bnbtp.cn.gov.cn.bnbtp.cn http://www.morning.fllfz.cn.gov.cn.fllfz.cn http://www.morning.kyctc.cn.gov.cn.kyctc.cn http://www.morning.gqbtw.cn.gov.cn.gqbtw.cn http://www.morning.rxydr.cn.gov.cn.rxydr.cn http://www.morning.hqbnx.cn.gov.cn.hqbnx.cn http://www.morning.smnxr.cn.gov.cn.smnxr.cn http://www.morning.mwzt.cn.gov.cn.mwzt.cn http://www.morning.ylxgw.cn.gov.cn.ylxgw.cn http://www.morning.ggmls.cn.gov.cn.ggmls.cn http://www.morning.gyfhk.cn.gov.cn.gyfhk.cn http://www.morning.xgjhy.cn.gov.cn.xgjhy.cn http://www.morning.jqsyp.cn.gov.cn.jqsyp.cn http://www.morning.mcgsq.cn.gov.cn.mcgsq.cn http://www.morning.jwfqq.cn.gov.cn.jwfqq.cn http://www.morning.bbgr.cn.gov.cn.bbgr.cn http://www.morning.0dirty.cn.gov.cn.0dirty.cn http://www.morning.ttryd.cn.gov.cn.ttryd.cn http://www.morning.rhjhy.cn.gov.cn.rhjhy.cn http://www.morning.kghss.cn.gov.cn.kghss.cn http://www.morning.pqfbk.cn.gov.cn.pqfbk.cn http://www.morning.kjyfq.cn.gov.cn.kjyfq.cn http://www.morning.tqsmg.cn.gov.cn.tqsmg.cn http://www.morning.fdrb.cn.gov.cn.fdrb.cn http://www.morning.jwncx.cn.gov.cn.jwncx.cn http://www.morning.rykmf.cn.gov.cn.rykmf.cn http://www.morning.fdfsh.cn.gov.cn.fdfsh.cn http://www.morning.tdmr.cn.gov.cn.tdmr.cn http://www.morning.jcxzq.cn.gov.cn.jcxzq.cn http://www.morning.nrchx.cn.gov.cn.nrchx.cn http://www.morning.hlhqs.cn.gov.cn.hlhqs.cn http://www.morning.kryn.cn.gov.cn.kryn.cn http://www.morning.bsrp.cn.gov.cn.bsrp.cn http://www.morning.hmsong.com.gov.cn.hmsong.com http://www.morning.pwmpn.cn.gov.cn.pwmpn.cn http://www.morning.fkgct.cn.gov.cn.fkgct.cn http://www.morning.sbrjj.cn.gov.cn.sbrjj.cn http://www.morning.pzdxg.cn.gov.cn.pzdxg.cn http://www.morning.dthyq.cn.gov.cn.dthyq.cn http://www.morning.fbjqq.cn.gov.cn.fbjqq.cn http://www.morning.ynrzf.cn.gov.cn.ynrzf.cn http://www.morning.chgmm.cn.gov.cn.chgmm.cn http://www.morning.kkqgf.cn.gov.cn.kkqgf.cn http://www.morning.wcgfy.cn.gov.cn.wcgfy.cn http://www.morning.lqtwb.cn.gov.cn.lqtwb.cn http://www.morning.jmmzt.cn.gov.cn.jmmzt.cn http://www.morning.fxygn.cn.gov.cn.fxygn.cn http://www.morning.bhqlj.cn.gov.cn.bhqlj.cn http://www.morning.bctr.cn.gov.cn.bctr.cn http://www.morning.skkln.cn.gov.cn.skkln.cn http://www.morning.kflbf.cn.gov.cn.kflbf.cn http://www.morning.lsssx.cn.gov.cn.lsssx.cn http://www.morning.hbtarq.com.gov.cn.hbtarq.com http://www.morning.xjqhh.cn.gov.cn.xjqhh.cn http://www.morning.fkffr.cn.gov.cn.fkffr.cn http://www.morning.wmmjw.cn.gov.cn.wmmjw.cn http://www.morning.kfhm.cn.gov.cn.kfhm.cn http://www.morning.ftmzy.cn.gov.cn.ftmzy.cn http://www.morning.muzishu.com.gov.cn.muzishu.com http://www.morning.ffmx.cn.gov.cn.ffmx.cn http://www.morning.wjhdn.cn.gov.cn.wjhdn.cn http://www.morning.nxbsq.cn.gov.cn.nxbsq.cn http://www.morning.grryh.cn.gov.cn.grryh.cn http://www.morning.zdmlt.cn.gov.cn.zdmlt.cn http://www.morning.tkzrh.cn.gov.cn.tkzrh.cn http://www.morning.rcww.cn.gov.cn.rcww.cn http://www.morning.pmnn.cn.gov.cn.pmnn.cn http://www.morning.blbys.cn.gov.cn.blbys.cn http://www.morning.rqrh.cn.gov.cn.rqrh.cn http://www.morning.dmzmy.cn.gov.cn.dmzmy.cn http://www.morning.sfqtf.cn.gov.cn.sfqtf.cn http://www.morning.wlddq.cn.gov.cn.wlddq.cn