ExtJS 4为TreeStore创建节点

时间:2013-08-30 13:58:57

标签: javascript extjs tree extjs4

我试图用ExtJs 4构建TreeGrid。 我有一个servlet,它返回我的大对象,我想将这个JSON数据划分为不同的树,并将它们显示在不同的Ext.Tree.Panels上。 我不希望服务器端有多个查询来获取每个树的数据,因为逻辑上它是一个对象。 对象看起来像这样。

root : {
 ...
 some_meta_data_fields here..
 ..
 tree1 :{ 
  a : {
     data:..
     children: [..]
   },
  b : {
     data:..
     children: [..]
   }
  ...more childs here...
 },
 tree2 : {
  ..another tree...
 }
}

因此,tree1用于一个Tree.Panel,tree2用于另一个,并且所有与服务器的同步都应该是集中的。

我从服务器获取数据,如Ext.Ajax(..),并使用不同的根参数创建了不同的Tree.Store,如:

 var store = Ext.create('Ext.data.TreeStore', {
                model: 'model',
                root : root.tree1,
                proxy  : {
                   type: 'memory',
                   ....JSON reader and other.
                 }
                }
             });

但是,当这样的商店负载转换原始对象(分离叶子为exapmle),这是不可接受的。 我想我应该像这样手动填充树存储:

 var store = Ext.create('Ext.data.TreeStore', {
             root : {name: "proxyRoot"},
            model: 'model',
            load : function(node){
                var that = this;

                Ext.each(a.children, function(child, index) {


                    var node = that.getRootNode().appendChild({
                            name : child.name,
                            val : child.val
                        });
                        Ext.each(node.children, function(leaf, index) {
                            node.appendChild({
                                name : leaf.name,
                                val : leaf.val
                            });
                        })
                    });
                    console.log(that);

                }
             });

树加载后,它只显示无法展开的根,但在控制台中,我可以看到数据已正确加载,并且我在ExtJs商店对象中有所需的层次结构。 我真的坚持这个问题,但我对每棵树进行不同的查询是完全不可接受的。 如果有人可以指出我的解决方案,我会很高兴。 感谢。

0 个答案:

没有答案