Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将extjsajax代理返回的原始数据对象修改为一个JSON对象,供树存储使用_Extjs_Extjs Stores - Fatal编程技术网

如何将extjsajax代理返回的原始数据对象修改为一个JSON对象,供树存储使用

如何将extjsajax代理返回的原始数据对象修改为一个JSON对象,供树存储使用,extjs,extjs-stores,Extjs,Extjs Stores,为了创建一个treepanel,我将其配置为一个treestore,它的AJAX代理url接收我无法控制的json数据。但是,在readRecords执行之前,使用可调用的Ext.data.reader.Json的transform,可以将从AJAX代理传递的原始(反序列化)数据对象修改为修改过的或全新的数据对象。转换配置提供以下代码片段: Ext.create('Ext.data.Store', { model: 'User', proxy: { type: '

为了创建一个treepanel,我将其配置为一个treestore,它的AJAX代理url接收我无法控制的json数据。但是,在readRecords执行之前,使用可调用的
Ext.data.reader.Json
transform
,可以将从AJAX代理传递的原始(反序列化)数据对象修改为修改过的或全新的数据对象。
转换
配置提供以下代码片段:

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
}
]
放入适合treestore的JSON对象中

将使用返回的JSON中的条件
administrator==true
,呈现层次结构树以显示哪个用户是哪个管理员,然后是返回此处显示的管理员用户的第二个AJAX请求

[
    {
        "user_id": 3,
        "admin_id": 1,
    },
    {
        "user_id": 4,
        "admin_id": 2,
    }
    ]

数据是嵌套的吗?否则,为什么要使用树面板而不是网格?对于您的问题,这将取决于您如何配置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
        }

为了更清楚地说明数据是否嵌套,我修改了这个问题。事实上,你的回答让我更接近我所寻求的解决方案。期待您或其他人对此问题的意见。