0

为了创建树形面板,我使用树形存储对其进行配置,其 AJAX 代理 url 接收我无法控制的 json 数据。但是在 readRecords 执行之前使用Ext.data.reader.Json'stransform 属性invokable 可以将传递的原始(反序列化)数据对象从 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,
    }
    ]
4

1 回答 1

0

数据是嵌套的吗?否则为什么要使用树形面板而不是网格?不过,对于您的问题,这取决于您如何配置树形面板,但可能是这样的:

        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
        }
于 2020-02-07T20:48:15.343 回答