隐藏

js将表数据递归成树形菜单

发布:2024/12/2 23:08:54作者:管理员 来源:本站 浏览次数:142

const { object } = require("prop-types");

let json='[{"TreeCheckBox":{"Id":1,"id2":16,"pid":0,"name":"系统管理","jingle":"system","href":"javascript:;","level":1,"sort":2,"icon":"icon-cog","status":1,"create_time":1531020243,"update_time":1531020243}},{"TreeCheckBox":{"Id":2,"id2":25,"pid":16,"name":"管理员","jingle":"admin","href":"/administrators/index.html","level":2,"sort":1,"status":1,"create_time":1531020243,"update_time":1531020243}},{"TreeCheckBox":{"Id":3,"id2":28,"pid":25,"name":"新增","jingle":"add admin","href":"/administrators/add.html","level":3,"sort":1,"status":1,"create_time":1531020243,"update_time":1531020243}},{"TreeCheckBox":{"Id":4,"id2":29,"pid":25,"name":"编辑","jingle":"edit admin","href":"/administrators/edit.html","level":3,"sort":1,"status":1,"create_time":1531020243,"update_time":1531020243}},{"TreeCheckBox":{"Id":5,"id2":30,"pid":25,"name":"删除","jingle":"delete admin","href":"/administrators/delete.html","level":3,"sort":1,"status":1,"create_time":1531020243,"update_time":1531020243}},{"TreeCheckBox":{"Id":6,"id2":27,"pid":16,"name":"网站设置","jingle":"web site","href":"/web_site_setting/index.html","level":2,"sort":1,"status":1,"create_time":1531020243,"update_time":1531020243}},{"TreeCheckBox":{"Id":7,"id2":31,"pid":27,"name":"编辑","jingle":"edit site","href":"/web_site_setting/edit.html","level":3,"sort":1,"status":1,"create_time":1531020243,"update_time":1531020243}}]';

//console.log(JSON.parse(json))


let data=JSON.parse(json);
let _data=[];
data.forEach(a=>{
    _data.push(a.TreeCheckBox);
})
//console.log(_data)




let defaultData=[];
defaultData=getTreeJson(0);


function getTreeJson(pid){
    let _defaultData=[];
    _defaultData=_data.filter(a=>{
        return a.pid===pid
    });
    
    //console.log(_defaultData)
    
    let _jj=[];
    _defaultData.forEach(a=>{
        var _o=new Object();
        _o.id2=a.id2;
        _o.name=a.name;
        _o.jingle=a.jingle;
        _o.pid=a.pid;
        _o.href=a.href;
        _o.level=a.level;
        _o.sort=a.sort;
        _o.icon=a.icon;
        _o.status=a.status;
        _o.create_time=a.create_time;
        _o.update_time=a.update_time;

        let _nodes=getTreeChildJson(a.id2);
        _o.children=_nodes;
        _jj.push(_o);

        //console.log(a.text);

    });
    //console.log(_jj);
    return _jj;
    
}


function getTreeChildJson(pid){
    let _defaultData=[];
    _defaultData=_data.filter(a=>{
        return a.pid===pid
    });
    
    //console.log(_defaultData)
    
    let _jj=[];
    _defaultData.forEach(a=>{
        var _o=new Object();
        _o.id2=a.id2;
        _o.name=a.name;
        _o.jingle=a.jingle;
        _o.pid=a.pid;
        _o.href=a.href;
        _o.level=a.level;
        _o.sort=a.sort;
        _o.icon=a.icon;
        _o.status=a.status;
        _o.create_time=a.create_time;
        _o.update_time=a.update_time;


        let _nodes=getTreeChildJson(a.id2);
        _o.children=_nodes;
        _jj.push(_o);  
        


        //console.log(a.text);


    });
    //console.log(_jj);
    return _jj;
    
}




console.log(defaultData);
console.log(JSON.stringify(defaultData));