Treepanel with nested data from JSON

主宰稳场 提交于 2019-12-20 02:52:21

问题


I want to add nested data from JSON into a treepanel. My problem is that the tree is being listed for the number of JSON records (number of divisions, here 1 in the example) without any text. But when I select a child, it shows Uncaught TypeError: Cannot read property 'internalId' of undefined error. This is what I have tried so far.

JSON file (schemesInfo.json).

{
"divisions":[
{"name": "division1","id": "div1","subdivisions":[
    {"name": "Sub1Div1","id": "div1sub1","schemes":[
        {"name": "Scheme1","id": "scheme1"},
        {"name": "Scheme2","id": "scheme2"}]},
    {"name": "Sub2Div1","id": "div1sub2","schemes":[
        {"name": "Scheme3","id": "scheme3"}]}

]}]
}

Model : (schemesmodel.js)

   Ext.define('GridApp.model.schemesmodel',{
    extend: 'Ext.data.Model',
    fields: ['name','id'],
    proxy: {
        type: 'ajax',
        api: {
            read: 'data/schemesInfo.json'
        },
        reader: {
            type: 'json',
            root: 'divisions'
        }
    }
    });

Store : (schemesstore.js)

    Ext.define('GridApp.store.schemesstore',{
    extend: 'Ext.data.TreeStore',
    model: 'GridApp.model.schemesmodel',
    autoLoad: true,

    listeners: {
        load:function(){
            console.log('Schemes Data store loaded');
        }
    },
    root: {
        text: "Divisions",
        expanded: true,
        id: 'NULL'
//      children: [
//                 {
//                     text:['name'],
////                       id: 'divsionnameid',
////                       leaf: true
//                 }]
    }
});

I am pretty new to Extjs 4. Let me know where I am going wrong. Any points regarding this will be very helpful.

Updated the question based on the discussion with @Izhaki.


回答1:


The property that represents children in your JSON has to be consist and match the root property of your reader. As yours is divisions you JSON should be:

{
   "divisions":[
      {
         "name":"division1",
         "id":"div1",
         "divisions":[
            {
               "name":"Sub1Div1",
               "id":"div1sub1",
               "divisions":[
                  {
                     "name":"Scheme1",
                     "id":"scheme1"
                  },
                  {
                     "name":"Scheme2",
                     "id":"scheme2"
                  }
               ]
            },
            {
               "name":"Sub2Div1",
               "id":"div1sub2",
               "divisions":[
                  {
                     "name":"Scheme3",
                     "id":"scheme3"
                  }
               ]
            }
         ]
      }
   ]
}

Also, don't forget displayField: 'name' in your TreePanel configs.



来源:https://stackoverflow.com/questions/12040130/treepanel-with-nested-data-from-json

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