Js code is recursive tree data array conversion.

Paste the code:

// 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;
    }

 React above code development project, to use the content.
Note that, Gird Tree structure with the conversion is assigned a reference. That changed after the gird or treeData value will affect change.
You do not need it, then transferred after a deep copy.
Benefits shallow copy is to use a reference characteristic value after storage interface change treeData gridData is possible to reduce the TreeData - "GridData operation.
Of course, the control itself needs additional such judgments, to do the display screen refresh.

 shouldComponentUpdate(newProps, newState) {
        if (newProps.tableData !== this.props.tableData 
            || JSON.stringify(newProps.tableData) !== JSON.stringify(this.props.tableData)) {
            this.loadData(newProps);
     
        }
        // debugger;
        return true;
    }

Guess you like

Origin www.cnblogs.com/hijushen/p/11103995.html