/** * Created by cjj on 2018/5/4. */ function convert(rows){//默认三级 ,图标可变 function exists(rows, parentId){ for(var i=0; i<rows.length; i++){ if (rows[i].id == parentId) return true; } return false; } var nodes = []; // get the top level nodes for(var i=0; i<rows.length; i++){ var row = rows[i]; if (!exists(rows, row.parentId)){ nodes.push({ id:row.id, text:row.name }); } } var toDo = []; // var toDo = nodes; for(var i=0; i<nodes.length; i++){ toDo.push(nodes[i]); } while(toDo.length){ var node = toDo.shift(); // the parent node // get the children nodes for(var i=0; i<rows.length; i++){ var row = rows[i]; if (row.parentId == node.id){ var child = {id:row.id,text:row.name}; if (node.children){ node.children.push(child); } else { node.children = [child]; } toDo.push(child); } } } //设置菜单图标 var iconCls=["fi-home","fi-battery-half","fi-monitor"]; function icon(nodes,index) { if(index<iconCls.length) nodes["iconCls"] = iconCls[index++];//本级 if(nodes.children) {//子集递归 for (var i = 0; i < nodes.children.length; i++) { icon( nodes.children[i],index); } } } //遍历第一级 for(var j=0;j<nodes.length;j++){ nodes[j]["iconCls"]=iconCls[0];//一级 icon( nodes[j],0); } // console.log( nodes[j]); // if(nodes[j].children){ // for(var i=0;i<nodes[j].children.length;i++){ // // var node =nodes[j].children[i]; // node["iconCls"]="fi-battery-half";//二级 // if(node.children) {//三级 // for (var i = 0; i < nodes[j].children.length; i++) { // var node = nodes[j].children[i]; // node["iconCls"] = "fi-monitor";//三级 // } // } // } //} return nodes; }
easyui tree菜单根据json转换
最新推荐文章于 2022-04-12 11:07:25 发布