TreeGrid:设置数据root无效

时间:2012-02-23 10:30:09

标签: extjs extjs4 extjs-mvc

我正在尝试设置TreeGrid,我的数据对象如下所示:

{
    "code": "success",
    "data": {
        "text": ".",
        "children": [
            {
                "clientname": "Market",
                "contact": "OpenX Market Advertiser",
                "email": "asdasd@asdasd222.de",

我需要告诉Ext它应该使用 data 作为根元素:

var store = Ext.create('Ext.data.TreeStore', {
    model: 'Task',
    proxy: {
        type: 'ajax',
        url: 'http://localhost/rocketads/trunk/advertisers/index/stats:true/',
        reader:{
            type:'json',
            root:'data'
        }
    },
});

虽然我在普通商店成功使用它,但这对我不起作用。

4 个答案:

答案 0 :(得分:3)

由于您已使用root: 'data'设置了阅读器,因此必须更换“孩子”。使用'数据'在你的json:

{
    "code": "success",
    "data": {
        "text": ".",
        "data": [ // << not "children"
            {

答案 1 :(得分:3)

您也可以使用:

        reader: {
        type: 'json',
        // See http://stackoverflow.com/questions/9159627/extjs-loading-tree-from-json-file-using-mvc
        // http://stackoverflow.com/questions/6263380/extjs4-json-treestore
        root: function(o) {
            if (o.data) {
                return o.data;
            } else {
                return o.children;
            }
        }
    }, 

答案 2 :(得分:1)

不幸的是,对于TreeStore,无论你使用root用户还是用作每个后续级别的根,所以它可能找到根节点,然后找不到它的data属性。将顶级属性从数据更改为子级或将每个子级实例更改为数据。

答案 3 :(得分:0)

我在ExtJS 4.2中遇到同样的问题 我尝试通过以下代码来解决这个问题。

Ext.define('XZSoftware.view.sysconfig.permission.PermissionWindow', {
    extend: 'Ext.window.Window',
    itemid: "sysconfig-permission-window",
    vierConfig: { loadMask: true },
    layout:
    {
        type: "fit"
    },
    minWidth: 400,
    minHeight: 300,
    tree: null,
    treeStore: null,
    initComponent: function () {
        var me = this;
        Ext.log("initComponent()", me);

        me.treeStore = Ext.create("Ext.data.TreeStore", {
            proxy: {
                type: "ajax",
                actionMethods: { read: "POST" },
                reader: {
                    type: "json",
                    root: "data"
                },
                url: '/permissiontree_load.xzsoftware?o=AAAAACB'
            },
            root: {
                text: "Premission Tree",
                expanded: true
            }
        });

        me.tree = Ext.create("Ext.tree.Panel", {
            rootVisible: false,
            store: me.treeStore
        });

        me.items = [
            me.tree
        ];

        this.callParent(arguments);
    },

    Version: "1.0.0.0"

});

将从HTTP请求中响应JSON。

{
    "total": 17,
    "data": [{
        "data": [{
            "data": [],
            "expanded": true,
            "leaf": "true",
            "checked": false,
            "text": "HSBC"
        }, {
            "data": [],
            "expanded": true,
            "leaf": "true",
            "checked": false,
            "text": "All IDoc"
        }, {
            "data": [],
            "expanded": true,
            "leaf": "true",
            "checked": false,
            "text": "ProCRM"
        }],
        "expanded": true,
        "leaf": "false",
        "checked": false,
        "text": "Application Portal"
    }, {
        "data": [{
            "data": [],
            "expanded": true,
            "leaf": "true",
            "checked": false,
            "text": "Order List"
        }, {
            "data": [{
                "data": [],
                "expanded": true,
                "leaf": "true",
                "checked": false,
                "text": "Report by monthly total"
            }],
            "expanded": true,
            "leaf": "true",
            "checked": false,
            "text": "Order Report"
        }, {
            "data": [],
            "expanded": true,
            "leaf": "true",
            "checked": false,
            "text": "Search"
        }],
        "expanded": true,
        "leaf": "false",
        "checked": false,
        "text": "CNMOT - OMS"
    }, {
        "data": [{
            "data": [],
            "expanded": true,
            "leaf": "true",
            "checked": false,
            "text": "Company"
        }, {
            "data": [],
            "expanded": true,
            "leaf": "true",
            "checked": false,
            "text": "Funcation Setting"
        }, {
            "data": [],
            "expanded": true,
            "leaf": "true",
            "checked": false,
            "text": "Menu Setting"
        }, {
            "data": [],
            "expanded": true,
            "leaf": "true",
            "checked": false,
            "text": "Page Setting"
        }, {
            "data": [],
            "expanded": true,
            "leaf": "true",
            "checked": false,
            "text": "Permission"
        }, {
            "data": [],
            "expanded": true,
            "leaf": "true",
            "checked": false,
            "text": "Roles"
        }, {
            "data": [],
            "expanded": true,
            "leaf": "true",
            "checked": false,
            "text": "Users"
        }],
        "expanded": true,
        "leaf": "false",
        "checked": false,
        "text": "System Setting"
    }],
    "success": true,
    "message": "Success"
}

注意: 1.应该在TreeStore中设置root配置。 2. JSON中的children字段需要更改为store的代理中的root配置。在我的样本“孩子”中设置为“数据”。