ExtJS4如何从JSON设置TreePanels根节点

时间:2011-08-05 13:58:18

标签: json tree extjs4 root

我想使用ExtJS4创建TreePanel。所以我将JSON发送到具有以下格式的代理阅读器

{
    "text": "en",
    "children": {
        "text": "/",
        "children": [{
                "text": "/page",
                "children": [{
                        "text": "/page/new",
                        "children": [],
                        "leaf": true,
                        "expanded": false
                    },
                    {
                        "text": "/page/remove",
                        "children": [],
                        "leaf": true,
                        "expanded": false
                    }
                ],
                "leaf": false,
                "expanded": false
            },
            {
                "text": "/home",
                "children": [],
                "leaf": true,
                "expanded": false
            }
        ],
        "leaf": false,
        "expanded": true
    }
}

如果我希望en节点作为我的根节点,我如何配置我的商店。

Ext.define('Example.store.WebItems', {
    extend: 'Ext.data.TreeStore',

    model: 'Example.model.Item',

    proxy: {
        type: 'ajax',
        api: {
            read: 'some/url',
        },
        reader: {
            type: 'json',
            root: 'children' // Is this correct?
        }
    },
    root: // What should I write here?
});

当我将TreeStore的根定义为root: 'My Root'时,它会添加新根,但我想使用JSON中定义的根。

所以我的问题是:

  1. 如何使用JSON中的根节点而不是手动定义它?
  2. 我是否必须在代理阅读器和TreeStore中定义根节点?

3 个答案:

答案 0 :(得分:5)

响应必须为root

例如:

{
     MyRoot: {
         "text": "en",
         "children": {
             "text": "/",
             "children": [{
                 "text": "/page",
                 "children": [{
                     "text": "/page/new",
                     "children": [],
                     "leaf": true,
                     "expanded": false
                 }, {
                     "text": "/page/remove",
                     "children": [],
                     "leaf": true,
                     "expanded": false
                 }],
                 "leaf": false,
                 "expanded": false
             }, {
                 "text": "/home",
                 "children": [],
                 "leaf": true,
                 "expanded": false
             }],
             "leaf": false,
             "expanded": true
         }
     }
 }

在此示例中,rootMyRoot。现在,您必须“告诉”读者您的rootMyRoot

        // ...
        reader: {
            type: 'json',
            root: 'MyRoot'
        }
    },
    //root: this config is not needed now
});

答案 1 :(得分:1)

据我所知,你需要指定root如下:

root: {
    text: 'en',
    id: 'src',
    expanded: true '
}

尝试上面的代码段,它会对你有帮助。

答案 2 :(得分:0)

可能会迟到但是对于其他人来说,将JSON包含在数组[]内就像这样:

[{
    "text": "en",
    "children": {
        "text": "/",
        "children": [{
            "text": "/page",
            "children": [{
                "text": "/page/new",
                "children": [],
                "leaf": true,
                "expanded": false
            }, {
                "text": "/page/remove",
                "children": [],
                "leaf": true,
                "expanded": false
            }],
            "leaf": false,
            "expanded": false
        }, {
            "text": "/home",
            "children": [],
            "leaf": true,
            "expanded": false
        }],
        "leaf": false,
        "expanded": true
    }
}]

删除阅读器块

reader: {
    type: 'json',
    root: 'MyRoot'
}

添加根块:<​​/ p>

root: {
    text: 'en',
    id: 'src',
    expanded: true '
}

rootVisible: false中添加treepanel