json - ExtJS4 如何从 JSON 设置 TreePanels 根节点

标签 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 节点成为我的根节点,我该如何配置我的 Store。

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 中定义根节点?

最佳答案

响应必须有 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
});

关于json - ExtJS4 如何从 JSON 设置 TreePanels 根节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6957485/

相关文章:

javascript - JSON 将数字解析为字符串

javascript - 使用字符串变量作为 json 对象名称

haskell - 在 Haskell 中树 a -> a 的 Nil 值

javascript - 如何高亮Extjs网格新插入的行?

java - iReport 与 extjs4.0.2a 和 Java + Spring + Hibernate 的集成

java - 如何在 Gson 中制作自定义列表反序列化器?

php - 如何根据连接数组的值选择 INNER JOIN 表的列

c - 使用树在C中进行霍夫曼解码

c - 插入偏序树

extjs4 - ExtJS 4 面板 - 折叠时的文本方向