阅读背景:

Js 代码递归实现树形数据与数组相互转换。

来源:互联网 

贴代码:

// Grid-》Tree 结构组装。
var tree = [];
this.setTreeData(table, tree, "");
//组装树形
     setTreeData = (source, list, pid) => {
        if (typeof (source) == "undefined") return;
        source.forEach((father) => {
            if (father.PID == pid) {
                list.push(father);
                if (!father.IsLowest) {
                    if (typeof (father.children) == "undefined") {
                        father.children = [];
                    }
                    father.children = this.setTreeData(source, father.children, father.ContractListDtlID);
                }
            }


//Tree -> 数组机构
//树形数据转换成grid,调用者自己决定children 属性删除与否 2019-
     
var list= [];
this.setGridDataFromTree(list, tree, "");

setGridDataFromTree= function(list,dataSource){
        if (!(Array.isArray(dataSource) && dataSource.length >0)) return ;            
        dataSource.forEach((father) => {
            // debugger;
            list.push(father);            
            if (typeof (father.children) == "undefined") {                
            } else {                
                this.setGridDataFromTree(list, father.children);
            }
        });
        // return;
    }
        })
        return list;
    }// Grid-》Tree 结构组装。
var tree = [];
this.



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: