如何在一个请求中加载TreePanel中的所有节点(及其子节点)?

时间:2014-02-05 21:20:57

标签: extjs extjs4 extjs4.2

我正在尝试创建一个TreePanel,在创建时在一个请求中加载其所有项目(及其子项)。从我阅读的所有内容中,如果项目具有“子”属性,则不应尝试在扩展时再次加载子项,不管我的是什么。

Ext.define('ec.view.MarketGroupTree', {
extend: 'Ext.tree.Panel',
requires:[
    'Ext.tree.*',
    'Ext.data.*'
],

xtype: 'market-group-tree',

store: Ext.create('Ext.data.TreeStore', {
    autoLoad: true,
    root: {
        expanded: true
    },
    fields: ['marketGroupName', 'children', 'marketGroupID'],
    proxy: {
        type: 'ajax',
        url: 'backend/market.php?a=marketGroupTree',
        reader: {
            type: 'json',
            root: 'records'
        }
    }
}),

rootVisible: false,
rowLines: true,
sealedColumns: true,
singleExpand: true,
useArrows: true,
headerPosition: 'left',
lines: false,

columns: {
    items: [
        {
            xtype: 'treecolumn',
                dataIndex: 'marketGroupName',
                flex: 1
            },
            {
                xtype: 'treecolumn',
                dataIndex: 'marketGroupID',
                flex: 0.3
            }
        ]
    }
});

从后端/ market.php收到的JSON样本位?a = marketGroupTree是:

{
   "success":true,
   "records":[
      {
         "marketGroupID":"2",
         "marketGroupName":"Blueprints",
     "iconID":"2703",
     "hasTypes":"1",
     "leaf":false,
     "children":[
        {
           "marketGroupID":"204",
           "marketGroupName":"Ships",
           "iconID":"2703",
           "hasTypes":"1",
           "leaf":false,
           "children":[
              {
                 "marketGroupID":"205",
                 "marketGroupName":"Frigates",
                 "iconID":"2703",
                 "hasTypes":"1",
                 "leaf":false,
                 "children":[
                    {
                       "marketGroupID":"261",
                       "marketGroupName":"Caldari",
                       "iconID":"0",
                       "hasTypes":"1",
                       "leaf":true
                    },
                    {
                       "marketGroupID":"264",
                       "marketGroupName":"Minmatar",
                       "iconID":"0",
                       "hasTypes":"1",
                       "leaf":true
                    }
                 ]
              },
              {
                 "marketGroupID":"206",
                 "marketGroupName":"Cruisers",
                 "iconID":"2703",
                 "hasTypes":"1",
                 "leaf":false,
                 "children":[
                    {
                       "marketGroupID":"273",
                       "marketGroupName":"Minmatar",
                       "iconID":"0",
                       "hasTypes":"1",
                       "leaf":true
                    },
                    {
                       "marketGroupID":"274",
                       "marketGroupName":"Amarr",
                       "iconID":"0",
                       "hasTypes":"1",
                       "leaf":true
                    }
                 ]
              }
           ]
        },
        {
           "marketGroupID":"209",
           "marketGroupName":"Ship Equipment",
           "iconID":"2703",
           "hasTypes":"1",
           "leaf":false,
           "children":[
              {
                 "marketGroupID":"210",
                 "marketGroupName":"Turrets & Bays",
                 "iconID":"2703",
                 "hasTypes":"1",
                 "leaf":false,
                 "children":[
                    {
                       "marketGroupID":"286",
                       "marketGroupName":"Hybrid Turrets",
                       "iconID":"2703",
                       "hasTypes":"1",
                       "leaf":false,
                       "children":[
                          {
                             "marketGroupID":"289",
                             "marketGroupName":"Large",
                             "iconID":"0",
                             "hasTypes":"1",
                             "leaf":true
                          },
                          {
                             "marketGroupID":"290",
                             "marketGroupName":"Medium",
                             "iconID":"0",
                             "hasTypes":"1",
                             "leaf":true
                          }
                       ]
                    },
                    {
                       "marketGroupID":"287",
                       "marketGroupName":"Projectile Turrets",
                       "iconID":"2703",
                       "hasTypes":"1",
                       "leaf":false,
                       "children":[
                          {
                             "marketGroupID":"296",
                             "marketGroupName":"Small",
                             "iconID":"0",
                             "hasTypes":"1",
                             "leaf":true
                          },
                          {
                             "marketGroupID":"297",
                             "marketGroupName":"Medium",
                             "iconID":"0",
                             "hasTypes":"1",
                             "leaf":true
                          }
                       ]
                    }
                 ]
              },
              {
                 "marketGroupID":"214",
                 "marketGroupName":"Hull & Armor ",
                 "iconID":"2703",
                 "hasTypes":"1",
                 "leaf":false,
                 "children":[
                    {
                       "marketGroupID":"335",
                       "marketGroupName":"Hull Upgrades",
                       "iconID":"0",
                       "hasTypes":"1",
                       "leaf":true
                    },
                    {
                       "marketGroupID":"1536",
                       "marketGroupName":"Armor Repairers",
                       "iconID":"0",
                       "hasTypes":"1",
                       "leaf":true
                    }
                 ]
              }
           ]
        }
     ]
  },
  {
     "marketGroupID":"4",
     "marketGroupName":"Ships",
     "iconID":"1443",
     "hasTypes":"1",
     "leaf":false,
     "children":[
        {
           "marketGroupID":"391",
           "marketGroupName":"Shuttles",
           "iconID":"1443",
           "hasTypes":"1",
           "leaf":false,
           "children":[
              {
                 "marketGroupID":"393",
                 "marketGroupName":"Amarr",
                 "iconID":"0",
                 "hasTypes":"1",
                 "leaf":true
              },
              {
                 "marketGroupID":"394",
                 "marketGroupName":"Minmatar",
                 "iconID":"0",
                 "hasTypes":"1",
                 "leaf":true
              }
           ]
        },
        {
           "marketGroupID":"1361",
           "marketGroupName":"Frigates",
           "iconID":"1443",
           "hasTypes":"1",
           "leaf":false,
           "children":[
              {
                 "marketGroupID":"5",
                 "marketGroupName":"Standard Frigates",
                 "iconID":"1443",
                 "hasTypes":"1",
                 "leaf":false,
                 "children":[
                    {
                       "marketGroupID":"61",
                       "marketGroupName":"Caldari",
                       "iconID":"0",
                       "hasTypes":"1",
                       "leaf":true
                    },
                    {
                       "marketGroupID":"64",
                       "marketGroupName":"Minmatar",
                       "iconID":"0",
                       "hasTypes":"1",
                       "leaf":true
                    }
                 ]
              },
              {
                 "marketGroupID":"1362",
                 "marketGroupName":"Faction Frigates",
                 "iconID":"1443",
                 "hasTypes":"1",
                 "leaf":false,
                 "children":[
                    {
                       "marketGroupID":"1365",
                       "marketGroupName":"Pirate Faction",
                       "iconID":"0",
                           "hasTypes":"1",
                           "leaf":true
                        },
                        {
                           "marketGroupID":"1366",
                           "marketGroupName":"Navy Faction",
                           "iconID":"0",
                           "hasTypes":"1",
                           "leaf":true
                        }
                     ]
                  }
               ]
            }
         ]
      }
   ],
   "metaData":{
      "rootProperty":"records"
   },
   "num_sent":2
}

1 个答案:

答案 0 :(得分:3)

事实证明,我使用“记录”作为根。看来,如果你改变了根目录的名称,这个名字也会改变孩子们所在的位置,所以每个项目都应该有一个带有子项的“records”属性,而不是“child”属性。