ExtJS 4.0.7 Load complete TreePanel

被刻印的时光 ゝ 提交于 2020-01-02 05:22:09

问题


I am in trouble with the ExtJS 4.0.7 TreePanel. I would like to load the complete tree from on JSON request. The request returns the following:

{
  "data": {
    "children": [
      {
        "text": "OS",
        "leaf": false,
        "children": [
          {
            "text": "Hostname",
            "leaf": false,
            "children": [
              {
                "text": "hostname.int.com",
                "leaf": true,
                "children": []
              }
            ]
          }
        ]
      }
    ]
  }
}

With it doesn't work with the following store configuration (or any other i've tried)

Ext.define('My.store.SysInfo', {
extend: 'Ext.data.TreeStore',
    model: 'My.model.SysInfo',

    proxy : {
        type : 'ajax',
        url : '/sysinfo/getSysInfo.php',
        reader : {
            root : 'data'
        }
    }
});

The Model has the following code:

Ext.define('My.model.SysInfo', {
    extend: 'Ext.data.Model',
    fields: ['text']
});

When adding a treepanel with this configuration, it doesn't work:

{
    xtype: 'treepanel',
    name : 'sysinfo',
    height: '100%',
    store: 'My.store.SysInfo',
    lines: true,
    autoScroll : true,
    expanded : true,
    rootVisible: false,
    folderSort: true,
    multiSelect: false,
    useArrows: true,
}

By opening a node, ExtJS always loads the whole tree from it's root node into the subnode by requesting it via ajax, instead of showing the preloaded data. How can i achieve, that it loads the "Hostname" node by opening "OS"?


回答1:


You can definitely do that. But there seems to be an issue with your json.

The root of your json is data, but in order for the nested data to load correctly, your root and all children properties need to be the same. Since you have defined your reader's root as data, you should replace all children in your json to data.

Please look at this question, and if you fancy a deeper understanding of it all also look at this question. You can also have a look at this working JsFiddle.

If you define your reader like so:

    reader : {
        type: 'json',
        // root : 'children' // no real need for this a children is the default.
    }

This json should load correctly:

{
  "children":[
     {
        "text":"OS",
        "leaf":false,
        "children":[
           {
              "text":"Hostname",
              "leaf":false,
              "children":[
                 {
                    "text":"hostname.int.com",
                    "leaf":true,
                    "children":[

                    ]
                 }
              ]
           }
        ]
     }
  ]
}


来源:https://stackoverflow.com/questions/12176410/extjs-4-0-7-load-complete-treepanel

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