我打电话时正在使用tree.Panel和TreeStore
sel_node.parentNode.appendChild(node);
tree.getSampleStore().sync();
Run Code Online (Sandbox Code Playgroud)
ExtJS fire事件称为样本存储代理:更新url而不是create url我可能做错了什么?
您使用的 ExtJS4 的具体版本是什么?
在我的情况下,使用 ext-4.0.7-gpl,我进行了一些调试,发现该appendChild方法从 和 对象创建一个节点,然后执行一些有关节点在树中的位置的更新操作,例如设置下一个同级、父级等。 ,参见[1]。
同步时,存储使用getUpdatedRecords和getNewRecords[2] 方法来确定要运行哪个操作。更新或创建。不知何故,我们附加的孩子最终被更新,而不是被创建。
请注意,该方法不会检查父节点的子节点是否已加载,只是将新节点推送到空数组中childNodes;所有这些操作结束后,父节点的其他子节点永远不会显示在树中;如果更新操作导致服务器端生成 new id,则代码会中断-客户端上original = me.tree.getNodeById(record.getId());不存在这样的节点,而旧节点会生成。id
简而言之,我认为这是一个错误。
[1] http://docs.sencha.com/ext-js/4-0/source/NodeInterface.html#Ext-data-NodeInterface-method-appendChild [2] http://docs.sencha.com/ext -js/4-0/source/AbstractStore.html#Ext-data-AbstractStore-method-getUpdatedRecords
添加:ExtJS 4.1 beta 2 对我来说效果不佳
使用临时解决方案更新:我进行了一些修改,认为我通过覆盖下面appendChild的方法解决了问题(只是为了设置属性以便创建记录,而不是更新记录)。NodeInterfacephantom
请注意: 1)您应该将您的appendChild调用包含在NodeInterface expand方法回调中,否则推入空的错误childNodes将仍然存在:新节点将出现在错误的位置;2)我也必须覆盖updateIndexes,AbstractView尽量不要这样做,也许你会找到原因;3)当商店在下次同步时尝试删除我们新创建的节点时,会出现一些问题 - 尚无法跟踪它;0)我不是 ExtJS 甚至 JS 大师,所以请随时纠正这个 hack)
Ext.data.NodeInterface.oldGpv = Ext.data.NodeInterface.getPrototypeBody;
Ext.data.NodeInterface.getPrototypeBody = function(){
var ret = Ext.data.NodeInterface.oldGpv.apply(this, arguments);
ret.appendChild = function(node, suppressEvents, suppressNodeUpdate) {
var me = this,
i, ln,
index,
oldParent,
ps;
if (Ext.isArray(node)) {
for (i = 0, ln = node.length; i < ln; i++) {
me.appendChild(node[i]);
}
} else {
node = me.createNode(node);
if (suppressEvents !== true && me.fireEvent("beforeappend", me, node) === false) {
return false;
}
index = me.childNodes.length;
oldParent = node.parentNode;
if (oldParent) {
if (suppressEvents !== true && node.fireEvent("beforemove", node, oldParent, me, index) === false) {
return false;
}
oldParent.removeChild(node, null, false, true);
}else{
node.phantom = true;
}
if(me.isLoaded()){
index = me.childNodes.length;
if (index === 0) {
me.setFirstChild(node);
}
me.childNodes.push(node);
node.parentNode = me;
node.nextSibling = null;
me.setLastChild(node);
ps = me.childNodes[index - 1];
if (ps) {
node.previousSibling = ps;
ps.nextSibling = node;
ps.updateInfo(suppressNodeUpdate);
} else {
node.previousSibling = null;
}
node.updateInfo(suppressNodeUpdate);
}
//console.log('appendChild was called');
// I don't know what this code mean even given the comment
// in ExtJS native source, commented out
// As soon as we append a child to this node, we are loaded
//if (!me.isLoaded()) {
// me.set('loaded', true);
//}
// If this node didnt have any childnodes before, update myself
//else
//if (me.childNodes.length === 1) {
// me.set('loaded', me.isLoaded());
//}
if (suppressEvents !== true) {
me.fireEvent("append", me, node, index);
if (oldParent) {
node.fireEvent("move", node, oldParent, me, index);
}
}
return node;
}
};
return ret;
};
Run Code Online (Sandbox Code Playgroud)
这是我的代码,用于通过从表单中获取的值添加节点domainForm。actioncolumn单击树形网格中的图标即可打开该表单:
var node = grid.store.getAt(rowIndex);
node.expand(false, function(){
var newDomain = domainForm.getValues();
newDomain.parent = {id: node.raw.id}; // i don't know whether you'll need this
var newNode = node.appendChild(newDomain);
me.store.sync();
});
Run Code Online (Sandbox Code Playgroud)
和updateIndexes覆盖者:
Ext.override(Ext.view.AbstractView, {
updateIndexes : function(startIndex, endIndex) {
var ns = this.all.elements,
records = this.store.getRange(),
i;
startIndex = startIndex || 0;
endIndex = endIndex || ((endIndex === 0) ? 0 : (ns.length < records.length?(ns.length - 1):records.length-1) );
for(i = startIndex; i <= endIndex; i++){
ns[i].viewIndex = i;
ns[i].viewRecordId = records[i].internalId;
if (!ns[i].boundView) {
ns[i].boundView = this.id;
}
}
}
});
Run Code Online (Sandbox Code Playgroud)