简体   繁体   English

将嵌套的json对象值绑定到表单字段

[英]binding nested json object value to a form field

I am building a dynamic form to edit data in a json object. 我正在构建一个动态表单来编辑json对象中的数据。 First, if something like this exists let me know. 首先,如果存在这样的事情让我知道。 I would rather not build it but I have searched many times for a tool and have found only tree like structures that require entering quotes. 我宁愿不构建它,但我已经多次搜索一个工具,并且发现只有树状结构需要输入引号。 I would be happy to treat all values as strings. 我很乐意将所有值视为字符串。 This edit functionality is for end users so it needs to be easy an not intimidating. 此编辑功能适用于最终用户,因此它非常简单,不会令人生畏。

So far I have code that generates nested tables to represent a json object. 到目前为止,我有代码生成嵌套表来表示json对象。 For each value I display a form field. 对于每个值,我显示一个表单域。 I would like to bind the form field to the associated nested json value. 我想将表单字段绑定到关联的嵌套json值。 If I could store a reference to the json value I would build an array of references to each value in a json object tree. 如果我可以存储对json值的引用,我将构建一个对json对象树中每个值的引用数组。 I have not found a way to do that with javascript. 我还没有找到一种方法来使用javascript。

My last resort approach will be to traverse the table after edits are made. 我的最后一种方法是在编辑后遍历表格。 I would rather have dynamic updates but a single submit would be better than nothing. 我宁愿有动态更新,但单个提交总比没有好。

Any ideas? 有任何想法吗?

// 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));
}

A much simpler approach would be to accept a form submission and output the data in JSON format. 一种更简单的方法是接受表单提交并以JSON格式输出数据。 That way, there is no need to bind variables. 这样,就不需要绑定变量了。

The solution has arrived. 解决方案已经到来。 JQuery now has plugins for data binding and templates. JQuery现在有用于数据绑定和模板的插件。

http://www.borismoore.com/2010/09/introducing-jquery-templates-1-first.html http://api.jquery.com/jQuery.template/ http://api.jquery.com/category/plugins/data-link/ http://www.borismoore.com/2010/09/introducing-jquery-templates-1-first.html http://api.jquery.com/jQuery.template/ http://api.jquery.com/category /插件/数据链路/

There is another simple template engine that loads JSON data directly into the form. 还有另一个简单的模板引擎可以将JSON数据直接加载到表单中。 See http://plugins.jquery.com/project/loadJSON plugin. 请参阅http://plugins.jquery.com/project/loadJSON插件。 It works similar way as the one that Jack placed here but it uses plain HTML for template. 它的工作方式与Jack放在这里的方式类似,但它使用纯HTML作为模板。

You can see instructions how to use it on the http://code.google.com/p/jquery-load-json/wiki/WorkingWithFormElements and live example on the http://jquery-load-json.googlecode.com/svn/trunk/edit.html?ID=17 . 您可以在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM