从 JSON 加载 jsTree 子级

发布于 2024-10-11 16:47:09 字数 992 浏览 7 评论 0原文

我对 JavaScript 编码相当陌生,特别是在使用 jQuery 方面。我正在尝试使用 jsTree 尝试通过 JSON 填充它。现在我相信我的问题已在相关问题上得到解答,但我似乎无法找到我正在做的事情的具体答案。我也参考了 jsTree 文档,但我仍然无法弄清楚。

下面是我正在尝试的代码,

buildTree : function(name, url, nodeRef) {

     $("#"+name).jstree({ 
      "json_data" : {
    "ajax" : {
     "url" : url + nodeRef,
     "data" : function(n) {
      return { id : n.attr ? n.attr("id") : 0 };
     }
    }
   },

   "plugins" : [ "themes", "json_data" ]
  });
    }

这基本上是 jsTree 文档中 JSON 代码的副本。这里发生的事情是我能够填充其内容树的根文件夹。以下是我收到的 JSON 响应。

[
 {
 "attr":{"id":"a3e6a1f0-ec50-4215-a6df-2065eb09115e","rel":"file"},
 "data":"File 1","state":""
 },
 {
 "attr":{"id":"b38e4a72-875b-4d69-95d3-5437f7e65575","rel":"folder"},
 "data":"Folder 1","state":"closed"
 }
]

但是,当我单击该文件夹的节点时,它会由相同的 JSON 请求填充,具有相同的值。

我知道罪魁祸首是

"url" : url + nodeRef,

,但我不知道如何传递从响应中获得的 ID,替换初始的 nodeRef 值。我尝试过使用回调,但没有任何反应。

非常感谢任何帮助。

I'm fairly new to JavaScript coding, specifically on using jQuery. I'm trying to use jsTree trying to populate it through JSON. Now I believe my question had been answered on related questions but I can't seem to find a specific answer to what I'm doing. I have referred to the jsTree documentation as well, but I still can't figure it out.

below is the code I'm trying out

buildTree : function(name, url, nodeRef) {

     $("#"+name).jstree({ 
      "json_data" : {
    "ajax" : {
     "url" : url + nodeRef,
     "data" : function(n) {
      return { id : n.attr ? n.attr("id") : 0 };
     }
    }
   },

   "plugins" : [ "themes", "json_data" ]
  });
    }

this is basically a copy of the code in the jsTree documentation for JSON. What's happening here is I am able to populate the root folder of the tree of it's contents. Below is the response of the JSON I'm getting.

[
 {
 "attr":{"id":"a3e6a1f0-ec50-4215-a6df-2065eb09115e","rel":"file"},
 "data":"File 1","state":""
 },
 {
 "attr":{"id":"b38e4a72-875b-4d69-95d3-5437f7e65575","rel":"folder"},
 "data":"Folder 1","state":"closed"
 }
]

But when I click the the node of the folder, It is being populated by the same JSON request, having the same values.

I know the culprit is

"url" : url + nodeRef,

but I am lost on how I'm going to pass the ID I'm getting from the response, replacing the initial nodeRef value. I have tried using the callbacks but nothing is happening.

Any help is very much appreciated.

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

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

发布评论

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

评论(1

好久不见√ 2024-10-18 16:47:09

代码中的以下部分已经负责将“id”查询字符串参数附加到 ajax url。该函数返回一个带有键/值对的对象,该对象将用于要请求的 url 的查询字符串参数。

"data" : function(n) {       return { id : n.attr ? n.attr("id") : 0 };      } 

我不确定您的代码中的 nodeRef 变量的用途是什么。是为了找到根节点吗?如果是这样,您不能为此使用不同的查询字符串参数吗?

使用 fiddler 确认 jsTree 构造的 url 是您期望的。也许它当前正在创建一个带有两个 id 查询字符串参数的 url,并且它始终使用您的 nodeRef 变量提供的一个?

The following part in your code already takes care of appending an "id" querystring parameter to the ajax url. The function returns an object with key/value pairs that will be used for querystring parameters for the url to be requested.

"data" : function(n) {       return { id : n.attr ? n.attr("id") : 0 };      } 

I am not sure what the purpose of the nodeRef variable is in your code. Is it to find the root node? If so, could you not use a different querystring parameter for that?

Use fiddler to confirm the urls constructed by jsTree are what you expect it to be. Perhaps it's currently creating a url with two id querystring parameters and it always uses the one provided by your nodeRef variable?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文