【问题标题】:Use custom Json data in jstree form以 jstree 形式使用自定义 Json 数据
【发布时间】:2017-05-30 16:39:37
【问题描述】:

这是我输入的 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,但我需要一些其他字段,例如 dataType 等。

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

这是我的表单和 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"]

【问题讨论】:

    标签: json jstree


    【解决方案1】:

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

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

    data[count]["original"]["path"]

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-03
      • 1970-01-01
      • 2014-05-15
      • 1970-01-01
      • 1970-01-01
      • 2020-03-14
      相关资源
      最近更新 更多