需要树商店
问题描述:
ExtJS4需要树商店
我创建了一个TreePanel中的副本
var tree = Ext.create('Ext.tree.TreePanel', <some config>);
tree.store.setRootNode(treeJSON);
现在我想创建一个相同的存储数据,但不同的存储对象另一棵树。如果我这样做:
var tree1 = tree.cloneConfig(<separate listeners>);
然后,它创建一个不同的树。但是两者都有联系。当我折叠或展开一个树节点时,另一个树中的对应节点也具有相似的行为。
存储没有cloneConfig属性,所以我可以复制它。我试图从这个树的JSON重新创建商店。
var store2 = Ext.create('Ext.data.TreeStore', {store: treeJSON});
var tree1 = tree.cloneConfig({store: store2});
我想store2
会从tree
4S店不同。但是由于我使用了相同的treeJSON,问题就在那里。
我可以做的一件事是将JSON转换为字符串,将其解码以创建另一个JSON对象并将其分配给新的商店。这与以前的商店不同。但是,必须有一个快速的方法。
如何创建具有不同存储对象重复树,这样当我展开/折叠一个节点在一棵树,它不展开/折叠以同样的方式在另外一个?
答
我已经做了类似的事情。
解析您的老树,以创建新树
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;
}
答
Ext.data.NodeInterface有方法“复制”与参数“深”,但ExtJS的4.1.3深克隆不起作用了。更详细的:他们只是在调用childNode.clone时忘记传递“id”参数。
对于人们仍然使用ExtJS的< 4.1.3使用它进行的树木深克隆:如果我们有一些内置的功能
/**
* 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);
其更好 – Shashwat 2012-08-14 06:33:30