【问题标题】:jsTree load and clickjsTree 加载并点击
【发布时间】:2011-12-28 13:39:15
【问题描述】:

我有两个关于 jsTree 的问题:

  1. 我正在使用带有 struts2 的 JSON 生成一个 jsTree。第一次正确显示树。但是在按钮上单击我想将具有不同数据的树重新加载到同一个 div 中。在按钮单击上,我给出了与第一次生成 jstree 相同的操作名称。

  2. 如果我单击树的任何节点,那么我想要该节点的 id,如果该节点有任何父节点,那么还想要该节点的父节点 id,父节点有父节点,然后 id父节点也

例子

    Parent 1
       |__Parent 2
            |__Child 1

我点击子 1,然后我想要子 1、父 2、父 1 等的 id。

【问题讨论】:

    标签: javascript jquery jstree


    【解决方案1】:

    我不确定你是否真的浏览过文档,但这里有一些可以帮助你的东西:

    对于您的第一个问题,您可以使用回滚:http://luban.danse.us/jazzclub/javascripts/jquery/jsTree/reference/_examples/5_others.html 刷新也可以是一个选项:http://luban.danse.us/jazzclub/javascripts/jquery/jsTree/reference/_examples/2_operations.html

    对于第二个问题,您可以使用回调:http://luban.danse.us/jazzclub/javascripts/jquery/jsTree/reference/_examples/3_callbacks.html

    ._get_parent 也会帮助你:

    ._get_parent(节点)

    获取表示所传递节点的父节点的 LI 元素。 失败时返回 false。混合节点

    这可以是 DOM 节点、jQuery 节点或指向元素的选择器 在树中,我们想要它的父级。

    查看完整文档了解更多详情:http://www.jstree.com/documentation/core

    【讨论】:

      【解决方案2】:

      如果您想获取节点的完整路径,可以使用 get_path

      // The true means it will return the ID's of the parent, therefore ALL NODES need an ID
      var parents = $("#your_jstree").jstree("get_path",$("#the_node"),true); 
      $.each(parents, function(k, v){
          // Log down the ID's
          console.log(v);
      });
      

      完整示例

      var ids = $(this).closest(".jstree").jstree("get_path", $(this), true);
      var path = "";
      $.each(ids, function (k, v) {
          if (k == 0) return;
      
          if (path != "")
              path += "/";
      
          path += clone.html();
      });
      

      将返回 /parent 1/parent 2/child 1

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多