Sencha Touch 2.4:如何以编程方式向TreeStore添加多个记录

时间:2017-01-04 15:50:06

标签: extjs sencha-touch-2

我在理解如何以编程方式将数据添加到已初始化的TreeStore时遇到了麻烦。

考虑来自Sencha Touch文档的示例:

  var data = {
      "items" : [{
            "text" : "Today",
            "items" : [{
                        "text" : "Eat",
                        "leaf" : true
                    }, {
                        "text" : "Sleep",
                        "leaf" : true
                    }, {
                        "text" : "Drinking",
                        "leaf" : true
                    }]
        }, {
            "text" : "Tomorrow",
            "items" : [{
                        "text" : "Watch TV",
                        "leaf" : true
                    }, {
                        "text" : "Watch Video",
                        "leaf" : true
                    }]
        }, {
            "text" : "This week",
            "items" : [{
                        "text" : "Shopping",
                        "leaf" : true
                    }]
        }, {
            "text" : "Later",
            "items" : [{
                        "text" : "Eat",
                        "leaf" : true
                    }, {
                        "text" : "Sleep",
                        "leaf" : true
                    }, {
                        "text" : "Drinking",
                        "leaf" : true
                    }]
        }]
   };

   Ext.define('Task', {
       extend: 'Ext.data.Model',
       config: {
           fields: [{
               name: 'text',
               type: 'string'
           }]
       }
   });

   var store = Ext.create('Ext.data.TreeStore', {
       model: 'Task',
       defaultRootProperty: 'items',
       //root: data
   });

   store.addData(data.items);  // I need to add data here

   console.log(store.getAllCount()) // prints 4 but should be 13

结果我只添加了4个父项。没有孩子。

提前致谢。

1 个答案:

答案 0 :(得分:0)

您只需添加树的第一级。 ExtJS正在寻找孩子来在内部制作他的树。

在您的JSON中将items改为children

var data = {
      "items" : [{
            "text" : "Today",
            "children" : [{                 // <<== HERE
                        "text" : "Eat",
                        "leaf" : true
                    }, {
                        "text" : "Sleep",
                        "leaf" : true
                    }, {
                        "text" : "Drinking",
                        "leaf" : true
                    }]
        }, {
            "text" : "Tomorrow",
            "children" : [{               // <<== AND HERE

            // etc, ...