vue element-ui tree,筛选出所有父节id回显

遇到一个后台只返回一个数节点的id,需要回显选择树节点,搜索了很多方法,所以在这里记录一下使用方法,
筛选的树结构 data

 "data":[
        {
            "id":10,
            "pid":0,
            "level":1,
            "fullName":null,
            "cateName":"环评类",
            "sort":1,
            "pic":"",
            "isShow":1,
            "children":[
                {
                    "id":1001,
                    "pid":10,
                    "level":2,
                    "fullName":null,
                    "cateName":"安评技术",
                    "sort":1,
                    "pic":"",
                    "isShow":1,
                    "children":[
                        {
                            "id":10001,
                            "pid":1001,
                            "level":3,
                            "fullName":null,
                            "cateName":"一级消防",
                            "sort":1,
                            "pic":"",
                            "isShow":1,
                            "children":[

                            ]
                        },
                        {
                            "id":10002,
                            "pid":1001,
                            "level":3,
                            "fullName":null,
                            "cateName":"一级消防市政",
                            "sort":1,
                            "pic":"",
                            "isShow":1,
                            "children":[

                            ]
                        },
                        {
                            "id":10003,
                            "pid":1001,
                            "level":3,
                            "fullName":null,
                            "cateName":"电气工程师",
                            "sort":1,
                            "pic":"",
                            "isShow":1,
                            "children":[

                            ]
                        },
                        {
                            "id":10004,
                            "pid":1001,
                            "level":3,
                            "fullName":null,
                            "cateName":"注册消防",
                            "sort":1,
                            "pic":"",
                            "isShow":1,
                            "children":[

                            ]
                        },
                        {
                            "id":10005,
                            "pid":1001,
                            "level":3,
                            "fullName":null,
                            "cateName":"评估师",
                            "sort":1,
                            "pic":"",
                            "isShow":1,
                            "children":[

                            ]
                        },
                        {
                            "id":10006,
                            "pid":1001,
                            "level":3,
                            "fullName":null,
                            "cateName":"拍卖师",
                            "sort":1,
                            "pic":"",
                            "isShow":1,
                            "children":[

                            ]
                        },
                        {
                            "id":10007,
                            "pid":1001,
                            "level":3,
                            "fullName":null,
                            "cateName":"二建注册消防",
                            "sort":1,
                            "pic":"",
                            "isShow":1,
                            "children":[

                            ]
                        },
                        {
                            "id":10008,
                            "pid":1001,
                            "level":3,
                            "fullName":null,
                            "cateName":"二建注册公司车",
                            "sort":1,
                            "pic":"",
                            "isShow":1,
                            "children":[

                            ]
                        },
                        {
                            "id":10009,
                            "pid":1001,
                            "level":3,
                            "fullName":null,
                            "cateName":"一级消防工程师",
                            "sort":1,
                            "pic":"",
                            "isShow":1,
                            "children":[

                            ]
                        },
                        {
                            "id":10010,
                            "pid":1001,
                            "level":3,
                            "fullName":null,
                            "cateName":"测试1",
                            "sort":1,
                            "pic":"",
                            "isShow":1,
                            "children":[

                            ]
                        }
                    ]
                },
                {
                    "id":1002,
                    "pid":10,
                    "level":2,
                    "fullName":null,
                    "cateName":"环评",
                    "sort":1,
                    "pic":"",
                    "isShow":1,
                    "children":[
                        {
                            "id":10011,
                            "pid":1002,
                            "level":3,
                            "fullName":null,
                            "cateName":"测试测",
                            "sort":1,
                            "pic":"",
                            "isShow":1,
                            "children":[

                            ]
                        },
                        {
                            "id":10012,
                            "pid":1002,
                            "level":3,
                            "fullName":null,
                            "cateName":"电气工程师",
                            "sort":1,
                            "pic":"",
                            "isShow":1,
                            "children":[

                            ]
                        },
                        {
                            "id":10013,
                            "pid":1002,
                            "level":3,
                            "fullName":null,
                            "cateName":"测试测注册消防",
                            "sort":1,
                            "pic":"",
                            "isShow":1,
                            "children":[

                            ]
                        },
                        {
                            "id":10014,
                            "pid":1002,
                            "level":3,
                            "fullName":null,
                            "cateName":"评估师",
                            "sort":1,
                            "pic":"",
                            "isShow":1,
                            "children":[

                            ]
                        },
                        {
                            "id":10015,
                            "pid":1002,
                            "level":3,
                            "fullName":null,
                            "cateName":"测试测拍卖师",
                            "sort":1,
                            "pic":"",
                            "isShow":1,
                            "children":[

                            ]
                        },
                        {
                            "id":10016,
                            "pid":1002,
                            "level":3,
                            "fullName":null,
                            "cateName":"注册消防",
                            "sort":1,
                            "pic":"",
                            "isShow":1,
                            "children":[

                            ]
                        },
                        {
                            "id":10017,
                            "pid":1002,
                            "level":3,
                            "fullName":null,
                            "cateName":"二建注册",
                            "sort":1,
                            "pic":"",
                            "isShow":1,
                            "children":[

                            ]
                        },
                        {
                            "id":10018,
                            "pid":1002,
                            "level":3,
                            "fullName":null,
                            "cateName":"注册消防",
                            "sort":1,
                            "pic":"",
                            "isShow":1,
                            "children":[

                            ]
                        },
                        {
                            "id":10019,
                            "pid":1002,
                            "level":3,
                            "fullName":null,
                            "cateName":"二建注册",
                            "sort":1,
                            "pic":"",
                            "isShow":1,
                            "children":[

                            ]
                        }
                    ]
                },
                {
                    "id":1003,
                    "pid":10,
                    "level":2,
                    "fullName":null,
                    "cateName":"环评工程师/技术员",
                    "sort":1,
                    "pic":"",
                    "isShow":1,
                    "children":[

                    ]
                },
                {
                    "id":1004,
                    "pid":10,
                    "level":2,
                    "fullName":null,
                    "cateName":"环评报告编写",
                    "sort":1,
                    "pic":"",
                    "isShow":1,
                    "children":[

                    ]
                },
                {
                    "id":1005,
                    "pid":10,
                    "level":2,
                    "fullName":null,
                    "cateName":"环评主管",
                    "sort":1,
                    "pic":"",
                    "isShow":1,
                    "children":[

                    ]
                },
                {
                    "id":1006,
                    "pid":10,
                    "level":2,
                    "fullName":null,
                    "cateName":"环评咨询工程师/咨询员",
                    "sort":1,
                    "pic":"",
                    "isShow":1,
                    "children":[

                    ]
                },
                {
                    "id":1007,
                    "pid":10,
                    "level":2,
                    "fullName":null,
                    "cateName":"环境应急预案编制员",
                    "sort":1,
                    "pic":"",
                    "isShow":1,
                    "children":[

                    ]
                },
                {
                    "id":1008,
                    "pid":10,
                    "level":2,
                    "fullName":null,
                    "cateName":"清洁生产审核",
                    "sort":1,
                    "pic":"",
                    "isShow":1,
                    "children":[

                    ]
                }
            ]
        },
        {
            "id":20,
            "pid":0,
            "level":1,
            "fullName":null,
            "cateName":"经营管理",
            "sort":1,
            "pic":"",
            "isShow":1,
            "children":[
                {
                    "id":2001,
                    "pid":20,
                    "level":2,
                    "fullName":null,
                    "cateName":"总工程师/副总工程师",
                    "sort":1,
                    "pic":"",
                    "isShow":1,
                    "children":[

                    ]
                },
                {
                    "id":2002,
                    "pid":20,
                    "level":2,
                    "fullName":null,
                    "cateName":"分公司/办事处总经理",
                    "sort":1,
                    "pic":"",
                    "isShow":1,
                    "children":[

                    ]
                },
                {
                    "id":2003,
                    "pid":20,
                    "level":2,
                    "fullName":null,
                    "cateName":"CEO/总裁/总经理",
                    "sort":1,
                    "pic":"",
                    "isShow":1,
                    "children":[

                    ]
                },
                {
                    "id":2004,
                    "pid":20,
                    "level":2,
                    "fullName":null,
                    "cateName":"COO/副总裁/副总经理",
                    "sort":1,
                    "pic":"",
                    "isShow":1,
                    "children":[

                    ]
                },
                {
                    "id":2005,
                    "pid":20,
                    "level":2,
                    "fullName":null,
                    "cateName":"总监",
                    "sort":1,
                    "pic":"",
                    "isShow":1,
                    "children":[

                    ]
                },
                {
                    "id":2006,
                    "pid":20,
                    "level":2,
                    "fullName":null,
                    "cateName":"项目总监/产品经理",
                    "sort":1,
                    "pic":"",
                    "isShow":1,
                    "children":[

                    ]
                },
                {
                    "id":2007,
                    "pid":20,
                    "level":2,
                    "fullName":null,
                    "cateName":"合伙人",
                    "sort":1,
                    "pic":"",
                    "isShow":1,
                    "children":[

                    ]
                }
            ]
        },
    ],

