json - 以jstree形式使用自定义Json数据

标签 json jstree

这是我输入 Json 数据文件的一部分:

{
  "id": "ns=2:s=Configured Tags",
  "text": "Configured Tags",
  "path": "Configured Tags",
  "children": [
    {
      "id": "ns=2:s=[System]",
      "text": "System",
      "path": "Configured Tags/System",
      "children": [
        {
          "id": "ns=2:s=[System]Gateway",
          "text": "Gateway",
          "path": "Configured Tags/System/Gateway",
          "children": []
        }]
    }]
 }

这里我有自定义字段path,但我需要其他一些字段,例如数据类型等。

我想返回来自自定义字段的串联字符串,作为表单的输入值。

这是我的表单和 jstree 脚本部分:

<form id="form" action="/opc_add_data" method="post">
            <div id="tree"></div>
            <br>
            <button class="btn-flat inverse pull-right" name="submit" id="submit" onclick="return confirm('Are you sure?')" type="submit">Confirm</button>
        </form>


    $('#form').submit(function() {
        var data = $('#tree').jstree(true).get_bottom_selected(true);
        for(var count = 0; count < data.length; count++){
           $(this).append('<input type="text" name="' + data[count]["id"] + '" value="' + data[count]["path"] + '" hidden/>');
        }
        return true;
    });

我的表单流程的一部分(Python2.7):

        for key in request.form.keys():
        if key != "submit":        
            description.append(re.sub('[^a-zA-Z0-9 \n.]', '_', request.form.get(key)))

如果我尝试获取 data[count]["id"]data[count]["text"] 我会成功,因为 textidthe doc 中描述的字段。 。但是当我尝试使用我的自定义时,我得到 "undefined" 作为值。

我的问题是:我真的可以做我想做的事,即以这种方式获取自定义字段data[count][“path”]

最佳答案

嗯,调试帮助我找到了自己的答案(好像我问得太快了):

所有这些键:值对(文档定义为自定义定义)都存储在节点对象的 original 属性中。因此,我们可以使用以下语法访问自定义(在我的例子中):

数据[计数]["原始"]["路径"]

或者甚至在 Json 中定义一个新的结构:

{
      "id": "ns=2:s=[System]Gateway",
      "text": "Gateway",
      "attr": {
               "path": "Configured Tags/System/Gateway",
               "other": "other_value"
      },
      "children": []
}

然后使用data[count]["original"]["attr"]["path"]

关于json - 以jstree形式使用自定义Json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44267659/

相关文章:

php - json_decode 到数组或对象

android - 如何将 json 对象转换为 android 中的字符串..?

javascript - 如何在完整的 jstree 重新加载事件上绑定(bind)回调函数?

javascript - 如何删除没有子节点的根节点的三 Angular 形图标(用于展开/折叠)?

javascript - JStree:如何将 AJAX 加载的根节点的状态设置为未确定

json - 将 mongoengine 对象转换为 JSON

php - Facebook 个人资料图像并将其下载到我的网络目录

javascript - 是否可以合并来自 JSON 对象和列的数据? [C3JS]

asp.net - 通过asp :hiddenfield回发将json数据持久保存在jstree中

javascript - jstree select_limit 不工作。我想将选择限制设置为仅选择 3 个节点