Javascript appendChild树节点未展开
我有一个树面板,在那里我在初始加载时设置了一些根级别的节点。然后,当打开一个节点时,我有一个javascript函数,在它下面添加子节点 我有一个问题,我无法使节点可扩展。它不显示展开节点的箭头 下面是我添加子节点的方式:Javascript appendChild树节点未展开,javascript,extjs,tree,extjs4.2,treepanel,Javascript,Extjs,Tree,Extjs4.2,Treepanel,我有一个树面板,在那里我在初始加载时设置了一些根级别的节点。然后,当打开一个节点时,我有一个javascript函数,在它下面添加子节点 我有一个问题,我无法使节点可扩展。它不显示展开节点的箭头 下面是我添加子节点的方式: var onbeforeitemexpand = function(node, options) { if (!node.hasChildNodes()) { node.appendChild({ title:'Should n
var onbeforeitemexpand = function(node, options) {
if (!node.hasChildNodes()) {
node.appendChild({
title:'Should not be expandable',
checked: false,
leaf:true
});
node.appendChild({
title:'Should expand',
checked: false,
leaf:false,
expandable:true
});
};
tree.on('beforeitemexpand', onbeforeitemexpand, null);
但是第二个孩子是不可扩展的。要使其可扩展,唯一的方法是向其添加空子对象,如下所示:
node.appendChild({
title:'Should expand',
checked: false,
leaf:false,
expandable:true,
children:[{}]
});
var onbeforeitemexpand = function(node, options) {
var firstChild = node.getChildAt(0);
if (firstChild.get('tempitem')) {
node.removeChild(firstChild, true);
}
node.store.load({ node: node });
};
tree.on('checkchange', oncheckchange, null);
tree.on('beforeitemexpand', onbeforeitemexpand, null);
但是在它下面会有一个空节点,当我尝试在这个节点下添加子节点时,当它展开时,子节点将进入空节点
是否有更好的方法通过我自己的javascript函数(不是通过ajax请求…)来“延迟加载”树?无论我尝试了什么,当我通过函数向树中添加节点时,我都无法将>箭头指向节点。我尝试了上述解决方案和自定义代理读取器。。我终于解决了这个问题:
Ext.define('Ext.proxy.DocumentNodes', {
extend: 'Ext.data.proxy.Memory',
alias: 'proxy.documentnodes',
read: function(operation, callback, scope) {
var parent=operation.node;
var children=[];
children.push(
Ext.create('Document',{
title:'Some document',
iconCls:'task-folder',
checked: false,
leaf: false,
expandable: true,
children: [{tempitem: true}]
})
);
var result=Ext.create("Ext.data.ResultSet",{records: children});
Ext.apply(operation, {resultSet: result});
operation.setCompleted();
operation.setSuccessful();
Ext.callback(callback, scope || this, [operation]);
}
});
Ext.define('Document', {
extend: 'Ext.data.Model',
fields: [
{name: 'title', type: 'string'},
]
});
var store = Ext.create('Ext.data.TreeStore', {
model: 'Document',
proxy: {
type: 'documentnodes'
},
nextId: 1,
folderSort: false
});
然后将该存储用于树面板,并将onbeforeitemexpand listener用于树,如下所示:
node.appendChild({
title:'Should expand',
checked: false,
leaf:false,
expandable:true,
children:[{}]
});
var onbeforeitemexpand = function(node, options) {
var firstChild = node.getChildAt(0);
if (firstChild.get('tempitem')) {
node.removeChild(firstChild, true);
}
node.store.load({ node: node });
};
tree.on('checkchange', oncheckchange, null);
tree.on('beforeitemexpand', onbeforeitemexpand, null);
这一切所做的是,首先代理读取器创建节点并在其下方添加一个空节点,以便>箭头显示在节点之前。然后,在beforeitemexpand中,它检查第一个节点是否为临时节点,并将其删除,并为该节点调用重新加载,然后执行代理读取器,该读取器在已打开的节点下添加新节点,并在其下添加临时节点
这样做看起来很愚蠢,但它是有效的,我不知道如果节点中没有任何子节点,但leaf设置为false,expandable设置为true,如何才能使>箭头显示在节点之前
也许有人对此有更好的答案