javascript - jsTree扩展时如何延迟加载节点?

标签 javascript html lazy-loading jstree

我有这个 js 代码来使用 jsTree 插件创建一棵树。

    var n2 = {
          id          : "B", // will be autogenerated if omitted
          text        : "B", // node text
          icon        : undefined, // string for custom
          state       : {
            opened    : false,  // is the node open
            disabled  : false,  // is the node disabled
            selected  : false  // is the node selected
          },
          children    : [],  // array of strings or objects
          li_attr     : {},  // attributes for the generated LI node
          a_attr      : {},  // attributes for the generated A node 
        };
    var n1 = {
          id          : "A", // will be autogenerated if omitted
          text        : "A", // node text
          icon        : undefined, // string for custom
          state       : {
            opened    : false,  // is the node open
            disabled  : false,  // is the node disabled
            selected  : false  // is the node selected
          },
          children    : [n2],  // array of strings or objects
          li_attr     : {},  // attributes for the generated LI node
          a_attr      : {}  // attributes for the generated A node
        };


    $('#jstree_demo_div').jstree({ 
        'core' : {
            'data' : function (obj, cb) {
                console.log(obj);
                cb.call(this, [n1]);
            }
        }
    }); 

有2个节点,A和B。B是A的子节点。我想将其设置为B是A的子节点,但B仅在A第一次展开时添加到DOM中。

上面的代码不起作用,似乎总是在加载A时加载B。有谁知道该怎么做吗?

谢谢

最佳答案

我明白了。您只需将 children 设置为 true,这意味着它有子项,但它还不知道它们。实际的子项可以存储在 data 属性中。当节点展开时,您只需从 data 获取子节点并将其返回,即可渲染到 DOM 中。数据回调在初始加载时被调用,id将为#,这意味着您需要返回根元素数组。如果不是这样,那么您扩展了一个节点。

    var n2 = {
          id          : "B", // will be autogenerated if omitted
          text        : "B", // node text
          icon        : undefined, // string for custom
          state       : {
            opened    : false,  // is the node open
            disabled  : false,  // is the node disabled
            selected  : false  // is the node selected
          },
          children    : [],  // array of strings or objects
          li_attr     : {},  // attributes for the generated LI node
          a_attr      : {},  // attributes for the generated A node 
        };
    var n1 = {
          id          : "A", // will be autogenerated if omitted
          text        : "A", // node text
          icon        : undefined, // string for custom
          state       : {
            opened    : false,  // is the node open
            disabled  : false,  // is the node disabled
            selected  : false  // is the node selected
          },
          children    : true,  // array of strings or objects
          data : [n2],
          li_attr     : {},  // attributes for the generated LI node
          a_attr      : {}  // attributes for the generated A node
        };
    var root = [n1];

    $('#jstree_demo_div').jstree({ 
        'core' : {
            'data' : function (obj, cb) {
                cb.call(this, obj.id == "#" ? root : obj.data);  
            }
        }
    }); 

关于javascript - jsTree扩展时如何延迟加载节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48361613/

相关文章:

reactjs - Webpack 2 配置用于 React 项目上的 System.import 树摇动和延迟加载

javascript - 将标准右键单击选项添加到自定义右键单击

javascript - 使用javascript的正则表达式删除特殊字符

html - 在 html 表中填充整列

javascript - 如何在 angularjs 中单独将指令附加到 div

orm - 学说2 ArrayCollection过滤方法

java - 使用 jpa 和 hibernate 在 spring 中延迟加载实体

javascript - 如何从外部加载 json 到 D3 应用程序

javascript - 如何分离规则数组并从该数组执行函数

jquery - 如何自动右/左浮动