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

本文介绍如何处理后台返回的数据格式与jQuery EasyUI树形控件要求不符的情况,通过loadFilter方法实现数据格式转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用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;
					}
			    }
              
	        });



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值