jstree jquery如何遍历所有节点 [英] jstree jquery how to iterate through all nodes

查看:1028
本文介绍了jstree jquery如何遍历所有节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遍历jstree中树视图中的每个节点。树视图有4层深,但我似乎无法超越第1层。以下是用于迭代的jQuery。

I'm trying to iterate through every node within a treeview in jstree. The treeview is 4 levels deep but I can't seem to get past the 1st level. The following is the jQuery used to iterate.

$("#myTree").bind('ready.jstree', function (event, data) {
    $('#myTree li').each(function () {
        // Perform logic here
        }
    });
});

这里是一个说明我的观点的小说。请帮助我如何迭代jstree中的每个节点。

Here is a jsfiddle illustrating my point. Please help on how I can iterate through every node in jstree.

推荐答案

这会使你的树的所有孩子都在一个平面阵列中为你的 .each 循环。

This gets all the children of your tree in a flat array for your .each loop.

$("#tree").bind('ready.jstree', function(event, data) {
  var $tree = $(this);
  $($tree.jstree().get_json($tree, {
      flat: true
    }))
    .each(function(index, value) {
      var node = $("#tree").jstree().get_node(this.id);
      var lvl = node.parents.length;
      var idx = index;
      console.log('node index = ' + idx + ' level = ' + lvl);
    });
});

JSFiddle - get_json的文档

这篇关于jstree jquery如何遍历所有节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