javascript - appendChild树节点不展开

标签 javascript extjs tree extjs4.2 treepanel

我有一个树面板,我在初始加载时设置了一些根级别节点。然后,当打开一个节点时,我有一个 JavaScript 函数,可以在其下添加子节点。

我遇到一个问题,无法使节点可扩展。它不显示展开节点的箭头。

以下是添加子节点的方法:

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);

但是第二个 child 是不可扩展的。我可以让它可扩展的唯一方法是向其中添加空子项,如下所示:

        node.appendChild({
            title:'Should expand',
            checked: false,
            leaf:false,
            expandable:true,
            children:[{}]
        });

但是它下面会有一个空节点,当我尝试在该节点展开时在该节点下添加子节点时,子节点将进入空节点内部。

有没有更好的方法通过我自己的 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 监听器,如下所示:

        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 中检查第一个节点是否是临时节点并将其删除并调用该节点的重新加载,然后执行代理读取器,该代理读取器在已打开的节点下方添加新节点,并在其下方添加一个临时节点...

看起来是很愚蠢的方法,但它有效,而且我不知道如果节点中没有任何子节点但叶子设置为,我不知道如何才能让 > 箭头出现在节点之前false 且可扩展设置为 true。

也许有人对此有更好的答案。

关于javascript - appendChild树节点不展开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26103230/

相关文章:

tree - 实现可变树结构

c - 如何统计树遍历的函数调用次数

javascript - 导航栏在滚动上显示/隐藏,如脉冲淡入淡出效果

javascript - 如果未单击按钮如何运行函数?

extjs - 从组合框中删除输入光标

javascript - Extjs Combo 显示值 - 如果未找到值

Java:通过延迟删除从 JTree 中剪切项目

javascript - AWS S3 CORS上传-文件大小控制

javascript - 当动态包含 js 文件时,LinkedIn 的社交共享链接不起作用

javascript - 何时重写 UI/javascript 或支付商业许可费用?