我正在寻找一种递归扩展树节点的方法。该树尚未完全加载。我们一次只加载一层深度。因此,例如,如果我的路径有 3 个深度级别 (/nodeId1/nodeId14/nodeId142),我想加载第一个节点,然后通过路径中的第二个 ID 获取 2 级节点(在本例中为 nodeId14)展开它,然后得到第三个等等。
但是,当展开节点时,代理会进行 AJAX 调用来获取该节点子节点的数据,并且由于此调用是异步的,因此程序本身会在请求完成之前尝试继续展开下一级节点。是时候给我一个“节点未定义错误”了,因为级别 2 尚未加载。
我已经搜索了 1 天来解决这个问题,但没有任何帮助。我发现一个博客解决了同样的问题,但该帖子是 2009 年的,他使用的一些东西已被弃用。
http://hamisageek.blogspot.gr/2009/04/extjs-tip-recusively-opening-nodes-in.html
一些有帮助的代码:
Ext.define('treeStore',
{
extend : 'Ext.data.TreeStore',
alias: 'widget.treeStore',
autoLoad : false,
model : 'treeModel',
root : {
id: 0,
name : 'Root',
expanded : true,
loaded: true
},
proxy : {
type : 'ajax',
url : 'MyServlet',
reader : {
type : 'json',
root : 'children'
}
},
folderSort: true
});
Ext.define('Ext.tree.Panel',{
.
.
.
//Stuff about the tree panel etc.
dockedItems: {
xtype: 'textfield',
name: 'Search',
allowBlank: true,
enableKeys: true,
listeners: {
specialkey: function (txtField, e) {
if (e.getKey() == e.ENTER){
var searchValue = txtField.getValue();
Ext.Ajax.request({
url: 'MyServlet',
params: {
caseType: 'search',
value: searchValue
},
success: function(response) { //ATTENTION: When calling the .expand() the AJAX hasn't finished and cannot find the node.
response = Ext.decode(response.responseText);
var panel = txtField.up();
response.IDs.forEach(function(entry){
panel.getStore().getNodeById(entry.folderId).expand(); <-problem here
});
}
});
}
}
}
}
我尝试过向expand()回调添加函数,我尝试过delayedtask,我尝试过setTimer等,但没有任何作用。我真的没有选择,这看起来是一件很简单的事情,但它让我发疯。
最佳答案
如果您有要扩展的节点的路径,例如从先前调用 getPath 保存的路径
var path = node.getPath();
那么任务就很简单了:
tree.expandPath(path);
您可以在 http://docs.sencha.com/extjs/4.2.2/extjs-build/examples/build/KitchenSink/ext-theme-neptune/#tree-reorder 测试此方法通过在控制台中输入:
Ext.ComponentQuery.query('treepanel[title=Files]')[0].expandPath('/Ext JS/app/domain', 'text');
如果您没有路径,但在父级展开(加载)后决定扩展哪个子级,则任务会有点麻烦。在这种情况下,您可能会使用回调函数进行扩展,并继续在回调中扩展子级。
参见node.expand了解详情
关于recursion - ExtJS中递归扩展树节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23826387/