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);

 

這樣就能獲取完整的整條數據鏈

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM