【问题标题】:open tree nodes with id starting with a particular word打开 id 以特定单词开头的树节点
【发布时间】:2013-02-02 16:49:54
【问题描述】:

我正在使用 jsTree,到目前为止它看起来不错。

我有一个节点列表,其 id 会随着每个新节点(如 (g1,g2,g3 ...以及其他一些节点,如 k1,k2,k3) 而增加)

我可以通过使用在文档加载时打开特定节点

              "core": { 
                "animation": 0,
                "open_parents": true,
                "initially_open": ['g1']
                },

但我想打开所有以 'g' 而不是 'k' 开头的节点, 可以使用 $(id^=g) 之类的东西吗?

更新:

节点是通过网络服务动态创建的,比如

 Dim oSB1 As StringBuilder = New StringBuilder
oSB1.Append(" <h5 >JSTree</h5> <div id='divtree' ><ul id='tree'> <li id='g1'><a       href='#' class='usr'>1st Node</a><ul> <li><a href='#'  rel='file'>1.1</a></li><li><a href='#'  class='usr'>1.2</a></li><li><a href='#'  class='file'>1.3</a></li></ul></li></ul><ul><li id='g2'><a href='#' class='usr'>2nd Node</a><ul> <li><a href='#'  rel='file'>2.1</a></li><li><a href='#' >2.2</a></li></ul></ul> <ul><li id='k2'><a href='#' class='usr'>3rd Node</a><ul> <li><a href='#'  rel='file'>3.1</a></li><li><a href='#' >3.2</a></li></ul></ul> <ul><li id='k2'><a href='#' class='usr'>4th Node</a><ul> <li><a href='#'  rel='file'>4.1</a></li><li><a href='#' >4.2</a></li></ul></ul></div>")
Return oSB1.ToString

从 web 服务返回的数据分配给 jstree,因此我只需要打开 id 以“g”而不是“k”开头的节点,在上面的示例中只有 2 个节点,但想象一下如果有超过 100 个节点。

树是这样称呼的

  $("#G2").html(data.d);
$("#divtree").jstree(
                  {
                     "state": "open",
                      "animated": "slow",
                      "plugins": ["themes", "html_data", "ui", "crrm", "contextmenu"],

                        //working
                      "core": {
                          "animation": 0,
                          "open_parents": true,
                          "initially_open": ['g1']
                      },

                      "contextmenu": {
                          "items": function ($node) {
                              return {
                                  "Create": {
                                      "label": "Create a new Node",
                                      "action": function (obj) {
                                          $("#divtree").jstree("create_node", function () { alert("are you sure?") }, true);
                                          this.create(obj);
                                      }
                                  },
                                  "Rename": {
                                      "label": "Rename Node",
                                      "action": function (obj) {
                                          $("#divtree").jstree("rename_node", function () { alert("you are trying to rename") }, true);
                                          this.rename(obj);

                                      }
                                  },
                                  "Delete": {
                                      "label": "Delete Node",
                                      "action": function (obj) {
                                          $("#divtree").jstree("delete_node", function () { alert("Really!!?") }, true);
                                          this.remove(obj);


                                      }
                                  }
                              };
                          }
                      }

                  });

她只打开 id 'g1' 的节点,而我想打开所有以 id 'g' 开头的节点 有没有办法让它运行起来?

【问题讨论】:

    标签: javascript jquery jquery-plugins jquery-selectors jstree


    【解决方案1】:

    jsTree 非常非常好,但它的文档确实有一些不足之处。我也为此苦苦挣扎了一段时间,终于想出了这个- selectively expanding/cotracting jsTree nodes

    这可能无法直接回答您的具体问题,但我认为它可能会有所帮助。

    【讨论】:

    • 我仍然不确定如何根据我的需要自定义:(
    【解决方案2】:

    生成 json 时,您可以只使用 "state" =&gt; "open" 来表示您希望在加载时打开的节点。所以逻辑在生成json而不是jsTree本身的代码中。

    阅读更多doc for json_data plugin

    以 JSON 格式提供数据时需要遵循的基本结构是:

    {
        "data" : "node_title",  // omit `attr` if not needed; the `attr` object gets passed to the jQuery `attr` function
        "attr" : { "id" : "node_identificator", "some-other-attribute" : "attribute_value" }, // `state` and `children` are only used for NON-leaf nodes
        "state" : "closed", // or "open", defaults to "closed"
        "children" : [ /* an array of child nodes objects */ ]
    }
    

    【讨论】:

    • 还有一件事我忘了说的是节点是在 web 服务中形成的,而不是在 jstree 的脚本中。所以他们的 id 将出现在网络服务中。
    • 如果节点处于close状态,则发送AJAX请求,然后返回数据用于渲染/更新jsTree。所以很简单,如果你使用上述设置。数据操作在服务器端完成。
    • 感谢您的回复,但我在哪里添加“状态”:关闭?我的节点是从网络服务调用的。请查看更新后的问题
    • 我的回答中有一个例子,在示例代码中。即使您在代码中使用了"state": "open",。因此,当您的 Web 服务创建新节点并且您需要为该节点创建 ajax 时,您可以在同一级别添加 "state" : "closed",例如 dataattr。请注意,您可以使用attr 部分添加您自己的属性,然后在创建 AJAX 调用时,您可以将这些属性传递给您的 Web 服务。这里有很多例子stackoverflow.com/questions/8078534/…
    猜你喜欢
    • 1970-01-01
    • 2017-04-13
    • 2021-02-10
    • 2022-10-12
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    • 1970-01-01
    • 2019-10-06
    相关资源
    最近更新 更多