【问题标题】:Need a copy of the tree store需要树商店的副本
【发布时间】:2012-07-27 23:55:58
【问题描述】:

ExtJS4

我创建了一个 TreePanel 作为

var tree = Ext.create('Ext.tree.TreePanel', <some config>);
tree.store.setRootNode(treeJSON);

现在我想创建另一个具有相同商店数据但不同商店对象的树。如果我这样做:

var tree1 = tree.cloneConfig(<separate listeners>);

然后,它会创建一棵不同的树。但两者仍然是联系在一起的。当我折叠或展开一个树节点时,另一棵树中的相应节点也表现类似。

store 也没有 cloneConfig 属性,所以我可以复制它。我尝试从 JSON 为这棵树重新创建存储。

var store2 = Ext.create('Ext.data.TreeStore', {store: treeJSON});
var tree1 = tree.cloneConfig({store: store2});

我认为store2trees 商店不同。但是问题就在那里,因为我使用的是相同的 treeJSON。

我可以做的一件事是将 JSON 转换为字符串,对其进行解码以创建另一个 JSON 对象并将其分配给新存储。那将与以前的商店不同。但必须有一种快速的方法。

如何创建具有不同存储对象的重复树,以便当我展开/折叠树中的一个节点时,它不会以相同的方式在另一个节点中展开/折叠?

【问题讨论】:

    标签: extjs4 clone store treepanel


    【解决方案1】:

    我做过类似的事情。

    解析旧树以创建新树

    var root = existingTree.getRootNode ();
    if (root) {
      var rootNode = this.getClonedTreeRoot(root);
      newTree.store.setRootNode (rootNode);
    }
    
    
    getClonedTreeRoot: function (node) {
    
      var me = this;
      var rootData;
      var childData = [];
    
      if (node.hasChildNodes ()) {
        var childNodes = node.childNodes;
        Ext.Array.each (childNodes, function (child) {
           if (child.get ('checked')) {
        childData.push (me.getClonedTreeRoot(child));           
           }        
        });
      }
    
      if (node.isLeaf ()) {
        rootData = {
            "text" : node.get ('text'),
            "leaf" : true,
            "expanded" : false,
            "children" : childData
        };
      } else {
        rootData = {
            "text" : node.get ('text'),
            "leaf" : false,
            "expanded" : true,
            "children" : childData
         };
       }
    
       return rootData;
    }
    

    【讨论】:

    • 如果我们有一些内置函数会更好
    【解决方案2】:

    Ext.data.NodeInterface 有方法“copy”,参数为“deep”,但在 ExtJs 4.1.3 之前,深度克隆不起作用。更详细:他们只是在调用 childNode.clone 时忘记传递“id”参数。

    对于仍在使用 ExtJS

    /**
     * Because of a bug in Ext.data.NoteInterface in ExtJs < 4.1.3
     * we have to do deep cloning.
     */
    var clone = function(node) {
      var result = node.copy(),
          len = node.childNodes ? node.childNodes.length : 0,
          i;
      // Move child nodes across to the copy if required
      for (i = 0; i < len; i++)
        result.appendChild(clone(node.childNodes[i]));
      return result;
    };
    
    var oldRoot = store1.getRootNode(),
        newRoot = clone(oldRoot);
    
    store2.setRootNode(newRoot);
    

    【讨论】:

      猜你喜欢
      • 2015-10-13
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      • 1970-01-01
      • 2011-09-21
      • 2019-07-18
      • 2012-06-10
      • 1970-01-01
      相关资源
      最近更新 更多