如何消除无子节点的递归 AJAX 请求?
问题:
根据作者的说法,jsTree 文档:
当打开一个关闭的节点(没有加载的子节点)时,会发出 AJAX 请求。
如何配置jsTree来摆脱对每个空/无子节点发出的这些AJAX数据请求?我希望我的空节点保持为空(或无子节点)!
给定(简化):
JSON 数据容器 (data.json)
{
"data" : "Root node with no children",
"children" : []
}
jsTree 配置
{
"json_data" : {
"ajax" : {
"url" : "data.json",
"type" : "GET",
"dataType" : "json",
"dataFilter" : function (data, type) {
//some filtering function
}
}
},
"plugin" : ["json_data"]
}
Problem:
According to the author, jsTree Documentation:
When opening a closed node (that has no loaded children) an AJAX request is made.
How do I configure jsTree to get rid of these AJAX data requests made for each empty/childless node? I want my empty nodes remain empty (or childless)!
Given (simplified):
JSON data container (data.json)
{
"data" : "Root node with no children",
"children" : []
}
jsTree configuration
{
"json_data" : {
"ajax" : {
"url" : "data.json",
"type" : "GET",
"dataType" : "json",
"dataFilter" : function (data, type) {
//some filtering function
}
}
},
"plugin" : ["json_data"]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将叶节点的状态标记为“leaf”。那应该解决它。
Mark the state of the leaf node as "leaf". That should fix it.
我在为 XML 树的无子节点设置属性 state="close" 时遇到了这个问题。删除状态属性可以解决该问题。
I have had this problem setting the attribute state="closed" on childless nodes for XML trees. Removing the state attribute solves the issue.
几周前我也遇到了类似的问题。我在“url”字段中进行了函数调用,最终生成了基于 SQL 查询生成 JSON 字符串的 java 代码。因此,当我单击一个无子节点的关闭节点时,该函数会再次被调用,从而产生一棵无尽的树。
我解决这个问题的方法是:
“data”中定义的函数的结果将作为参数添加到“url”函数中。然后我可以检查参数是否为 0(初始加载)或 1(我的 root 的 id)或其他值。
如果这对你不起作用,也许你可以尝试这样的事情:
不过我不太确定这是否有效。 “before.jstree”在所有事件之前触发。我正在检查即将触发的函数是否是“create”,如果是,我检查所选节点的 id。如果它不是我的 root id 或 0(初始加载),我将停止创建函数。
我在不同的情况下使用类似的结构,所以这样的东西应该有效。但“创建”事件可能不是您应该绑定的事件。您可以将其更改为
查看调用了哪些函数。
I had a similar problem a couple of weeks ago. I had a function call in the "url" field, which ultimately led to java code that made a JSON string based on a SQL query. So when I clicked on a childless closed node, the function was called again, resulting in an endless tree.
the way I solved this was:
The result of the function defined in "data" will be added as a parameter to the "url" function. Then I can check whether the parameter was 0 (initial load) or 1 (the id of my root) or something else.
If this doesn't work for you, maybe you could try something like this:
I'm not exactly sure this works though. "before.jstree" fires before all events. I'm checking whether the function about to fire is "create", and if it is I check the id of the selected node. If it's something else than my root's id or 0 (initial load) I stop the create function.
I use a similar structure for a different situation, so something like this should work. It could be that the "create" event is not what you should be binding to though. You can change it to
To see which functions are called.
只需跳过 Children 属性即可。显然你的节点没有子节点,但是你指定了属性?只需跳过它,该节点将被渲染为叶节点,并且不会发出进一步的请求。
Just skip the children attribute. Obviously your node has no children, but you specify the attribute? Simply skip it, the node will be rendered as a leaf node and no further requests will be made.
我也一直在为这个问题苦苦挣扎。我从 jsTree - 通过 ajax 按需加载子节点得到了主要想法
基本问题是,当我们单击未设置为叶子的子节点时,会使用我们在树配置中设置的 URL 生成新的 AJAX 请求。上面链接中看到的技巧是提供一个函数而不是静态 URL 字符串。我的 jstree 用于显示服务器上的目录结构,因此我可以安排动态添加子目录的 URL。如果您将一个函数分配给 jstree 配置中 ajax 属性的 url 属性,该函数将接收您单击的节点作为参数。我的节点显示目录或文件的名称,因此我可以使用 text() 函数来获取原始目录名称。以这种方式返回的名称前面似乎有一些空格,因此我使用了 String trim() 函数,然后encodeURIComponent 为我提供了可以在 URL 中使用的内容。
如果 -1 传递给 url 函数,那么您就处于根目录,可以安全地使用您的基本 URL。现在,这仅适用于层次结构的第一级。我还有更多工作要做,添加节点元数据的完整路径或类似的东西,但这个想法可能会让您走上正确的轨道。看起来这并不完全是一个错误,而是设计使然。您必须确保子节点触发的请求向服务器发送合适的 URL。
下面是我在 jstree 配置中分配给 ajax 对象的 url 属性:
我的计划是通过轮询节点的完整路径来累积构建 URL,然后添加如上所述的节点名称以创建最终 URL。伪代码:
更新
请参阅我的答案如何获取 jsTree 的元数据。 关于使用node.data().path从节点获取元数据
I've been struggling with this problem too. I got the main idea from jsTree - loading subnodes via ajax on demand
The basic problem is that when we click a child node that is not set to leaf, a new AJAX request is generated with the URL we set in the tree configuration. The trick seen in the above link is to provide a function instead of a static URL string. My jstree is used to display a directory structure on the server, so I can arrange to dynamically add to the URL for sub-directories. If you assign a function to the url property of the ajax property in your jstree configuration, the function receives the node you clicked as an argument. My nodes display the names of directories or files so I can use the text() function to get the raw directory name. There seems to be some space in front of the name returned in this way, so I used a String trim() function and then encodeURIComponent to give me something I can use in a URL.
If -1 is passed to the url function, then you're at the root and you can safely use your base URL. Now, this only works for the first level of the hierarchy. I've got a bit more work to do, adding the full path to the metadata of the node or something like that, but this idea might put you on the right track. It looks as if it's not exactly a bug but by design. You have to make sure a request triggered by a subnode sends a suitable URL to the server.
Here's the url property I've assigned to the ajax object in my jstree configuration:
My plan is to build the URL cumulatively by polling the node for its full path, then adding the node's name as above to create the final URL. Pseudo code:
UPDATE
See my answer here how to get the metadata of jsTree. about getting the metadata from the node using node.data().path