ExtJs4 Json TreeStore?

霸气de小男生 提交于 2019-12-21 23:01:42

问题


I am migrating my ExtJs3 application to ExtJs4.
In ExtJs3 I had a tree grid which had a loader to load the tree data, like below:

loader: new Ext.tree.TreeLoader({ 
            dataUrl: 'Department/DepartmentTree', 
            nestedRoot: 'Data.items'
    })

So I am trying to create a treeStore in ExtJs4 like below:

   var store = Ext.create('Ext.data.TreeStore', {
        model: 'DepartmentTreeModel',
        folderSort: true,
        proxy: {
            type: 'ajax',
            url: 'Department/DepartmentTree',
            reader: {
                type: 'json',
                root: 'Data.items'
            }
        }
    });

I get an error when I call the load function on the above store Cannot read property 'items' of undefined.

The JSON response looks like this:

    {
"Data":{
    "__type":"ListWrapperOfDepartmentTreeNodewnEzJCii:#PortalMvc.Global.Classlibrary.Model.Ui.JSONWrappers",
    "items":[{
        "ActualHeadcount":0,
        "Headcount":0,
        "Leavers":0,
        "ParentId":"~~",
        "Starters":0,
        "children":[{
            "ActualHeadcount":0,
            "Headcount":0,
            "Leavers":0,
            "ParentId":"!#",
            "Starters":0,
            "children":[{
                "ActualHeadcount":0,
                "Headcount":0,
                "Leavers":0,
                "ParentId":"*w",
                "Starters":0,
                "children":[{
                    "ActualHeadcount":0,
                    "Headcount":0,
                    "Leavers":0,
                    "ParentId":"*z",
                    "Starters":0,
                    "children":[],
                    "iconCls":"admin_button",
                    "id":"*{",
                    "leaf":true,
                    "text":"Parking1_subtree1"
                }]
            }]
        }]
    }]
  }
}

回答1:


It expects the root to be repeated for each set of child nodes. So for "children" it's also trying to read "Data.items".

If you can't alter the data structure, the root can also be a function, something like:

root: function(o) {
    if (o.Data) {
        return o.Data.items;
    } else {
        return o.children;
    }
}


来源:https://stackoverflow.com/questions/6263380/extjs4-json-treestore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!