1

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

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

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

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

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

更新:

节点是通过 web 服务动态创建的,例如

 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”开头的节点有没有办法让它运行?

4

2 回答 2

0

jsTree 非常非常好,但它的文档确实有一些不足之处。我也为此苦苦挣扎了一段时间,最后想出了这个 - 选择性地扩展/收缩 jsTree 节点

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

于 2013-02-18T09:59:52.053 回答
0

生成 json 时,您可以仅"state" => "open"将其用于要在加载时打开的节点。所以逻辑在生成 json 而不是 jsTree 本身的代码中。

阅读更多关于 json_data 插件的文档

以 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 */ ]
}
于 2013-02-19T21:48:34.247 回答