jsTree:使用 ajax 进行渐进式渲染/从数组中渲染节点

发布于 2025-01-08 03:36:43 字数 1348 浏览 0 评论 0原文

这是关于 jsTree jQuery 插件。我已经为此苦苦挣扎了一段时间,现在才意识到 (本身)不可能做到,所以我考虑了以下解决方案来解决我的问题(这不起作用)。

我有一棵树,它使用 json_data 插件和 ajax。一旦你打开 特定节点,服务器返回的结果是超过 1000 个的数组 json 节点。响应速度相当快,但渲染本身需要 一会儿(用户体验是他得到了烦人的“脚本不 响应 - 停止脚本/继续”消息。

我想到的解决方案是限制发回的结果 将服务器设置为较小的数字(例如 200)并使用一些“显示更多” 标签(或使用 jQuery 滚动事件)来获取接下来的 200 个。 但是,在每个节点上使用 jstree.create 似乎是 非常慢。 然后我注意到 jsTree google 群组上的这个帖子 其中 Ivan 建议可以使用一次创建所有节点 parse_json 函数 - 这对我不起作用。

我正在尝试做的事情的简短代码片段: (当单击“显示更多”标签时):

$.ajax({
   // send data to server in order to get the relevant json back
   }(),
   success : function (r) {
           var parent_node = data.inst._get_parent(data.rslt.obj);
           var id = parent_node.attr("id");
           $("#root_tree").jstree("_parse_json", r, parent_node );
           $("#root_tree").jstree("clean_node", parent_node, false);
           }
   });

上面的示例不会渲染 json 并将子项添加到 父节点。

我将非常感谢任何其他方法或者如果有人可以指出 看看我做错了什么。 再次,使用:

$.each(r, function(i, node) {
       var id = parent_node.attr("id");
       $("#root_tree").jstree("create", "#"+id, "last", node, false, true);
});

确实有效,但非常非常慢(比渲染所有 1000 个节点慢 一起)。

谢谢

This is regarding the jsTree jQuery plugin. I've been struggling with this for a while now only to realise it's
not (natively) possible to do, so I thought about the following solution to my problem below (which doesn't work).

I have a tree that uses the json_data plugin with ajax. Once you open
a specific node the result from the server is an array of over 1000
json nodes. The response is pretty fast but the rendering itself takes
a while (the user experience is that he gets the annoying "script not
responding - stop script / continue" message.

The solution I thought about was limiting the results sent back from
the server to a smaller number (say 200) and using some "show more"
label (or using the jQuery scroll event) to fetch the next 200.
However, using the jstree.create on each of those nodes appears to be
very slow.
I then noticed this thread on the jsTree google group
in which Ivan suggest it's possible to create all nodes at once using
the parse_json function - this doesn't work for me.

A short code snippet of what I'm trying to do:
(when clicking the "show more" label):

$.ajax({
   // send data to server in order to get the relevant json back
   }(),
   success : function (r) {
           var parent_node = data.inst._get_parent(data.rslt.obj);
           var id = parent_node.attr("id");
           $("#root_tree").jstree("_parse_json", r, parent_node );
           $("#root_tree").jstree("clean_node", parent_node, false);
           }
   });

The above example doesn't render the json and adds the children to the
parent node.

I would highly appreciate any other approach or if anyone could point
out what am I doing wrong.
Again, using:

$.each(r, function(i, node) {
       var id = parent_node.attr("id");
       $("#root_tree").jstree("create", "#"+id, "last", node, false, true);
});

Does work, but very very slowly (slower than rendering all 1000 nodes
together).

Thanks

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

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

发布评论

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

评论(2

素衣风尘叹 2025-01-15 03:36:43

好吧,所以我的使用有点少。

我最终做的是在树视图上调用函数而不是监听事件:

 var ref = parent_node.attr("id");
 $.each(data, function(i, jsonNode) {
       var node = inst._parse_json(jsonNode);
       node.insertInside(ref);
 });

Ok, so my usage was a bit off.

What I ended up doing, was calling the function on the tree view instead of listening to the event:

 var ref = parent_node.attr("id");
 $.each(data, function(i, jsonNode) {
       var node = inst._parse_json(jsonNode);
       node.insertInside(ref);
 });
夜光 2025-01-15 03:36:43

这就是我设置树的方式,我有超过几百个节点,它的工作方式就像一个魅力。我在 IE6/7 中确实遇到了一个非常非常轻微的性能问题,但在其他地方都表现得很好。

$('#serverTree').bind("select_node.jstree", function (e, data) {
        var url = data.rslt.obj.children("a:eq(0)").attr("href");
        if (url === "hasChild") {
            data.inst.toggle_node(data.rslt.obj);
        }
        else {
           //Do something when the leaf nodes are clicked
        }

    }).jstree({
        "themes": { "theme": "apple", "dots": false, "icons": false },
        "json_data": {
            "ajax": {
                "url": "/Home/GetNodes",
                "data": function (n) {
                    return { id: n.attr ? n.attr("id") : 0 };
                }
            }
        },
        "plugins": ["themes", "json_data", "ui"]
    });

这是我从服务器返回的 JSON 的样子:

[{"data":{"title":"Node1","attr":{"id":null,"href":"hasChild"}},
 "attr":{"id":"Node1","href":null},"state":"closed"}]

This is how I have my tree set up and i have well over a few hundred nodes and it works like a charm. I did have a very very slight performance issue in IE6/7 but works like a champ everywhere else.

$('#serverTree').bind("select_node.jstree", function (e, data) {
        var url = data.rslt.obj.children("a:eq(0)").attr("href");
        if (url === "hasChild") {
            data.inst.toggle_node(data.rslt.obj);
        }
        else {
           //Do something when the leaf nodes are clicked
        }

    }).jstree({
        "themes": { "theme": "apple", "dots": false, "icons": false },
        "json_data": {
            "ajax": {
                "url": "/Home/GetNodes",
                "data": function (n) {
                    return { id: n.attr ? n.attr("id") : 0 };
                }
            }
        },
        "plugins": ["themes", "json_data", "ui"]
    });

This is how my JSON coming back from the server looks like :

[{"data":{"title":"Node1","attr":{"id":null,"href":"hasChild"}},
 "attr":{"id":"Node1","href":null},"state":"closed"}]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文