List 字段
由多行组成的表格,每一行都在子表单中编辑
list 字段显示一个由多行组成的表格,每一行都在专用的子表单面板中创建和编辑(下钻模式)。它取代了在 7.0.0 中移除的 table 字段。
主要功能
list 字段提供以下功能:
- 每一行都在全屏子表单中编辑,可以使用所有字段类型,包括嵌套列表。
- 子表单本身就是一个类型为
subform的独立表单,可以在不同表单的多个 list 字段中复用(例如客户、供应商、订单……共用同一个Address子表单)。 - 标记属性(
insertMarker、updateMarker、deleteMarker)支持幂等的 playbook 模式。 - 当
allowDelete: false时,已有的行受到保护;只有在当前会话中添加的行(带有insertMarker)才能删除。 columns属性控制哪些子表单字段作为列显示在汇总表格中。- 输出始终是一个对象数组。生成 extravars 时,会遵循子表单字段上声明的
model、output、outputObject和valueColumn。
子表单声明
子表单是一个带有 type: subform 的独立表单,和其他表单一样放在 forms 文件夹的某个文件中。它接受与其他表单相同的 fields 数组,但从不出现在表单磁贴列表中,也从不直接提交。
- name: Address
type: subform
fields:
- name: street
type: text
label: Street
required: true
- name: city
type: text
label: City
required: true
- name: country
type: text
label: Country
属性
list 字段可用的属性,按用途分组:
| 属性 | 说明 |
|---|---|
| basic | |
| name string / 必填 / 唯一 | Field name
This attribute represents the name of the form field. |
| type string / 必填 | Field type
Other attributes might only be available for some field types. 值: list 示例Text |
| label string | Field label
A friendly name/label for the field. Note: if not set, the field name is used as label, but not when type is html. Since 6.0.3: Supports placeholders for dynamic labels (e.g., |
| help string | Field help message
Some fields require additional help information. This help message will be shown below the field. Since 6.0.3: Supports placeholders for dynamic help text (e.g., |
| subform string / 必填 | Name of the subform that defines one row
For a |
| data | |
| default many | Default value
The type of the value depends on the field type.
For |
| expression string | A javascript expression.
By default this javascript is evaluated on the server side and limited to predefined functions (for security). However in combination with tip: use the alias 示例Get a list of users from a rest api Filter and sort an array of objects Make a numbered list Use the alias `local` Use the alias `local_out` |
| runLocal boolean | Run an expression locally
When running expressions, by default they are run remotely on the server. 默认值: false |
| dbConfig string, array or object | A database connection
When you want to query data from a database, you will need the proper connection information. 示例get information from a mysql database (legacy example - deprecated) get information from a mysql database (new example) get information from a mysql database (array example) get information from a mongodb |
| query string | The query to select data from a database
This is typically a classic SELECT statement. Every 示例get information from a mysql database get information from a mongodb |
| interaction | |
| dependencyFn string 在版本 4.0.0 中添加 | The dependency logical function
This attribute represents the logical function between multiple dependencies. 选项:
|
| dependencies list / elements=object | Show/hide this field based on the values or other fields
Each dependency element is either an object with the following 2 attributes:
Or with the following 2 attributes:
Use in combination with attribute 示例Show a field based on a checkbox Show a field if another field is valid Show a field based on 2 fields, using the 'or' function |
| allowInsert boolean | Allow table/list insert
A table or list can be used to modify existing data. If new data is not allowed, set this property to false. 默认值: true |
| allowDelete boolean | Allow table/list delete
A table can be used to modify existing data. If you don’t was recoreds to be deleted, set this property to false. 默认值: true |
| showLoadButton boolean 在版本 6.1.5 中添加 | Show load button for file import
Enables a “Load YAML” button that allows users to import data from .yml or .yaml files. For list fields, the YAML file must contain a valid array of objects. For yaml fields, the file can contain any valid YAML structure. Single objects are automatically wrapped in arrays for list fields. 默认值: false 示例List with YAML import YAML field with file import |
| showDownloadButton boolean 在版本 6.1.5 中添加 | Show download button for file export
Enables a “Download” button that exports the field content as a .yml file. The filename is automatically generated from the field name. 默认值: false |
| output | |
| output boolean 在版本 6.2.0 中添加 | Include field value as extravar
Form fields are by default sent as extravars. 默认值: true |
| model string or array | Extravar modelling
By default, a field is sent as a root-extravar. 示例Model 2 fields together as a single object |
| deleteMarker string | Adds an additional deletemarker field
When you remove a record in a table, it gets removed. Since 6.4.1 the markers of a 示例Mark delete record |
| insertMarker string | Adds an additional insertmarker field
When you add a record in a table, it simply gets added. 示例Mark new record |
| updateMarker string 在版本 5.1.0 中添加 | Adds an additional updatemarker field
In addition to the insert and delete markers, you can also add an update marker. 示例Mark updated record |
| security | |
| noLog boolean 在版本 2.2.3 中添加 | Disable backend logging
Disables logging in the backend, to hide passwords for example. 默认值: false 示例hide password in log |
| validation | |
| required boolean | Required field
Makes the field required. 默认值: false |
| validIf object 在版本 2.2.4 中添加 | An field based validation (field must be true)
Enforces a validation where a referencing (expression) field must be true. This field requires an object with 2 attributes:
示例Ip must be pingable |
| validIfNot object 在版本 2.2.4 中添加 | An field based validation (field must be false)
Enforces a validation where a referencing (expression) field must be false. This field requires an object with 2 attributes:
示例Check if powered off |
| ignoreIncomplete boolean | Allow form submit on non-evaluated placeholders
When an expression-based field has placeholders, 默认值: false |
| visualization | |
| group string | The field group name
With this attribute you can group fields together. |
| line string 在版本 4.0.3 中添加 | The field line name
With this attribute you can group fields in a single line together. |
| width string 在版本 4.0.3 中添加 | The field width
With this attribute you can set the width of a field. 选项:
示例Name and lastname |
| titleAdd string | Title shown in the subform pane when adding a new row
When you add a row through a |
| titleEdit string | Title shown in the subform pane when editing an existing row
When you edit a row through a |
| columns array | The list of columns visible in the dropdown/table
For an 示例Show only wanted columns |
示例
共享的地址子表单
同一个表单中的两个 list 字段复用一个 Address 子表单:
- name: Address
type: subform
fields:
- name: street
type: text
label: Street
required: true
- name: city
type: text
label: City
required: true
- name: country
type: text
label: Country
- name: Create customer
type: ansible
playbook: customer.yaml
roles: [default]
fields:
- name: name
type: text
label: Customer name
required: true
- name: addresses
type: list
label: Addresses
subform: Address
columns:
- city
- country
titleAdd: Add address
titleEdit: Edit address
allowInsert: true
allowDelete: true
标记跟踪(支持幂等)
预加载的行受到保护,标记会告诉 playbook 哪些行是新增、修改或删除的:
- name: people
type: list
label: People
subform: Person
allowDelete: false # preloaded rows are protected
insertMarker: added # session-added rows remain removable
updateMarker: updated
deleteMarker: removed
columns:
- name
- email
预填充行
列表可以从三种来源预先填充。无论哪种情况,得到的行都必须是对象,其键与所引用子表单的字段名一致。
静态 default
直接写在表单中的行:
- name: addresses
type: list
label: Addresses
subform: Address
columns:
- city
- country
default:
- street: Main 1
city: Brussels
country: BE
- street: Side 22
city: Paris
country: FR
来自 REST API( expression )
由服务器端表达式从 REST API 加载的行:
- name: users
type: list
label: Users
subform: User
columns:
- username
- email
expression: fn.fnRestBasic('get','https://api.example.com/users','','my_api_cred','')
来自数据库查询( dbConfig + query )
从数据库查询加载的行:
- name: servers
type: list
label: Servers
subform: Server
columns:
- hostname
- role
dbConfig: CMDB_CONN
query: SELECT hostname, role FROM servers WHERE active = 1
dbConfig 指定 连接 > 数据库 中的 数据库(从 7.5.0 起),否则和以前一样指定类型为数据库的 凭据(参见 表单中的数据库)。
来自本地表达式( runLocal )
由本地表达式在浏览器中计算出的行:
- name: ports
type: list
label: Open ports
subform: Port
columns:
- port
- protocol
runLocal: true
expression: |
[
{ port: 22, protocol: 'tcp' },
{ port: 80, protocol: 'tcp' },
{ port: 443, protocol: 'tcp' }
]
嵌套列表
子表单本身也可以包含 list 字段,从而在每一行中再嵌套一个列表:
- name: Address
type: subform
fields:
- name: street
type: text
label: Street
- name: city
type: text
label: City
- name: Person
type: subform
fields:
- name: name
type: text
label: Name
required: true
- name: addresses
type: list
label: Addresses
subform: Address
columns:
- street
- city
- name: My Form
type: ansible
playbook: people.yaml
roles: [default]
fields:
- name: people
type: list
label: People
subform: Person
columns:
- name
- addresses
通过 __parent__ 访问父表单数据
子表单行编辑器打开时,AnsibleForms 会向子表单注入一个 __parent__ 变量。它保存了父表单所有字段值的快照,包括常量和 vars。
在子表单的字段定义中,用标准的 $(...) 表达式语法引用它:
- name: NodeConfig
type: subform
fields:
- name: node_name
type: text
label: Node name
required: true
- name: node_type
type: enum
# only offer gpu in production environments (parent field)
expression: |
'$(__parent__.environment)' === 'production'
? ['standard', 'high-memory', 'gpu']
: ['standard', 'high-memory']
runLocal: true
default: __auto__
- name: Deploy cluster
type: ansible
playbook: deploy.yml
roles: [default]
fields:
- name: environment
type: enum
values: [dev, staging, production]
required: true
- name: nodes
type: list
subform: NodeConfig
columns: [node_name, node_type]
__parent__是一个仅限前端的辅助变量。它会从 Ansible extravars 中移除,永远不会发送给 playbook。