json数据转化为bootstrap treeview填充数据格式util

后台响应的数据:

[javascript] view plain copy
  1. [  
  2.         {  
  3.             "menuGroupName":"应用管理",  
  4.             "menu": [  
  5.                 {  
  6.                     "menuName""文件列表",  
  7.                     "function": [  
  8.                         {  
  9.                             "functionName""添加或编辑栏目分类"  
  10.                         },  
  11.                         {  
  12.                             "functionName""删除栏目分类"  
  13.                         },  
  14.                         {  
  15.                             "functionName""创建或编辑栏目分类"  
  16.                         },  
  17.                         {  
  18.                             "functionName""删除文章"  
  19.                         },  
  20.                         {  
  21.                             "functionName""移动文章"  
  22.                         },  
  23.                         {  
  24.                             "functionName""设置或移除焦点文章"  
  25.                         },  
  26.                         {  
  27.                             "functionName""同步微站栏目"  
  28.                         },  
  29.                         {  
  30.                             "functionName""同步微站文章"  
  31.                         }  
  32.                     ]  
  33.                 },  
  34.                 {  
  35.                     "menuName""用户列表",  
  36.                     "function": [  
  37.                         {  
  38.                             "functionName""回复消息"  
  39.                         },  
  40.                         {  
  41.                             "functionName""创建消息会话"  
  42.                         },  
  43.                         {  
  44.                             "functionName""新增或修改用户组"  
  45.                         },  
  46.                         {  
  47.                             "functionName""删除用户组"  
  48.                         }  
  49.                     ]  
  50.                 },  
  51.                 {  
  52.                     "menuName""用户记录",  
  53.                     "function": [  
  54.                     ]  
  55.                 },  
  56.                 {  
  57.                     "menuName""推送记录",  
  58.                     "function": [  
  59.                         {  
  60.                             "functionName""推送"  
  61.                         }  
  62.                     ]  
  63.                 },  
  64.                 {  
  65.                     "menuName""统计信息",  
  66.                     "function": [  
  67.                     ]  
  68.                 }  
  69.             ]  
  70.         },  
  71.         {  
  72.             "menuGroupName":"随访管理",  
  73.             "menu": [  
  74.                 {  
  75.                     "menuName""随访列表",  
  76.                     "function": [  
  77.                     ]  
  78.                 },  
  79.                 {  
  80.                     "menuName""随访档案",  
  81.                     "function": [  
  82.                     ]  
  83.                 },  
  84.                 {  
  85.                     "menuName""随访统计",  
  86.                     "function": [  
  87.                     ]  
  88.                 },  
  89.                 {  
  90.                     "menuName""随访设置",  
  91.                     "function": [  
  92.                     ]  
  93.                 },  
  94.                 {  
  95.                     "menuName""任务管理",  
  96.                     "function": [  
  97.                     ]  
  98.                 }  
  99.             ]  
  100.         }  
  101. ]  



bootstrap treeview接受的json数据格式(Demo):


[javascript] view plain copy
  1. [  
  2.             {  
  3.                 "text""Parent 1",  
  4.                 "nodes": [  
  5.                     {  
  6.                         "text""Child 1",  
  7.                         "nodes": [  
  8.                             {  
  9.                                 "text""SubChild 1"  
  10.                             },  
  11.                             {  
  12.                                 "text""SubChild 2"  
  13.                             },  
  14.                             {  
  15.                                 "text""SubChild 3"  
  16.                             },  
  17.                             {  
  18.                                 "text""SubChild 4"  
  19.                             }  
  20.                         ]  
  21.                     },  
  22.                     {  
  23.                         "text""Child 2"  
  24.                     },  
  25.                     {  
  26.                         "text""Child 3"  
  27.                     }  
  28.                 ]  
  29.             },  
  30.             {  
  31.                 "text""Parent 2",  
  32.                 "nodes": [  
  33.                     {  
  34.                         "text""Child 1"  
  35.                     },  
  36.                     {  
  37.                         "text""Child 2"  
  38.                     },  
  39.                     {  
  40.                         "text""Child 3"  
  41.                     }  
  42.                 ]  
  43.             },  
  44.             {  
  45.                 "text""Parent 3",  
  46.                 "nodes": [  
  47.                     {  
  48.                         "text""Child 1"  
  49.                     },  
  50.                     {  
  51.                         "text""Child 2"  
  52.                     },  
  53.                     {  
  54.                         "text""Child 3"  
  55.                     }  
  56.                 ]  
  57.             }  
  58. ]  



转化过程:


[javascript] view plain copy
  1. var columnStructure = [{text: "menuGroupName", nodes: "menu"},{text: "menuName", nodes: "function"},{text: "functionName", nodes: ""}];//外来数据转化为treeView数据的转化结构  
[javascript] view plain copy
  1. $scope.columnsTree = obj2treeview(response,columnStructure);//将外来数据转化为treeview数据  



util源码:


[javascript] view plain copy
  1. /** 
  2.  * @desc 将N级外来数据转化为bootstrap treeview可填充的数据 
  3.  * @param {Object} resp 需要被处理的数组 
  4.  * @param {Array} structure such as [{text: "menuGroupName", nodes: "menu"},{text: "menuName", nodes: "function"},{text: "functionName", nodes: "..."},...] 
  5.  *                              但需要注意,structure内的元素必须是按层级从左向右依次下降的。 
  6.  */  
  7.   
  8. loopLevel=0;  
  9. function obj2treeview(resp,structure){  
  10.     var nodeArray = new Array();  
  11.     var i = 0;  
  12.         for(i= 0;i<resp.length;i++){  
  13.             var treeViewNodeObj;  
  14.             var textStr = structure[loopLevel].text;  
  15.             var nodeStr = structure[loopLevel].nodes;  
  16.           
  17.             var subNode;  
  18.             if(resp[i][nodeStr] != undefined){  
  19.                 loopLevel++;  
  20.                 subNode = obj2treeview(resp[i][nodeStr],structure);  
  21.                 loopLevel--;  
  22.             }  
  23.               
  24.             if(subNode != undefined){  
  25.                 treeViewNodeObj = {  
  26.                     text: resp[i][textStr],  
  27.                     nodes: subNode  
  28.                 };  
  29.             }else{  
  30.                 treeViewNodeObj = {  
  31.                     text: resp[i][textStr]  
  32.                 };  
  33.             }  
  34.             nodeArray.push(treeViewNodeObj);  
  35.         }  
  36.         return nodeArray  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值