js版本的無限分類 樹形資料 格式化(配合遞迴)

來源:互聯網
上載者:User

標籤:cti   lis   return   console   name   ptr   格式化   function   格式   

function genTree(list) {    var temptree = [], tree = [], items = [];    for (var i in list) {        if (!temptree[list[i].did]) {            var trow = {                id: ‘z‘ + list[i].did,                did: list[i].did,                fid: list[i].fid,                text: list[i].text,                iconCls: ‘im_role‘,                children: []            };            temptree[list[i].did] = trow;            items.push(trow);        }        if (list[i].uid > 0) {             temptree[list[i].did][‘children‘].push({                id: list[i].uid,                text: list[i].realname,                iconCls: ‘im_user‘            });        }    }     for (var j in items) {        if (temptree[items[j].fid]) {            temptree[items[j].fid][‘children‘].push(temptree[items[j].did]);        } else {            tree.push(temptree[items[j].did]);        }    }    temptree = null;    items = null;    return tree; } var list=[//目錄    {did:1,fid:0,text:‘1111‘},    {did:2,fid:1,text:‘2222‘},    {did:3,fid:1,text:‘3333‘},    {did:4,fid:1,text:‘4444‘},    {did:5,fid:3,text:‘5555‘},    {did:6,fid:4,text:‘6666‘},    {did:7,fid:5,text:‘7777‘},    {did:8,fid:3,text:‘8888‘},    {did:9,fid:4,text:‘9999‘},     //目錄下面的人    {uid:1,did:2,realname:‘r111‘},    {uid:2,did:9,realname:‘r222‘},    {uid:3,did:2,realname:‘r333‘},    {uid:4,did:6,realname:‘r444‘},    {uid:5,did:7,realname:‘r555‘},    {uid:6,did:8,realname:‘r666‘},    {uid:7,did:9,realname:‘r777‘},     //或者在同一條記錄裡面,資料需要先排序保證目錄在前面    {did:10,fid:1,text:‘1010‘,uid:10,realname:‘r1010‘},    {did:11,fid:10,text:‘1111‘,uid:11,realname:‘r1111‘},    {did:12,fid:11,text:‘1212‘,uid:12,realname:‘r1212‘},    {did:13,fid:12,text:‘1313‘,uid:13,realname:‘r1313‘}];var tree=genTree(list);console.log(tree);  /*[    {        "id": "z1",        "did": 1,        "fid": 0,        "text": "1111",        "iconCls": "im_role",        "children": [            {                "id": "z2",                "did": 2,                "fid": 1,                "text": "2222",                "iconCls": "im_role",                "children": [                    {                        "id": 1,                        "text": "r111",                        "iconCls": "im_user"                    },                    {                        "id": 3,                        "text": "r333",                        "iconCls": "im_user"                    }                ]            },            {                "id": "z3",                "did": 3,                "fid": 1,                "text": "3333",                "iconCls": "im_role",                "children": [                    {                        "id": "z5",                        "did": 5,                        "fid": 3,                        "text": "5555",                        "iconCls": "im_role",                        "children": [                            {                                "id": "z7",                                "did": 7,                                "fid": 5,                                "text": "7777",                                "iconCls": "im_role",                                "children": [                                    {                                        "id": 5,                                        "text": "r555",                                        "iconCls": "im_user"                                    }                                ]                            }                        ]                    },                    {                        "id": "z8",                        "did": 8,                        "fid": 3,                        "text": "8888",                        "iconCls": "im_role",                        "children": [                            {                                "id": 6,                                "text": "r666",                                "iconCls": "im_user"                            }                        ]                    }                ]            },            {                "id": "z4",                "did": 4,                "fid": 1,                "text": "4444",                "iconCls": "im_role",                "children": [                    {                        "id": "z6",                        "did": 6,                        "fid": 4,                        "text": "6666",                        "iconCls": "im_role",                        "children": [                            {                                "id": 4,                                "text": "r444",                                "iconCls": "im_user"                            }                        ]                    },                    {                        "id": "z9",                        "did": 9,                        "fid": 4,                        "text": "9999",                        "iconCls": "im_role",                        "children": [                            {                                "id": 2,                                "text": "r222",                                "iconCls": "im_user"                            },                            {                                "id": 7,                                "text": "r777",                                "iconCls": "im_user"                            }                        ]                    }                ]            },            {                "id": "z10",                "did": 10,                "fid": 1,                "text": "1010",                "iconCls": "im_role",                "children": [                    {                        "id": 10,                        "text": "r1010",                        "iconCls": "im_user"                    },                    {                        "id": "z11",                        "did": 11,                        "fid": 10,                        "text": "1111",                        "iconCls": "im_role",                        "children": [                            {                                "id": 11,                                "text": "r1111",                                "iconCls": "im_user"                            },                            {                                "id": "z12",                                "did": 12,                                "fid": 11,                                "text": "1212",                                "iconCls": "im_role",                                "children": [                                    {                                        "id": 12,                                        "text": "r1212",                                        "iconCls": "im_user"                                    },                                    {                                        "id": "z13",                                        "did": 13,                                        "fid": 12,                                        "text": "1313",                                        "iconCls": "im_role",                                        "children": [                                            {                                                "id": 13,                                                "text": "r1313",                                                "iconCls": "im_user"                                            }                                        ]                                    }                                ]                            }                        ]                    }                ]            }        ]    }]*/

js版本的無限分類 樹形資料 格式化(配合遞迴)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.