简体   繁体   English

AngularJS:动态表单生成

[英]AngularJS : dynamic form generation

I need a way of generating dynamic forms based on a document structure in order to update existing values of its attributes or add values to attributes that are multivalued. 我需要一种基于文档结构生成动态表单的方法,以便更新其属性的现有值或向多值属性添加值。

I have a controller in angular that receives a document class name and id and pulls a specific document of that class. 我有一个角度控制器,接收文档类名称和id,并拉出该类的特定文档。

The controller reads the document and saves data like number of attributes, attribute type, if the attribute is multivalued or not and the values. 控制器读取文档并保存数据,如属性数量,属性类型,属性是否为多值以及值。

Each class is different in number of attributes, attribute types. 每个类的属性数量,属性类型都不同。

Here is an example of the data generated by my controller for a specific document of a specific class: 以下是我的控制器为特定类的特定文档生成的数据示例:

 CLASS/id {cid: "Disease/54d49a8c3b70f6cce63dc475"}
 N° ATTRIBUTES:3(without counting multivalues of the same attribute)


 /////////////////////////////////////////////////////////////////////////////////////////////
 ATTRIBUTE NAME:displayName
 /////////////////////////////////////////////////////////////////////////////////////////////
 ATTRIBUTE DATA TYPE:text
 MULTI VALUE ATTRIBUTE:0(not multivalue field)
 ATTRIBUTE N° OF VALUES:1(existing n° of values at the moment)
 ATRIBUTE ARRAY POSITION:0 ---> value displayName #1


 /////////////////////////////////////////////////////////////////////////////////////////////
 ATTRIBUTE NAME:identifier
 /////////////////////////////////////////////////////////////////////////////////////////////
 ATTRIBUTE DATA TYPE:text
 MULTI VALUE ATTRIBUTE:1(multivalue field)
 ATTRIBUTE N° OF VALUES:1(existing n° of values at the moment)
 ATRIBUTE ARRAY POSITION:0 ---> value identifier #1


 /////////////////////////////////////////////////////////////////////////////////////////////
 ATTRIBUTE NAME:r
 /////////////////////////////////////////////////////////////////////////////////////////////
 ATTRIBUTE DATA TYPE:date
 MULTI VALUE ATTRIBUTE:1(multivalue field)
 ATTRIBUTE N° OF VALUES:3(existing n° of values at the moment)
 ATRIBUTE ARRAY POSITIONS:0 ---> value r #1
 ATRIBUTE ARRAY POSITIONS:1 ---> value r #2
 ATRIBUTE ARRAY POSITIONS:2 ---> value r #3

Depending on the processed class and document these value change. 根据处理的类和文档,这些值会发生变化。

All this works fine but I am new to angular and not sure how to render things on the view side based on the former generated data. 所有这一切都很好,但我是角度新手,不知道如何根据以前生成的数据在视图侧渲染事物。

Based on the data example shown above I would want the generated form to be something like: 基于上面显示的数据示例,我希望生成的表单类似于:

在此输入图像描述

Can anyone help me showing me the right approach for this? 任何人都可以帮我向我展示正确的方法吗?

1) Convert that document data to a JSON list of objects, each object being a field: 1)将该文档数据转换为JSON对象列表,每个对象都是一个字段:

fields = [
{"name": "displayName", dataType: "text", isMultiValue: false, numberOfValues: 1},
...
]

This is probably the most complicated part. 这可能是最复杂的部分。

2) Somewhere in the controller: 2)控制器中的某个地方:

$scope.documentData = {};
$scope.getFieldTemplateUrl = function(field) {
    return '/url/to/field/templates/' + field.dataType + '.html';
};

3) 3)

<div ng-repeat="field in fields">
    <div ng-include="getFieldTemplateUrl(field)"></div>
</div>

4) Define those field templates for each dataType . 4)为每个dataType定义那些字段模板。 For "text" it's quite simple: 对于“文本”,这很简单:

<label for="{{field.name}}">{{field.name}}</label>
<input id="{{field.name}}" type="text" ng-model="documentData[field.name]" />

Building on this you'll need to add support for isMultiValue and probably populate documentData with the existing data. 在此基础上,您需要添加对isMultiValue支持,并可能使用现有数据填充documentData

Also there are a lot of libraries doing exactly this: 还有很多库正是这样做的:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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