这是我输入 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"]
我会成功,因为 text
和 id
是 the 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/