2014-10-20 45 views
3

我有,我想,以填补一个jsTree一个div:jsTree - 填充树动态使用AJAX/C#Web方法

我得到的“加载”图标,从而使树的意思不过显示,似乎有一个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>'); 
    } 
}); 

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

是否有任何问题与上述(或有其他地方我应该看)?还是有更好的方法来实现其预期目的?

回答

4

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

"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"}] 
+0

当我试图这只是显示“正在加载...”在jstree与微调。我的ajax脚本返回一个200(OK)的状态,所以我知道它正在加载源数据。任何想法,为什么这可能会发生? – Andy 2017-12-04 11:50:28