![](/img/trans.png)
[英]Binding to a nested field of complex object and providing default value in AngularJs?
[英]binding nested json object value to a form field
我正在構建一個動態表單來編輯json對象中的數據。 首先,如果存在這樣的事情讓我知道。 我寧願不構建它,但我已經多次搜索一個工具,並且發現只有樹狀結構需要輸入引號。 我很樂意將所有值視為字符串。 此編輯功能適用於最終用戶,因此它非常簡單,不會令人生畏。
到目前為止,我有代碼生成嵌套表來表示json對象。 對於每個值,我顯示一個表單域。 我想將表單字段綁定到關聯的嵌套json值。 如果我可以存儲對json值的引用,我將構建一個對json對象樹中每個值的引用數組。 我還沒有找到一種方法來使用javascript。
我的最后一種方法是在編輯后遍歷表格。 我寧願有動態更新,但單個提交總比沒有好。
有任何想法嗎?
// the json in files nests only a few levels. Here is the format of a simple case,
{
"researcherid_id":{
"id_key":"researcherid_id",
"description":"Use to retrieve bibliometric data",
"url_template" :[
{
"name": "Author Detail",
"url": "http://www.researcherid.com/rid/${key}"
}
]
}
}
$.get('file.json',make_json_form);
function make_json_form(response) {
dataset = $.secureEvalJSON(response);
// iterate through the object and generate form field for string values.
}
// Then after the form is edited I want to display the raw updated json (then I want to save it but that is for another thread)
// now I iterate through the form and construct the json object
// I would rather have the dataset object var updated on focus out after each edit.
function show_json(form_id){
var r = {};
var el = document.getElementById(form_id);
table_to_json(r,el,null);
$('body').html(formattedJSON(r));
}
一種更簡單的方法是接受表單提交並以JSON格式輸出數據。 這樣,就不需要綁定變量了。
解決方案已經到來。 JQuery現在有用於數據綁定和模板的插件。
http://www.borismoore.com/2010/09/introducing-jquery-templates-1-first.html http://api.jquery.com/jQuery.template/ http://api.jquery.com/category /插件/數據鏈路/
還有另一個簡單的模板引擎可以將JSON數據直接加載到表單中。 請參閱http://plugins.jquery.com/project/loadJSON插件。 它的工作方式與Jack放在這里的方式類似,但它使用純HTML作為模板。
您可以在http://jatery-load-json.googlecode.com/上的http://code.google.com/p/jquery-load-json/wiki/WorkingWithFormElements和實際示例中查看如何使用它的說明。 svn / trunk / edit.html?ID = 17 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.