2017-05-30 45 views
1

这里使用自定义的JSON数据是我输入JSON数据文件的一部分:在jstree形式

{ 
    "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"]

回答

1

好,调试帮我找到我自己的答案(因为如果我问的问题太迅速):

所有这些键:值对,DOC-定义为自定义的,存储在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"]