[Baidu amis] Data Mapping
Data Mapping
Data Mapping allows users to obtain the value of a variable in the current Data Chain through ${xxx}
or $xxx
to achieve flexible data configuration functions. It is mainly used in scenarios such as template strings and custom api request data body formats.
Template string
1 | my name is rick |
1 | { |
Tip:
By default, when amis is parsing the template string, it will try to parse the variable and replace the template variable when it encounters the $
character. If you want to output plain text ${xxx}
or $xxx
, you need to precede the $ Add the escape character \\
, that is, \\${xxx}
.
1 |
|
1 | { |
Support chain value
You can use .
For chain value.
1 | my name is rick, I work for baidu |
1 | { |
Custom API request body data format
In the form submission interface, the default request body data format of amis may not meet your expectations. Don’t worry, you can use Data Mapping to customize the data format you want:
Look at the following scenario:
1 | Form |
1 | { |
After entering name: rick
and email: [email protected]
, the form gets the current data field, and the data format submitted to the back-end interface should look like this:
1 | { |
Unfortunately, your back-end API only supports the following input data structures and cannot be modified:
1 | { |
At this time, in addition to directly changing the name attribute of your name form item and mailbox form item to the corresponding field, you can configure the data attribute of the api, and use Data Mapping to easily customize the data format:
1 | { |
You can check the Network panel, the data body sent to the back-end API should have been successfully modified to:
1 | { |
Complex configuration
Expand the configured data
You can use &
as the Data Mapping key to expand the configured variables, for example:
In the following example, we want to add all the variables e
, f
, g
under c
in addition to the name and email variables when submitting, but according to what we said before, the api should be configured like this:
1 | { |
Click Submit to view the network panel data, you will find that the data is in line with expectations:
1 | { |
But when there are too many variable fields, you need to map the configuration one by one, which may be a bit troublesome, so you can use the &
identifier to expand the configured variables:
1 | { |
In the above example, our api.data
configuration is as follows:
1 | "data": { |
The &
identifier will expand and concatenate the value of the configured c
variable in data. Looking at the Network panel, you can see the data as follows:
1 | { |
Array extraction value
1 | { |
The data configuration format of the api in the above example is as follows:
1 | "data": { |
This configuration means that only the a variable and the c
variable in the table array are extracted to form a new array and assigned to the items variable
Click Submit and check the web panel of the browser to find that the submitted data structure of the form is as follows:
1 | { |
Namespace
Since 1.1.6
The default value is to fetch data from the current component context Data Chain, but some data may not be in this Data Chain, such as some data in global variables, and some data in localstorage.
Starting from version 1.1.6, a new syntax is supported, such as: ${window:document.title}
which means to take the title of the page from the global variable.
There are currently the following three namespaces
-
window
is a global variable -
ls
is localStorage. If the value is a json object, it can be used directly as an object. For example: ${ls:xxxxxlocalStrorageKey.xxxx} -
ss
is sessionStorage, ibid.
1 | The current page title is: amis-low-code front-end framework |
1 | { |
Filter
Filter is an enhancement to Data Mapping. Its function is to do some processing on the acquired data. The basic usage is as follows:
1 | ${xxx [|filter1 |filter2...]} |
See Data Mapping | amis - 低代码前端框架 - https://baidu.github.io/amis/zh-CN/docs/concepts/data-mapping#%E8%BF%87%E6%BB%A4%E5%99%A8 to learn more Filter.
Use filters in chain
Using a single filter may not meet all your needs. Fortunately, amis supports the use of filters in series, and the value of the previous filter will be used as the input parameter of the next filter for the next step. The syntax is as follows:
1 | ${xxx|filter1|filter2|...} |
1 | { |
In the above example, ${value|split|first}
will go through the following steps:
-
The split filter will be executed first to split the strings
a
,b
,c
into arrays["a", "b", "c"]
; -
Then pass the data to the next filter first, execute the filter, and get the first element of the array, which is
a
-
Output
a
Custom filter
The registerFilter
method is exposed in the amis npm package, through which you can add your own filter logic.
like:
1 | import {registerFilter} from'amis'; |
After registration, you can pass ${xxx|count}
to return the length of the string.
If your filter still supports parameters, you can refer to this example.
1 | import {registerFilter} from'amis'; |
The usage is ${xxxx|my-replace:aaaa:bbbb}
Custom filter in JS SDK
1 | let amisLib = amisRequire('amis'); |
References
[1] Data Mapping | amis - 低代码前端框架 - https://baidu.github.io/amis/zh-CN/docs/concepts/data-mapping
[2] Template | amis - 低代码前端框架 - https://baidu.github.io/amis/zh-CN/docs/concepts/template
[3] API | amis - 低代码前端框架 - https://baidu.github.io/amis/zh-CN/docs/types/api
[6] baidu/amis: 前端低代码框架,通过 JSON 配置就能生成各种页面。 - https://github.com/baidu/amis