You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+51-3
Original file line number
Diff line number
Diff line change
@@ -26,7 +26,7 @@ npm i directus-extension-computed-interface
26
26
-**Read Only**: Show an input with the computed value and disallow manual editing.
27
27
3.**Prefix**: a string to prefix the computed value.
28
28
4.**Suffix**: a string to suffix the computed value.
29
-
5.**Custom CSS**: an object for inline style binding. Only works with **Display Only** and **Read Only** mode. You can use this option to customize the appearance of the computed value such as font size, color, etc.
29
+
5.**Custom CSS**: a JSON object for inline style binding. Only works with **Display Only** and **Read Only** mode. You can use this option to customize the appearance of the computed value such as font size, color, etc. Example: `{"color": "red", "font-size": "20px"}`.
30
30
6.**Debug Mode**: Used for debugging the template. It will show an error message if the template is invalid. It will also log to console the result of each component of the template.
31
31
7.**Compute If Empty**: Compute the value if the field is empty. This is useful if you want a value to be computed once such as the created date or a unique ID.
32
32
8.**Initial Compute**: Compute the value when opening the form. This is useful if you want to compute a value based on the current date or other dynamic values.
@@ -68,6 +68,19 @@ Literal strings are enclosed by double quotes (`"`):
68
68
{{ CONCAT(file, ".txt") }}
69
69
```
70
70
71
+
Use `.` to access nested fields in M2O or M2M fields:
Combine `AT`, `FIRST`, `LAST`, `JSON_GET` to access nested fields in O2M or JSON fields:
77
+
```
78
+
{{ JSON_GET(AT(products, 0), "name") }}
79
+
{{ JSON_GET(LAST(products), "price") }}
80
+
```
81
+
82
+
**Note**: For M2O, O2M, M2M fields, you can only access the fields of the direct relation. For example, if you have a `user` field that is a M2O relation to the `users` collection, you can only access the fields of the `users` collection. You cannot access the fields of the `roles` collection even though the `users` collection has a M2O relation to the `roles` collection. On the other hand, JSON fields have no such limitation!
83
+
71
84
## Available operators
72
85
73
86
### Type conversion
@@ -102,6 +115,7 @@ Operator | Description
102
115
`MINUTES(a)` | get minutes of a date object, similar to `getMinutes`
103
116
`SECONDS(a)` | get seconds of a date object, similar to `getSeconds`
104
117
`TIME(a)` | get time of a date object, similar to `getTime`
118
+
`LOCALE_STR(a, locale, options)` | transform date or date-like object to string with locale format, `options` is a stringified JSON object. Example: `LOCALE_STR("2023-01-01", "en-US", "{\"weekday\": \"long\", \"year\": \"numeric\", \"month\": \"long\", \"day\": \"numeric\"}")` returns "Sunday, January 1, 2023".
105
119
106
120
### Arithmetic
107
121
@@ -132,7 +146,11 @@ Operator | Description
132
146
133
147
Operator | Description
134
148
--- | ---
135
-
`STR_LEN(str)` | length of string
149
+
`STR_LEN(str)` | length of string (deprecated, use `LENGTH` instead)
150
+
`LENGTH(str)` | length of string
151
+
`FIRST(str)` | first character of string
152
+
`LAST(str)` | last character of string
153
+
`REVERSE(str)` | reverse string
136
154
`LOWER(str)` | to lower case
137
155
`UPPER(str)` | to upper case
138
156
`TRIM(str)` | removes whitespace at the beginning and end of string.
@@ -147,6 +165,10 @@ Operator | Description
147
165
`SEARCH(str, keyword)` | search `keyword` in `str` and return the position of the first occurrence. Return -1 if not found.
148
166
`SEARCH(str, keyword, startAt)` | search `keyword` in `str` and return the position of the first occurrence after `startAt`. Return -1 if not found.
149
167
`SUBSTITUTE(str, old, new)` | replace all occurrences of `old` in `str` with `new`.
168
+
`AT(str, index)` | get character at `index` of `str`.
169
+
`INDEX_OF(str, keyword)` | get the position of the first occurrence of `keyword` in `str`. Return -1 if not found.
170
+
`INCLUDES(str, keyword)` | check if `str` contains `keyword`.
171
+
`SLICE(str, startAt, endAt)` | extract a part of `str` from `startAt` to `endAt`. `endAt` can be negative. Similar to `slice` method of `String`.
150
172
151
173
### Boolean
152
174
@@ -168,7 +190,27 @@ Operator | Description
168
190
169
191
Operator | Description
170
192
--- | ---
171
-
`ARRAY_LEN(a)` | length of array
193
+
`ARRAY_LEN(a)` | length of array (deprecated, use `LENGTH` instead)
194
+
`LENGTH(a)` | length of array
195
+
`FIRST(a)` | first element of array
196
+
`LAST(a)` | last element of array
197
+
`REVERSE(a)` | reverse array
198
+
`CONCAT(a, b)` | concat 2 arrays `a` and `b`.
199
+
`AT(a, index)` | get element at `index` of `a`.
200
+
`INDEX_OF(a, element)` | get the position of the first occurrence of `element` in `a`. Return -1 if not found.
201
+
`INCLUDES(a, element)` | check if `a` contains `element`.
202
+
`SLICE(a, startAt, endAt)` | extract a part of `a` from `startAt` to `endAt`. `endAt` can be negative. Similar to `slice` method of `Array`.
203
+
`MAP(a, expression)` | apply `expression` to each element of `a` and return a new array, each element of `a` must be an object. Example: `MAP(products, MULTIPLY(price, quantity))` returns an array of total price of each product.
204
+
`FILTER(a, expression)` | filter `a` with `expression` and return a new array, each element of `a` must be an object. Example: `FILTER(products, GT(stock, 0))` returns an array of products that are in stock.
205
+
`SORT(a, expression)` | sort `a` with `expression` and return a new array, each element of `a` must be an object. Example: `SORT(products, price)` returns an array of products sorted by price.
206
+
207
+
### JSON
208
+
209
+
Operator | Description
210
+
--- | ---
211
+
`JSON_GET(a, key)` | get value of `key` in JSON object `a`.
212
+
`JSON_PARSE(a)` | parse string `a` to JSON object.
213
+
`JSON_STRINGIFY(a)` | stringify JSON object `a`.
172
214
173
215
### Relational
174
216
@@ -190,6 +232,12 @@ Operator | Description
190
232
`IF(A, B, C)` | return `B` if `A` is `true`, otherwise `C`
191
233
`IFS(A1, B1, A2, B2, ..., An, Bn)` | return `Bi` if `Ai` is the first to be `true`, if none of `Ai` is `true`, return `null`
192
234
235
+
### Others
236
+
237
+
Operator | Description
238
+
--- | ---
239
+
`RANGE(start, end, step)` | create an array of numbers from `start` to `end` with `step` increment/decrement. Example: `RANGE(1, 10, 2)` returns `[1, 3, 5, 7, 9]`.
240
+
193
241
## Dynamic Variables
194
242
195
243
There are 2 dynamic variables available that you can use in the expressions:
0 commit comments