AI智能
改变未来

JS生成easyui-combotree所需要的数据格式

var obj = [{id:1,value:\"一\",parentid:1},{id:2,value:\"一\",parentid:1},{id:3,value:\"一\",parentid:2},{id:4,value:\"一\",parentid:1},{id:5,value:\"一\",parentid:3},{id:6,value:\"一\",parentid:2},];function ceshi(){var data = new Object();var parentids = new Array();//所有父节点for(var i=0;i<obj.length;i++){data[obj[i].id] =obj[i];if(obj[i].parentid && parentids.indexOf(obj[i].parentid)==-1){parentids.push(obj[i].parentid);}}var rootids=new Array();//主节点for(var i=0;i<obj.length;i++){obj[i].status = \"close\";if(parentids.indexOf(obj[i].id)==-1){obj[i].status = \"open\";}if(obj[i].id!=obj[i].parentid && obj[i].parentid){if(data[obj[i].parentid].children){data[obj[i].parentid].children.push(obj[i]);}else{data[obj[i].parentid].children = [obj[i]];}}else{rootids.push(obj[i].id);}}var back = new Array();for(var i=0;i<rootids.length;i++){back.push(data[rootids[i]]);}return back;}

利用了object的指针属性

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » JS生成easyui-combotree所需要的数据格式