为了创建一个treepanel,我给它配置了一个treestore,它的AJAX代理url接收我无法控制的json数据。但是,在readRecords执行之前使用Ext.data.reader.Json
的transform
property可调用,可以选择将从AJAX代理传递的原始(反序列化)数据对象修改为修改后的或全新的数据对象。transform
配置给出了以下代码片段:
Ext.create('Ext.data.Store', {
model: 'User',
proxy: {
type: 'ajax',
url : 'users.json',
reader: {
type: 'json',
transform: {
fn: function(data) {
// do some manipulation of the raw data object
return data;
},
scope: this
}
}
},
});
我想要一个关于如何修改返回的JSON对象的示例
[
{
"id": 3,
"attributes":
{},
"name": "user_one",
"login": "",
"email": "user_one@ats",
"phone": "0751223344",
"readonly": false,
"administrator": false,
"password": null
},
{
"id": 4,
"attributes":
{},
"name": "user_two",
"login": "",
"email": "user_two@ats",
"phone": "0751556677",
"readonly": false,
"administrator": false,
"password": null
}
]
转换为适合于树存储的JSON对象。
使用返回的JSON中的条件administrator==true
来呈现分层树,以显示哪个用户属于哪个管理员,然后使用第二个AJAX请求返回管理员的用户。
[
{
"user_id": 3,
"admin_id": 1,
},
{
"user_id": 4,
"admin_id": 2,
}
]
发布于 2020-02-08 04:48:15
数据是嵌套的吗?否则为什么要使用treepanel而不是网格呢?对于你的问题,这取决于你如何配置你的treepanel,但它可能是这样的:
transform: {
fn: function(data) {
var treeRecords = Ext.Array.map(data, function(i){
return {
text: i.name,
leaf: true
//any other properties you want
}
});
var treeData = {
root: {
expanded: true,
children: treeRecords
}
};
return treeData;
},
scope: this
}
https://stackoverflow.com/questions/60108827
复制相似问题