jsTree - 使用 AJAX/C#Web 方法动态填充树

2023-12-06

我有一个 div,我想用 jsTree 填充它:

我得到了树应该显示的“正在加载”图标,但是,即使没有抛出错误,似乎也会出现 JavaScript 错误。

我从 AJAX 请求加载文件夹结构,如下所示。 Documents.aspx/GetFolders Web 方法返回一个包含FolderId、ParentId 和文件夹名称的列表。我已经调试了 Web 方法,它正在将正确的结果传递给 jsTree“数据”函数。

$.ajax({
   type: "POST",
   url: 'Documents.aspx/GetFolders',
   contentType: "application/json; charset=utf-8",
   success: function (data) {
      data = data.d;

      $("#tree").jstree({
         "core": {
            "themes": {
               "responsive": true
            },
            "data": function () {
               var items = [];
               items.push({ 'id': "jstree_0", 'parent': "#", 'text': "Documents" });
               $(data).each(function () {
                  items.push({ 'id': "jstree_" + this.DocumentFolderId, 'parent': "jstree_" + this.ParentId, 'text': "" + this.Name });
               });
               return items;
            }
         },
         "types": {
            "default": {
               "icon": "fa fa-folder icon-lg"
            },
         },
         "plugins": ["contextmenu", "dnd", "state", "types"]
      });
   },
   error: function () {
      toastr.error('Error', 'Failed to load folders<span class=\"errorText\"><br/>There was a fatal error. Please contact support</span>');
   }
});

调试代码后,似乎正在正确检索数据并按预期返回对象数组。

上述内容是否有任何问题(或者我应该寻找其他地方)?或者有没有更好的方法来达到其预期目的?


我想我终于找到答案了! :)

"core": {
   "themes": {
      "responsive": true
   },
   "data": {
      type: "POST",
      url: "Documents.aspx/GetFolders",
      contentType: "application/json; charset=utf-8",
      success: function (data) {
         data.d;
         $(data).each(function () {
            return { "id": this.id };
         });
      }
   },
}

服务器端,需要以所需的数组格式返回数据,即:

[{id = "node0", parent = "#", text = "Documents"},
{id = "node1", parent = "node0", text = "Public"},
{id = "node2", parent = "node0", text = "Restricted"}]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

jsTree - 使用 AJAX/C#Web 方法动态填充树 的相关文章

随机推荐