节点不会使用lazyLoad回调中的数据进行扩展

时间:2017-07-18 17:56:48

标签: jquery fancytree

我遇到的问题是我从ajax调用返回JSON数据,但是当我将数据设置为等于lazyLoad回调中的data.result时,数据不会插入到树中。

    var values = [];
    //This call returns a var 'value' which contains the data I want to display.
    CallToGetData(databaseTree).done(function (value) {
    values = value;
    var treeStruc = [];
    //Grab only the necessary info needed and push each objective into an array.
    for (var j = 0; j < values.length; j++) {
        var obj = { title: values[j].Path, key: values[j].Id, lazy: true }; //folder: true, children: []
        treeStruc.push(obj);
    }
    $(function () {
         $("#tree").fancytree({
             checkbox: true,
             selectMode: 3,
             source: treeStruc, //Set the array above to the source.
             lazyLoad: function (event, data) {
                  data.result = [];
                  //This call returns a var 'value' which contains the data I want to display.
                  CallToGetData(databaseTree, data.node.title).done(function (value) {
                      var lazyNodeValues = [];
                      for (var l = 0; l < value.length; l++) {
                          var obj = { title: value[l].Path, key: value[l].Id, lazy: true }; //folder: true
                          lazyNodeValues.push(obj);
                      }
                      //Currently lazyNodeValues contains to objects that I want to display.
                      data.result = lazyNodeValues;//THIS ASSIGNMENT IS WHAT ISN'T WORKING.
                  });
             },
             activate: function (event, data) {
             $("#statusLine").text("Active node: " + data.node);
             }
        });
     });//End of $function       
});//End of CallToGetData

在树的初始填充中,var'treeStruc'包含一个格式为的对象数组:

标题:'我设置的价值',关键:'我设置的ID',懒惰:真实。

这完全填满了树。当我单击延迟加载时,我再次调用以获取更多JSON数据来填充子节点。它们的结构完全相同,但是当我单击一行时,我选择的行下面没有任何内容。有什么东西我错过了吗?任何帮助都会很棒!

1 个答案:

答案 0 :(得分:0)

从文档中我发现了这一点。创建延迟对象,将data.result设置为promise,go获取要嵌套在子项中的数据,并解析要放在子项中的数据。

var dfd = new $.Deferred();
data.result = dfd.promise();

CallToGetData(data.node.title).done(function (value) {
      var lazyNodeValues = [];

      for (var l = 0; l < value.length; l++) {
      var obj = { title: value[l].Path, key: value[l].Id, lazy: true }; 
      lazyNodeValues.push(obj);
      }
      dfd.resolve(lazyNodeValues);
});