0

我正在尝试将嵌套的 JSONData 加载到我的树形网格中。在第一次调用获取数据时,填充网格所需的所有数据都作为 JSON 对象在响应中返回。但是我可以看到它仍然尝试为网格中的所有父对象获取数据。

即使在虚假 GET 之后,它仍然无法填充子节点。

我定义了 2 个模型,具有“hasMany”关系的父节点指代子模型,而具有“BelongsTo”关系的子节点指代父模型

我正在使用带有 JSON 阅读器的 Ajax 代理。

在网上搜索我找不到太多信息,我使用了 extJS 文档中的 user-orderitems-products 示例来尝试设置我的模型和树。

我不完全确定我错过了什么。任何帮助将不胜感激。

JSON(人可能有也可能没有子对象):

People: {
 {firstName: john, id:123, uniqueID:1231, leaf:true},
 {firstName: jane, id:124, uniqueID:1240, 
  offspring:[
    {firstName: adam,    id:124,  uniqueID:1241, leaf:true},
    {firstName: brandon, id:124,  uniqueID:1242, leaf:true},
    {firstName: claire,  id:1243, uniqueID:1243, leaf:true}
 ]}
}

模型:

Ext.define('Person',{
  extend: 'Ext.data.Model',
  fields: [
    {name: 'firstName', type:'string'},
    {name: 'uniqueID',  type:'float'}
    hasMany: {
       model:'Offspring', 
       name: 'Offspring', 
       associationKey: 'offspring',
       primaryKey: 'uniqueID',
       foreignKey: 'id'
    }
  ],
  proxy: {
   type: 'rest',
   url: 'http://blah/blah',
   reader: {
     type: 'json',
     root: 'People'
   }

  }
});

Ext.define('Offspring',{
  extend: 'Ext.data.Model',
  fields: [
    {name: 'firstName', type:'string'},
    {name: 'uniqueID',  type:'float'}
 ],
 belongsTo: 'Person'
});

店铺定义:

var store = Ext.create('Ext.data.TreeStore', {
  model: 'Person',
  folderSort: true
}
4

2 回答 2

2

我怀疑您可能会将简单的父子关系与 hasMany 关系混淆。

但是对于你原来的问题。您正在返回一个不是叶子但没有返回子节点的节点 (jane)。由于您的代理根PersonPeople,children 属性也应该是people

我相信以下 JSON 对您有用:

People: {
    {firstName: john, id:123, uniqueID:1231, leaf:true},
    {firstName: jane, id:124, uniqueID:1240, 
    People:[
        {firstName: adam,    id:124,  uniqueID:1241, leaf:true},
        {firstName: brandon, id:124,  uniqueID:1242, leaf:true},
        {firstName: claire,  id:1243, uniqueID:1243, leaf:true}
    ]}
}

这是一个工作代码:

模型:

Ext.define('BS.model.ItemCategory', {
    extend: 'Ext.data.Model',
    fields: [
        {name: 'name'     , type: 'string'},
        {name: 'iconCls'  , type: 'string', defaultValue: 'treenode-no-icon'},
        {name: 'leaf'     , type: 'boolean', defaultValue: false},
        {name: 'expanded' , type: 'boolean', defaultValue: true, persist: false},
        {name: 'index'     , type: 'int'},        
    ],

    proxy: {
        type: 'direct',

        api: {
            create:  ItemCategories.Create,
            read:    ItemCategories.Read,
            update:  ItemCategories.Update,
            destroy: ItemCategories.Destroy,
        },
    },

});

店铺:

Ext.define('BS.store.ItemCategories', {

    extend: 'Ext.data.TreeStore',    
    model: 'BS.model.ItemCategory',

    autoSync: true,

    root: {
        text: 'Root',
        id: 'NULL',
        expanded: true
    },

    clearOnLoad: true,
});

JSON:

"result":{
  "success":true,
  "children":[
     {
        "id":"1",
        "parentId":null,
        "name":"DFM",
        "index":"0",
        "deleted":"0",
        "children":[
           {
              "id":"6",
              "parentId":"1",
              "name":"Studios",
              "index":"0",
              "deleted":"0",
              "loaded":true,
              "leaf":false
           },
           {
              "id":"7",
              "parentId":"1",
              "name":"Equipment",
              "index":"1",
              "deleted":"0",
              "children":[
                 {
                    "id":"18",
                    "parentId":"7",
                    "name":"Cameras",
                    "index":"0",
                    "deleted":"0",
                    "loaded":true,
                    "leaf":false
                 },
                 {
                    "id":"20",
                    "parentId":"7",
                    "name":"Tripods",
                    "index":"1",
                    "deleted":"0",
                    "loaded":true,
                    "leaf":false
                 },
                 {
                    "id":"26",
                    "parentId":"7",
                    "name":"Lighting Kits",
                    "index":"2",
                    "deleted":"0",
                    "loaded":true,
                    "leaf":false
                 }
              ],
              "leaf":false
           }
        ],
        "leaf":false
     },
     {
        "id":"3",
        "parentId":null,
        "name":"3D",
        "index":"2",
        "deleted":"0",
        "loaded":true,
        "leaf":false
     }
  ]
}
于 2012-06-29T01:37:03.773 回答
1

在 SDK 下载中有一个完全这样的例子:http: //dev.sencha.com/deploy/ext-4.1.0-gpl/examples/tree/treegrid.html

于 2012-06-28T23:58:24.257 回答