zoukankan      html  css  js  c++  java
  • iview tree 获取选中子节点的整条数据链

    这样子获取到数据是,checked等于true的,获取不到他的父级,父级的父级

    解决办法代码如下:

     //需要有一个唯一ID

            //======================================
            //扩展remove方法
            Array.prototype.remove = function (val) {
              let index = this.indexOf(val);
              if (index > -1) {
                this.splice(index, 1);
              }
            };
            //======================================
            //获取整条数据链
            function getParent(array, childs, ids) {
              for (let i = 0; i < array.length; i++) {
                let item = array[i];
                if (Number(item.id) === Number(ids)) {
                  childs.push(item);
                  return childs;
                }
                if (item.children && item.children.length > 0) {
                  childs.push(item);
                  let rs = getParent(item.children, childs, ids);
                  if (rs) {
                    return rs;
                  }
                  else {
                    childs.remove(item);
                  }
                }
              }
              return false;
            }
    
            //获取所有选中节点
            let params = this.$refs.tree.getCheckedNodes();
            //所有数据
            let allData = ['所有数据'];
            //循环执行所有选中的节点链,放到arr1数组里
            let arr1 = [];
            for (let i = 0; i < params.length; i++) {
              //单条数据链
              let aData = getParent(allData, [], params[i].id);//方法入口在这里
              for (let y = 0; y < aData.length; y++) {
                //拆分成单个json数组放到arr1里
                arr1.push(aData[y]);
              }
            }
    
            //arr1去重 es6的set方法
            function dedupe(array) {
              return Array.from(new Set(array));
            }
    
            arr1 = dedupe(arr1);

    这样就能获取完整的整条数据链

  • 相关阅读:
    POJ 3126 Prime Path
    POJ 2429 GCD & LCM Inverse
    POJ 2395 Out of Hay
    【Codeforces 105D】 Bag of mice
    【POJ 3071】 Football
    【POJ 2096】 Collecting Bugs
    【CQOI 2009】 余数之和
    【Codeforces 258E】 Devu and Flowers
    【SDOI 2010】 古代猪文
    【BZOJ 2982】 combination
  • 原文地址:https://www.cnblogs.com/xinchenhui/p/9504393.html
Copyright © 2011-2022 走看看