Skip to content

Commit bd8c98b

Browse files
Openblocks-docsgitbook-bot
Openblocks-docs
authored andcommitted
GitBook: [lowcoder-org#99] No subject
1 parent 275a6be commit bd8c98b

7 files changed

+112
-0
lines changed
Loading
Loading
Loading
Loading
81.1 KB
Loading

‎docs/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* [App editor](build-apps/app-editor.md)
2727
* [Event handlers](build-apps/event-handlers.md)
2828
* [Write JavaScript](build-apps/write-javascript/README.md)
29+
* [Write JavaScript in \{{ \}}](build-apps/write-javascript/write-javascript-in.md)
2930
* [Transformers](build-apps/write-javascript/transformers.md)
3031
* [Temporary state](build-apps/write-javascript/temporary-state.md)
3132
* [Use third-party libraries](build-apps/use-third-party-libraries.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Write JavaScript in \{{ \}}
2+
3+
When building apps in Openblocks, you can use JavaScript (JS) to access and transform data from objects, including components, queries, and global parameters. When writing JavaScript inside SQL editor, component property's input box, table column settings, etc., always remember to enclose all your JS code inside double curly braces, such as `{{'hello, ' + currentUser.name}}`.
4+
5+
## Access data
6+
7+
Objects have globally unique names, such as `input1`, `query1`, and `table1`. You can refer to the properties of the objects in your app by JS code.
8+
9+
### Access data in an object
10+
11+
Openblocks supports you accessing the data in an object using dot notation (`objectName.keyName`). For example,`{{userInfo.selectedRow.userName}}` accesses the `userName` value in the currently selected row of **Table** `userInfo`.
12+
13+
When writing JS in `{{ }}` to access values in an object, add a `.` after the object name to trigger an autosuggest menu in case you are not sure about objects' built-in properties or methods.
14+
15+
**Example**
16+
17+
This GIF shows how the dot notation triggers an autosuggest menu and displays the properties of `table1`.
18+
19+
<figure><img src="../../.gitbook/assets/access-data-in-an-object.gif" alt=""><figcaption></figcaption></figure>
20+
21+
### Access data in an array
22+
23+
You can access the values in an array by index. The index always starts at 0, so you can use `array[0]` to access the first element of the array.
24+
25+
**Example**
26+
27+
The **Data** property of **Table** component is an array of objects. This GIF shows how to access the value of `first_name` in the first element of the **data** array in `table1`.
28+
29+
<figure><img src="../../.gitbook/assets/access-data-in-an-array.gif" alt=""><figcaption></figcaption></figure>
30+
31+
## Transform data
32+
33+
You can leverage built-in JS functions and third-party libraries in `{{ }}` to transform data, such as `filter()`, `map()` and `reduce()` operations.
34+
35+
**Examples**
36+
37+
Lowercase a string.
38+
39+
```javascript
40+
{{input1.value.toLowerCase()}}
41+
```
42+
43+
Change date format.
44+
45+
```javascript
46+
{{moment(table1.selectedRow.date_column).format('YYYY-MM-DD')}}
47+
```
48+
49+
Return name from query results.
50+
51+
```javascript
52+
{{query1.data.map(i => i.name)}}.
53+
```
54+
55+
## Restrictions
56+
57+
The JS code in `{{ }}` should be a single-line code, such as `.map()` or `.reduce()` combined with an arrow function or a ternary operator.
58+
59+
**Examples**
60+
61+
```javascript
62+
{{query1.data.id.length}} // ✅ to reference a value
63+
{{query1.data.map(row => row.id)}} // ✅ .map() + arrow function
64+
{{ num1 > num2 ? num1 : num2 }} // ✅ ternary
65+
```
66+
67+
The following JS code examples are illegal in `{{ }}`.
68+
69+
```javascript
70+
{{
71+
// ❌ you can't write code like this in {{ }}
72+
const array = query1.data;
73+
const filterArray = array.filter(it => it.value > 10);
74+
return filterArray;
75+
}}
76+
```
77+
78+
If you wish to orchestrate multiple lines of JavaScript, Openblocks supports you writing such code in [transformers](transformers.md).
79+
80+
```javascript
81+
// codes inside a transformer
82+
if (select.value === "1") {
83+
return "Option 1";
84+
}
85+
if (select.value === "2") {
86+
return "Option 2";
87+
}
88+
return "Option 3";
89+
```
90+
91+
## View data
92+
93+
Data from queries can be complicated and nested in real cases. Viewing data provides you with the detailed structure of data in objects and helps you understand your data better. Before accessing or transforming data, you may need to view the data and its structure first. Openblocks offers three ways to view data.
94+
95+
### View query result
96+
97+
After running a query inside the query editor by clicking the **Run** button**.** Query result is displayed in the format shown below.
98+
99+
<figure><img src="../../.gitbook/assets/view-query-result.png" alt=""><figcaption></figcaption></figure>
100+
101+
### View data in Data Browser
102+
103+
Data browser located in the left pane displays all of the data inside your app. You can click on the node to expand and view the data structure.
104+
105+
<figure><img src="../../.gitbook/assets/view-data-in-data-browser.png" alt=""><figcaption></figcaption></figure>
106+
107+
### Real-time view
108+
109+
When setting up properties or writing JS code inside an editor, you can view the evaluated result in real-time in a box below your editor.
110+
111+
<figure><img src="../../.gitbook/assets/view-data-in-real-time.png" alt=""><figcaption></figcaption></figure>

0 commit comments

Comments
 (0)