打开具有以特定单词开头的id的树节点

时间:2013-02-18 09:38:25

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

我正在使用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'开头的所有节点 有没有办法让它运作?

2 个答案:

答案 0 :(得分:0)

jsTree非常好,但它的文档确实有点不尽如人意。我也忍受了一段时间的努力,终于想出了这个 - selectively expanding/cotracting jsTree nodes

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

答案 1 :(得分:0)

生成json时,您可以只使用"state" => "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 */ ]
}