【问题标题】:Sorting whole fancytree by title fails按标题对整个花式树进行排序失败
【发布时间】:2016-02-01 15:10:06
【问题描述】:

我的页面上有一个 js 代码,它通过单独的负载从服务器获取 JSON 层次结构,并在页面上构建可视化的 Fancytree 层次结构(我从示例中删除了 html 页面部分)。

  $(function() {
    $("#tree").fancytree({
      source: {
        url: '/products/ftree/?format=json',
        cache: false,
        datatype: 'json',
      },
      checkbox: false,
    })

    sortProducts();
    console.log('sorted');
  });

  function sortProducts(){
    console.log('in sortProducts()');
    var tree = $("#tree").fancytree("getTree");
    tree.rootNode.sortChildren(null, true);
    tree.visit( function(node) {
      node.sortChildren(null, false);
      return true;
    });
    tree.render(true, true);
    console.log('end of sort func');
  }
 .
 .
 .
 <div id='tree' ></div>

每个节点都有类似的标题

   1 Fooo
   2 Bar
   3 FoooBar
   3-1 BarFooo
   3-1-1 foofoofoo
   3-1-2 barbarbar
   4 Buuu
   4-1 BuuuBooo
   4-1-1 bubobubub

等等。页面加载正常,它正确呈现树,并且大多数节点的顺序正确,但在更深的部分,4-2 位于 4-1 之前,依此类推。所以我尝试用 .sortChildren(null, true) 之类的方法对它们进行排序,但它不会对它们进行排序。我得到了代码,它没有给出任何错误并且打印调试很好,但是树没有正确排序。

请注意,代码 sn-p 有两次尝试,通过 rootNodenode traverse,我已经单独尝试和一起尝试并获得相同的部分未排序树。在我的理解中,sortChildren() 应该对标题字符串进行排序而不覆盖默认的 cmp 函数。

知道这有什么问题吗?

【问题讨论】:

    标签: dynatree fancytree


    【解决方案1】:

    这段代码没问题

    var node = $("#tree").fancytree("getRootNode");
    node.sortChildren(null, true);
    

    但问题是您尝试对空树进行排序,因为异步请求尚未返回。
    一种解决方案是在 init 事件中调用它:

    $("#tree").fancytree({
        ...
        init: function(event, data) {
          data.tree.getRootNode().sortChildren(null, true);
        },
        ...
    

    【讨论】:

    • 有效!非常感谢!我已经为此奋斗了很多小时。我可以在哪里阅读更多关于这个请求序列/背景的信息,以便我将来了解整个情况?
    • 太棒了!欢迎来到stackoverflow;如果答案对你有用,应该“接受”它,...
    • 我不能接受它,因为我错过了一些“业力”。
    • 获得了一些声誉,现在完成了。我希望你没有屏住呼吸。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    • 1970-01-01
    • 1970-01-01
    • 2021-05-23
    • 1970-01-01
    • 2014-10-26
    相关资源
    最近更新 更多