在methods中写上方法

// 获取分类id渲染分类
    getCidList(val, cateId) {
      let cid_list = [];
      val.forEach((item, index) => {
        if (item.id == cateId) {
          // let obj = {
          //   id: item.id,
          //   cateName: item.cateName,
          // };
          cid_list = [item.id];
          // cid_list = obj
          return false;
        } else {
          if (item.children) {
            // let obj = {
            //   id: item.id,
            //   cateName: item.cateName,
            // };
            let newCid_list = [item.id];
            let list = nodefun(item.children, cateId, newCid_list);
            if (list) {
              cid_list = list;
            }
          }
        }
      });
      // 递归函数
      function nodefun(newVal, newId, newCid_list) {
        let flag = false;
        newVal.forEach((j) => {
          // console.log(j)
          if (j.id == newId) {
            // let obj = {
            //   id: j.id,
            //   name: j.name,
            // };
            newCid_list.push(j.id);
            flag = true;
          } else {
            if (j.children) {
              let cid_list = JSON.parse(JSON.stringify(newCid_list));
              // let obj = {
              //   id: j.id,
              //   name: j.name,
              // };
              cid_list.push(j.id);
              let list = nodefun(j.children, newId, cid_list);
              if (list) {
                newCid_list = list;
                flag = true;
              }
            }
          }
        });
        if (flag) {
          return newCid_list;
        }
      }
      return cid_list;
    },
    // 调用方法
    onScreen () {
    	let cateId = 2005;
		let cateIdList = this.getCidList(this.data, cateId);
		console.log(cateIdList );
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值