【发布时间】:2026-01-18 09:55:01
【问题描述】:
我的树面板上有一个搜索栏。当我写一些东西并按下回车键时,一个 Ajax 请求会触发并返回将树展开到请求的文件夹点所需的文件夹 ID。在 ajax.request 的成功配置中,我使用循环通过 getNodeById 调用每个节点的扩展函数。然而,在第一次扩展后,ExtJS 会触发来自代理的 ajax 请求以获取文件夹数据(因为它尚未加载)。由于 AJAX 是异步的,因此循环比服务器响应更快,它会在节点本身加载之前尝试调用节点的 .expand() 函数并给出未定义的错误。我应该如何解决这个问题?我知道通常使用 AJAX,您必须在处理请求后对要运行的所有内容使用回调函数,但我不确定在这种情况下如何执行此操作...
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()函数本身有一个回调函数。我正在尝试找到正确的方法来利用它。有什么想法吗?
标签: javascript ajax extjs tree extjs4