使用模型信息填充 JSTree 中的 AJAX url

发布于 2025-01-06 01:16:19 字数 1501 浏览 0 评论 0原文

我正在使用 ajax 命令填充 JSTree 视图。我当前的 JS 代码如下

$(document).ready(function() {
        $("#navigation").jstree({
            "json_data": {
                "ajax": {
                    "url": function (node) {
                        var nodeId = "";
                        var url = "";
                        if (node == -1) {
                            url = "@Url.Action("BaseTreeItems", "Events")";
                        } else {
                            nodeId = node.attr('id');
                            url = "@Url.Action("EventTreeItems", "Events")" +"?selectedYear=" + nodeId;
                        }
                        return url;
                    },
                    "dataType": "text json",
                    "contentType": "application/json charset=utf-8",
                    "data": function(n) { return { id: n.attr ? n.attr("id") : 0 }; },
                    "success": function() {
                    }
                }
            },
            "themes": {
                "theme": "classic"
            },
            "plugins": ["themes", "json_data", "ui"]
        });
    });

我想从“ajax”属性中删除 if 语句,并用来自服务器的 JSON 数据填充它。 JSON 数据如下所示

[{"data":{"title":"2012","attr":{"href":"/Events/EventList?selectedYear=2012"}},"attr":{"id":"2012","selected":false,"ajax":"/Events/EventTreeItems?selectedYear=2012"},"children":null,"state":"closed"},.....]

如何将 json 中的“ajax”属性提供给 JSTree 中的“ajax”属性?

I am populating a JSTree view with ajax commands. My current JS code is as follows

$(document).ready(function() {
        $("#navigation").jstree({
            "json_data": {
                "ajax": {
                    "url": function (node) {
                        var nodeId = "";
                        var url = "";
                        if (node == -1) {
                            url = "@Url.Action("BaseTreeItems", "Events")";
                        } else {
                            nodeId = node.attr('id');
                            url = "@Url.Action("EventTreeItems", "Events")" +"?selectedYear=" + nodeId;
                        }
                        return url;
                    },
                    "dataType": "text json",
                    "contentType": "application/json charset=utf-8",
                    "data": function(n) { return { id: n.attr ? n.attr("id") : 0 }; },
                    "success": function() {
                    }
                }
            },
            "themes": {
                "theme": "classic"
            },
            "plugins": ["themes", "json_data", "ui"]
        });
    });

I would like to eliminate the if statement from the "ajax" property and fill it with the JSON data that is coming from the server. The JSON data looks like this

[{"data":{"title":"2012","attr":{"href":"/Events/EventList?selectedYear=2012"}},"attr":{"id":"2012","selected":false,"ajax":"/Events/EventTreeItems?selectedYear=2012"},"children":null,"state":"closed"},.....]

How can I feed the "ajax" property from the json into the "ajax" property in the JSTree?

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

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

发布评论

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

评论(1

怀念你的温柔 2025-01-13 01:16:19

为了将来的参考,我通过执行以下操作修复了它

.jstree({
            "json_data": {
                "ajax": {
                    "url": function (node) {
                        var url;
                        if (node == -1) {
                            url = "@Url.Action("BaseTreeItems", "Events")";
                        } else {
                            url = node.attr('ajax');
                        }
                        return url;
                    },
                    "dataType": "text json",
                    "contentType": "application/json charset=utf-8",
                    "data": function(n) { return { id: n.attr ? n.attr("id") : 0, ajax: n.attr ? n.attr("ajax") : 0 }; },
                    "success": function() {
                    }
                }
            },

For future reference I fixed it by doing the following

.jstree({
            "json_data": {
                "ajax": {
                    "url": function (node) {
                        var url;
                        if (node == -1) {
                            url = "@Url.Action("BaseTreeItems", "Events")";
                        } else {
                            url = node.attr('ajax');
                        }
                        return url;
                    },
                    "dataType": "text json",
                    "contentType": "application/json charset=utf-8",
                    "data": function(n) { return { id: n.attr ? n.attr("id") : 0, ajax: n.attr ? n.attr("ajax") : 0 }; },
                    "success": function() {
                    }
                }
            },
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文