Camp List
Una taula de files, cadascuna editada en un subformulari
Un camp list mostra una taula de files, cadascuna creada i editada en un panell de subformulari dedicat (un patró de navegació en profunditat). Substitueix el camp table, que es va eliminar a la 7.0.0.
Característiques principals
El camp list ofereix les possibilitats següents:
- Cada fila s’edita en un subformulari a pantalla completa, amb accés a tots els tipus de camp, incloses les llistes niades.
- El subformulari és un formulari en si mateix, de tipus
subform, i es pot reutilitzar en diversos camps list de formularis diferents (p. ex. un sol subformulariAddresscompartit per clients, proveïdors, comandes…). - Les propietats de marcatge (
insertMarker,updateMarker,deleteMarker) permeten patrons de playbook idempotents. - Amb
allowDelete: false, les files que ja existien estan protegides; només es poden eliminar les files afegides a la sessió actual (les que porten l’insertMarker). - La propietat
columnscontrola quins camps del subformulari apareixen com a columnes a la taula de resum. - La sortida és sempre una matriu d’objectes. Quan es generen les extravars, es respecten els
model,output,outputObjectivalueColumndeclarats als camps del subformulari.
Declaració del subformulari
Un subformulari és un formulari en si mateix amb type: subform, en un fitxer de la carpeta de formularis com qualsevol altre formulari. Accepta la mateixa matriu fields que qualsevol altre formulari, però no apareix mai a la llista de mosaics de formularis i no s’envia mai directament.
- 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
Propietats
Les propietats disponibles per a un camp list, agrupades per finalitat:
| Atribut | Comentaris |
|---|---|
| basic | |
| name string / obligatori / únic | Field name
This attribute represents the name of the form field. |
| type string / obligatori | Field type
Other attributes might only be available for some field types. Valor: list ExemplesText |
| 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 / obligatori | 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 ExemplesGet 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. Per defecte: 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. Exemplesget 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 Exemplesget information from a mysql database get information from a mongodb |
| interaction | |
| dependencyFn string afegit a la versió 4.0.0 | The dependency logical function
This attribute represents the logical function between multiple dependencies. Opcions:
|
| 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 ExemplesShow 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. Per defecte: 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. Per defecte: true |
| showLoadButton boolean afegit a la versió 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. Per defecte: false ExemplesList with YAML import YAML field with file import |
| showDownloadButton boolean afegit a la versió 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. Per defecte: false |
| output | |
| output boolean afegit a la versió 6.2.0 | Include field value as extravar
Form fields are by default sent as extravars. Per defecte: true |
| model string or array | Extravar modelling
By default, a field is sent as a root-extravar. ExemplesModel 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 ExemplesMark delete record |
| insertMarker string | Adds an additional insertmarker field
When you add a record in a table, it simply gets added. ExemplesMark new record |
| updateMarker string afegit a la versió 5.1.0 | Adds an additional updatemarker field
In addition to the insert and delete markers, you can also add an update marker. ExemplesMark updated record |
| security | |
| noLog boolean afegit a la versió 2.2.3 | Disable backend logging
Disables logging in the backend, to hide passwords for example. Per defecte: false Exempleshide password in log |
| validation | |
| required boolean | Required field
Makes the field required. Per defecte: false |
| validIf object afegit a la versió 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:
ExemplesIp must be pingable |
| validIfNot object afegit a la versió 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:
ExemplesCheck if powered off |
| ignoreIncomplete boolean | Allow form submit on non-evaluated placeholders
When an expression-based field has placeholders, Per defecte: false |
| visualization | |
| group string | The field group name
With this attribute you can group fields together. |
| line string afegit a la versió 4.0.3 | The field line name
With this attribute you can group fields in a single line together. |
| width string afegit a la versió 4.0.3 | The field width
With this attribute you can set the width of a field. Opcions:
ExemplesName 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 ExemplesShow only wanted columns |
Exemples
Subformulari d’adreça compartit
Un subformulari Address reutilitzat per dos camps list del mateix formulari:
- 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
Seguiment amb marcadors (pensat per a la idempotència)
Les files precarregades estan protegides, i els marcadors indiquen al playbook quines files són noves, quines han canviat i quines s’han eliminat:
- 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
Emplenar les files per endavant
Una llista es pot emplenar per endavant des de tres fonts. En tots els casos, les files resultants han de ser objectes les claus dels quals coincideixin amb els noms dels camps del subformulari referenciat.
default estàtic
Files escrites al mateix formulari:
- 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
Des d’una API REST (expression)
Files carregades des d’una API REST per una expressió del costat del servidor:
- name: users
type: list
label: Users
subform: User
columns:
- username
- email
expression: fn.fnRestBasic('get','https://api.example.com/users','','my_api_cred','')
Des d’una consulta a base de dades (dbConfig + query)
Files carregades des d’una consulta a base de dades:
- name: servers
type: list
label: Servers
subform: Server
columns:
- hostname
- role
dbConfig: CMDB_CONN
query: SELECT hostname, role FROM servers WHERE active = 1
dbConfig nomena una base de dades de Connexions > Bases de dades (des de la 7.5.0), o si no una credencial de tipus base de dades, com abans (vegeu Bases de dades als formularis).
Des d’una expressió local (runLocal)
Files calculades al navegador per una expressió local:
- 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' }
]
Llistes niades
Un subformulari pot contenir un camp list propi, per tenir una llista dins de cada fila:
- 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
Accedir a les dades del formulari pare amb __parent__
Quan s’obre l’editor de files d’un subformulari, AnsibleForms injecta una variable __parent__ al subformulari. Conté una instantània de tots els valors dels camps del formulari pare, incloses les constants i les variables.
Fes-hi referència amb la sintaxi d’expressió estàndard $(...) a les definicions dels camps del subformulari:
- 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__és una eina auxiliar només del frontend. Es treu de les extravars d’Ansible i no s’envia mai al playbook.