jsTree搜索与阿贾克斯/ JSON不是要求网址网址、不是、jsTree、阿贾克斯

2023-09-10 19:19:16 作者:热情喂冷风

我真的AP preciate一些帮助,以下内容:

I would really appreciate some help with the following:

我有一个jsTree加载JSON数据通过URL。所有的数据是preloaded。

I have a jsTree loaded with JSON data via a URL. All the data is preloaded.

我需要做的是改变搜索功能的重新加载与基于用户输入通过AJAX新的JSON数据的整个树(因为我需要进行更复杂的节点搜索在后端)。

What I need to do is change the search functionality to reload the entire tree with new JSON data via AJAX based on the user input (because I need to do more complex node searching in the backend).

作为一个开始所有我想要做的是让jsTree code打电话给我的网址。我开始为我用最初装载树相​​同的URL。

As a start all I am trying to do is to get the jsTree code to call my URL. I started with the same URL as I use to originally load the tree.

但是 - 这是问题,我无法找到一个解决办法 - 虽然URL被称为成功地第一次加载树,当我输入了一些搜索文本,然后点击搜索,jsTree使用其正常的内部搜索的亮点节点,但我提供的网址永远不会再次调用。

However - and this is the problem for which I cannot find a solution - although the URL is called successfully to first load the tree, when I type some search text and click "Search", jsTree uses its normal internal search to highlight nodes, but the URL I provide is never called again.

的HTML是

<div>
    <form>
        <div>
             <input id="treeSearchText" type="text" />
             <button id="searchTree" class="btn">Search</button>
             <button id="clearSearch" class="btn">Clear</button>
        </div>
    </form>
</div>

<div id="myJsTree" style="height: 100%;"></div>

这是我使用的jsTree初始化:

The jsTree initialization that I am using is:

var url = <my_url>;

$("#myJsTree").jstree({
    "json_data" : {
        async : true,
        "ajax" : {
            "url" : url
        }
     },
    "search": {
        "case_insensitive" : true,
        "ajax" : {
            "url" : url       
        }
    },
    'ui' : {
        'select_limit' : 1,
        'initially_selected' : [${myId}],
    },
    "plugins" : [ "json_data", "search", "sort", "ui", "themeroller" ],
});

和搜索code中的片段:

And the snippet of search code:

$("#searchTree").click(function() {
    $("#myJsTree").jstree("search", $("#treeSearchText").val());
    return false;
});

我真的AP preciate任何帮助。

I would really appreciate any help.

感谢。

推荐答案

你得到最终的解决方案?

Did you get a solution in the end?

我在做同样的事情,此刻 - 对搜索 AJAX 部分插件选项的行为方式像jQuery AJAX的选择,对我来说我指定的键入:POST,一个成功功能数据类型:JSON和我的成功,函数记录结果在我的控制台

I'm doing the same thing at the moment - the ajax section of the search plugin options behaves just like jQuery ajax options, for me I specified type: "POST", a success function and dataType: "JSON" and my success function is recording the results in my console.

从手册的(未在世界上最好的手册),对每个搜索结果匹配的,你需要指定它的路径,而不包括匹配的节点ID。 jsTree将加载你给它的路径和内置高亮匹配的文本。例如。如果你的搜索匹配的条目是在 #secondchild 节点(3个等级下降),这就是你需要传递给jsTree它加载该结果在搜索:

From the manual (not the best manual in the world), for each search result matched, you need to specify the path to it without including the matching nodes ID. jsTree will then load the path you give it and the inbuilt highlighting will match the text. E.g. if the entry your search matched is in the #secondchild node (3 levels down), this is what you'll need to pass to jsTree for it to load that result in your search:

Array[ '#rootnode', '#firstchild', '#secondchild' ]

这将加载这三个节点,你已经已经指定,如果他们不已经在DOM存在的标准数据检索方法。做了几个测试,你的JSON结果被硬编码你后的路径,看看它是如何工作的。

This will load those three nodes with the standard data retrieval method you've specified already, if they don't already exist in the DOM. Do a few tests with your JSON results by hard coding the path you're after to see how it works.

请注意:如果你已经在你的Ajax请求定义的成功方法,你需要回到你在你的方法获取数据对象,这将使jsTree解析它,做它的事。在我来说,我有的console.log(数据),它什么也没做,因为我没有返回数据之后。

Note: if you've defined a success method in your Ajax request, you'll need to return the data object you get in your method, which will allow jsTree to parse it and do its thing. In my case I had console.log(data) and it did nothing, because I wasn't returning the data afterwards.

修改

我解决,而不是返回包含你需要返回一个包含唯一节点ID单级阵列中的每个匹配元素的路径多个阵列我的问题,和搜索插件解析它预期并加载新的客户端执行搜索之前。

I solved my problem, instead of returning multiple arrays containing the path to each matched element you need to return a single-level array containing the unique node IDs, and the search plugin parses it as expected and loads the new ones before the client side search is performed.

在这里看到我的解释:http://stackoverflow.com/a/20316921/2812842