获取JSTree的JSON数据及其元数据

发布于 2024-09-29 12:04:21 字数 544 浏览 3 评论 0原文

我们使用 jstree 作为导航菜单编辑器,并且一直将元数据分配给树的节点,如下所示:

var data = currentNode.data("jstree");
data.title = textBoxTitle.val();
data.linkType = textBoxLink.val();

我可以看到 data 对象包含相关属性,但不太确定 jquery 在哪里在此之后保留相关数据。

当我们保存数据(将其序列化为服务器端语言)时,元数据似乎被忽略......

var json = jQuery.jstree._reference(tree).get_json();
var jsonString = JSON.stringify(json);

检查 json 对象没有描述元数据的属性。

我们如何序列化对象及其元数据?

提前致谢,

  • Greg。

We're using jstree for a navigation menu editor, and have been assigning metadata to the nodes of the tree like this:

var data = currentNode.data("jstree");
data.title = textBoxTitle.val();
data.linkType = textBoxLink.val();

I can see that the data object contains the relevant properties, but not too sure where jquery keeps the associated data after this point.

When we come to save the data (serializing it to our server-side language), the metadata seems to be ignored...

var json = jQuery.jstree._reference(tree).get_json();
var jsonString = JSON.stringify(json);

The json object is inspected to have no property describing the metadata.

How do we serialize the object along with its metadata?

Thanks in advance,

  • Greg.

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

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

发布评论

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

评论(2

π浅易 2024-10-06 12:04:21

简要阅读文档,在我看来,您似乎应该使用节点上的属性来存储元数据。您可以使用 HTML5“data-”属性:

currentNode.attr('data-title', textBoxTitle.val());
currentNode.attr('data-link-type', textBoxLink.val());

然后当您从树中执行 .get_json() 时,您可以告诉它您想要什么属性:

var json = jQuery.jstree._reference(tree).get_json(-1, ['data-title', 'data-link-type', 'id', 'class']);

.get_json 函数实际上采用2 个属性名称列表,一个用于

  • 节点,一个用于 节点(按此顺序)。我不知道你的树是什么样子,所以我不确定你的属性会去哪里。 (另外,前导的“-1”参数告诉它获取整个树,这是您之前通过不传递任何内容来完成的。)
  • Reading the documentation briefly, it looks to me as if you should be using attributes on the nodes to store your metadata. You could use HTML5 "data-" attributes:

    currentNode.attr('data-title', textBoxTitle.val());
    currentNode.attr('data-link-type', textBoxLink.val());
    

    then when you do the .get_json() from the tree you tell it what attributes you want:

    var json = jQuery.jstree._reference(tree).get_json(-1, ['data-title', 'data-link-type', 'id', 'class']);
    

    The .get_json function actually takes 2 lists of attribute names, one for <li> nodes and one for <a> nodes (in that order). I don't know what your tree looks like so I'm not sure where your attributes would go. (Also that leading "-1" argument tells it to get the whole tree, which you were doing previously by just passing nothing.)

    淤浪 2024-10-06 12:04:21

    您也可以使用此名称空间

    $('#treeid').data().jstree 
    $('#treeid').data().jstree.get_json()
    

    ,并且还有其他方法可以获取您需要的数据

    you might use this namespace as well

    $('#treeid').data().jstree 
    $('#treeid').data().jstree.get_json()
    

    and also there are other methods where you can get the data you need

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