后台响应的数据:
- [
- {
- "menuGroupName":"应用管理",
- "menu": [
- {
- "menuName": "文件列表",
- "function": [
- {
- "functionName": "添加或编辑栏目分类"
- },
- {
- "functionName": "删除栏目分类"
- },
- {
- "functionName": "创建或编辑栏目分类"
- },
- {
- "functionName": "删除文章"
- },
- {
- "functionName": "移动文章"
- },
- {
- "functionName": "设置或移除焦点文章"
- },
- {
- "functionName": "同步微站栏目"
- },
- {
- "functionName": "同步微站文章"
- }
- ]
- },
- {
- "menuName": "用户列表",
- "function": [
- {
- "functionName": "回复消息"
- },
- {
- "functionName": "创建消息会话"
- },
- {
- "functionName": "新增或修改用户组"
- },
- {
- "functionName": "删除用户组"
- }
- ]
- },
- {
- "menuName": "用户记录",
- "function": [
- ]
- },
- {
- "menuName": "推送记录",
- "function": [
- {
- "functionName": "推送"
- }
- ]
- },
- {
- "menuName": "统计信息",
- "function": [
- ]
- }
- ]
- },
- {
- "menuGroupName":"随访管理",
- "menu": [
- {
- "menuName": "随访列表",
- "function": [
- ]
- },
- {
- "menuName": "随访档案",
- "function": [
- ]
- },
- {
- "menuName": "随访统计",
- "function": [
- ]
- },
- {
- "menuName": "随访设置",
- "function": [
- ]
- },
- {
- "menuName": "任务管理",
- "function": [
- ]
- }
- ]
- }
- ]
bootstrap treeview接受的json数据格式(Demo):
- [
- {
- "text": "Parent 1",
- "nodes": [
- {
- "text": "Child 1",
- "nodes": [
- {
- "text": "SubChild 1"
- },
- {
- "text": "SubChild 2"
- },
- {
- "text": "SubChild 3"
- },
- {
- "text": "SubChild 4"
- }
- ]
- },
- {
- "text": "Child 2"
- },
- {
- "text": "Child 3"
- }
- ]
- },
- {
- "text": "Parent 2",
- "nodes": [
- {
- "text": "Child 1"
- },
- {
- "text": "Child 2"
- },
- {
- "text": "Child 3"
- }
- ]
- },
- {
- "text": "Parent 3",
- "nodes": [
- {
- "text": "Child 1"
- },
- {
- "text": "Child 2"
- },
- {
- "text": "Child 3"
- }
- ]
- }
- ]
转化过程:
- var columnStructure = [{text: "menuGroupName", nodes: "menu"},{text: "menuName", nodes: "function"},{text: "functionName", nodes: ""}];//外来数据转化为treeView数据的转化结构
- $scope.columnsTree = obj2treeview(response,columnStructure);//将外来数据转化为treeview数据
util源码:
- /**
- * @desc 将N级外来数据转化为bootstrap treeview可填充的数据
- * @param {Object} resp 需要被处理的数组
- * @param {Array} structure such as [{text: "menuGroupName", nodes: "menu"},{text: "menuName", nodes: "function"},{text: "functionName", nodes: "..."},...]
- * 但需要注意,structure内的元素必须是按层级从左向右依次下降的。
- */
- loopLevel=0;
- function obj2treeview(resp,structure){
- var nodeArray = new Array();
- var i = 0;
- for(i= 0;i<resp.length;i++){
- var treeViewNodeObj;
- var textStr = structure[loopLevel].text;
- var nodeStr = structure[loopLevel].nodes;
- var subNode;
- if(resp[i][nodeStr] != undefined){
- loopLevel++;
- subNode = obj2treeview(resp[i][nodeStr],structure);
- loopLevel--;
- }
- if(subNode != undefined){
- treeViewNodeObj = {
- text: resp[i][textStr],
- nodes: subNode
- };
- }else{
- treeViewNodeObj = {
- text: resp[i][textStr]
- };
- }
- nodeArray.push(treeViewNodeObj);
- }
- return nodeArray
- }