1 function toTreeData(data){ 2 var pos={}; 3 var tree=[]; 4 var i=0; 5 while(data.length!=0){ 6 if(data[i].parentid==0){ 7 tree.push({ 8 resid:data[i].resid, 9 caption:data[i].caption, 10 href:data[i].href, 11 children:[] 12 }); 13 pos[data[i].resid]=[tree.length-1]; 14 data.splice(i,1); 15 i--; 16 }else{ 17 var posArr=pos[data[i].parentid]; 18 if(posArr!=undefined){ 19 20 var obj=tree[posArr[0]]; 21 for(var j=1;j<posArr.length;j++){ 22 obj=obj.children[posArr[j]]; 23 } 24 25 obj.children.push({ 26 resid:data[i].resid, 27 caption:data[i].caption, 28 href:data[i].href, 29 children:[] 30 }); 31 pos[data[i].resid]=posArr.concat([obj.children.length-1]); 32 data.splice(i,1); 33 i--; 34 } 35 } 36 i++; 37 if(i>data.length-1){ 38 i=0; 39 } 40 } 41 return tree; 42 }
时间: 2024-11-13 20:56:44