2017-05-30 43 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"]