• 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);
    复制代码

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

  • 相关阅读:
    chrome开发者工具使用方法。
    模拟window的history对象
    浏览器后退刷新(通过浏览器按钮)
    日常口语十
    日常口语九
    日常口语八
    日常口语七
    日常口语五
    日常口语五
    日常口语四
  • 原文地址:https://www.cnblogs.com/qdwz/p/11015384.html
Copyright © 2020-2023  润新知