jQuery-easyUI Tree 后台返回json数据和官网例子有差别,如何处理

在使用jquery-easyUI 时,接收后台返回是数据,可能不是官网例子的一个大数组,而是一个对象包含,多个对象,再包含数组:

官网json格式:

[{
    "id":1,
    "text":"Folder1",
    "iconCls":"icon-save",
    "children":[{
		"text":"File1",
		"checked":true
    },{
		"text":"Books",
		"state":"open",
		"attributes":{
			"url":"/demo/book/abc",
			"price":100
		},
		"children":[{
			"text":"PhotoShop",
			"checked":true
		},{
			"id": 8,
			"text":"Sub Bookds",
			"state":"closed"
		}]
    }]
},{
    "text":"Languages",
    "state":"closed",
    "children":[{
		"text":"Java"
    },{
		"text":"C#"
    }]
}]

而后台实际返回的json,可能是这样的:

 
 
{
  "msg": "success",
  "status":1,
   "data":[{
    "id":1,
    "text":"Folder1",
    "iconCls":"icon-save",
    "children":[{
		"text":"File1",
		"checked":true
    },{
		"text":"Books",
		"state":"open",
		"attributes":{
			"url":"/demo/book/abc",
			"price":100
		},
		"children":[{
			"text":"PhotoShop",
			"checked":true
		},{
			"id": 8,
			"text":"Sub Bookds",
			"state":"closed"
		}]
    }]
},{
    "text":"Languages",
    "state":"closed",
    "children":[{
		"text":"Java"
    },{
		"text":"C#"
    }]
}]
}

如何获取到渲染树所需的数据呢,这里用到一个方法loadFilter(返回过滤过的数据进行展示)

$('#treePower').tree({     
	          url:"../jquery-easyui/tree_data3.json",
	          method:"get",
	          animate:true,
	          checkbox:true,
	          loadFilter: function(data){ //返回过滤过的数据进行展示。返回数据是标准树格式。
					if (data.data){
						return data.data;
					} else {
						return data;
					}
			    }
              
	        });



猜你喜欢

转载自blog.csdn.net/yufengaotian/article/details/80091137