jstree、json、ajax:递归搜索和选择

发布于 2025-01-02 16:36:24 字数 1766 浏览 1 评论 0原文

我有 jstree 设置来通过 ajax 和 json 加载所有内容。脚本是否可以执行我的 ajax/json url 并在可能的情况下继续进行子搜索,如果它发现它打开所有子节点直到其可见并选择它(.jstree-单击)。如果不知道我可以从哪里开始,这样的功能是内置的吗?我对 javascript 不是最熟练的。 这是我的设置:

$("#jstFormMirror").jstree({
    "types" : { 
        "types" : { 
            "default" : { 
                "select_node" : function(e) {
                    this.toggle_node(e);
                    return true;
                } 
            }
        } 
    },
    "ui" : {
        "select_limit" : 1,
        "selected_parent_close" : "select_parent"
    },      
    "json_data" : {
        "progressive_unload" : true,
        "ajax" : {
            "url" : "/back-end/json/categories.json",
            "data" : function (n) {
                return { id : n.attr ? n.attr("id") : 0 }; 
            }
        }
    },
    "plugins" : [ "themes", "json_data", "types", "ui" ],
    "core": {
        "animation": 100
    }
}).bind("select_node.jstree", function(event, data) { 
    $("#category").val($(".jstree-clicked").parent().attr("rel"));
})

当脚本加载节点时,他是一个典型的 json 响应:

[
    {
        "attr": {
            "id": "87"
        },
        "data": {
            "title": "Bevel Clusters-Over 350 Designs",
            "attr": {
                "href": "#",
                "rel": "658"
            }
        },
        "state": "closed"
    },
    {
        "attr": {
            "id": "394"
        },
        "data": {
            "title": "Bevels, Straight Lines-Over 210 Shapes & Sizes",
            "attr": {
                "href": "#",
                "rel": "321"
            }
        },
        "state": "closed"
    }
]

我想执行:

$("#jstFormCategories").jstree("search", ID_HERE);

I have jstree setup to load all content via ajax and json. Is it possible for the script to execute my ajax/json url and keep sub-searching when possible, if it finds it open all sub nodes until its visible and select it (.jstree-clicked). And is such a feature built in, if not know any where I could start with this? I am not the most fluent at javascript.
Here is my setup:

$("#jstFormMirror").jstree({
    "types" : { 
        "types" : { 
            "default" : { 
                "select_node" : function(e) {
                    this.toggle_node(e);
                    return true;
                } 
            }
        } 
    },
    "ui" : {
        "select_limit" : 1,
        "selected_parent_close" : "select_parent"
    },      
    "json_data" : {
        "progressive_unload" : true,
        "ajax" : {
            "url" : "/back-end/json/categories.json",
            "data" : function (n) {
                return { id : n.attr ? n.attr("id") : 0 }; 
            }
        }
    },
    "plugins" : [ "themes", "json_data", "types", "ui" ],
    "core": {
        "animation": 100
    }
}).bind("select_node.jstree", function(event, data) { 
    $("#category").val($(".jstree-clicked").parent().attr("rel"));
})

And he is a typical json response when the script loads a node:

[
    {
        "attr": {
            "id": "87"
        },
        "data": {
            "title": "Bevel Clusters-Over 350 Designs",
            "attr": {
                "href": "#",
                "rel": "658"
            }
        },
        "state": "closed"
    },
    {
        "attr": {
            "id": "394"
        },
        "data": {
            "title": "Bevels, Straight Lines-Over 210 Shapes & Sizes",
            "attr": {
                "href": "#",
                "rel": "321"
            }
        },
        "state": "closed"
    }
]

I want to execute:

$("#jstFormCategories").jstree("search", ID_HERE);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万水千山粽是情ミ 2025-01-09 16:36:24

在这方面,DefiantJS 非常出色 (http://defiantjs.com)。
该库使用“搜索”方法扩展了全局对象 JSON,并使您能够使用 XPath 表达式搜索 JSON 结构。

要了解 XPath(一种标准化查询语言)有多么简单,请查看此工具;
http://www.defiantjs.com/#xpath_evaluator

这是一个工作小提琴;
http://jsfiddle.net/hbi99/vV43F/

var data = [
     {
        "attr": {
           "id": "87"
        },
        "data": {
           "title": "Bevel Clusters-Over 350 Designs",
           "attr": {
              "href": "#",
              "rel": "658"
           }
        },
        "state": "closed"
     },
     {
        "attr": {
           "id": "394"
        },
        "data": {
           "title": "Bevels, Straight Lines-Over 210 Shapes & Sizes",
           "attr": {
              "href": "#",
              "rel": "321"
           }
        },
        "state": "closed"
     }
  ],
  res = JSON.search( data, '//*[id=394]/../data' );

console.log( res[0].title );

For this, DefiantJS is excellent (http://defiantjs.com).
This lib extends the global object JSON with the method "search" and enables you to search a JSON structure with XPath expressions.

To understand how easy XPath (which is a standardised query language) is, check out this tool;
http://www.defiantjs.com/#xpath_evaluator

Here is a working fiddle;
http://jsfiddle.net/hbi99/vV43F/

var data = [
     {
        "attr": {
           "id": "87"
        },
        "data": {
           "title": "Bevel Clusters-Over 350 Designs",
           "attr": {
              "href": "#",
              "rel": "658"
           }
        },
        "state": "closed"
     },
     {
        "attr": {
           "id": "394"
        },
        "data": {
           "title": "Bevels, Straight Lines-Over 210 Shapes & Sizes",
           "attr": {
              "href": "#",
              "rel": "321"
           }
        },
        "state": "closed"
     }
  ],
  res = JSON.search( data, '//*[id=394]/../data' );

console.log( res[0].title );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文