Customers Admin Page
In this step, we will build a page that will interact with Data² Table, a database where important information for our applications is stored.
This page will perform read, write, update, and delete operations. These operations have a very commonly used acronym called CRUD (Create, Read, Update, Delete). For viewing (reading), we will use a table block provided by Data² called DataGridBlock
, and for the other operations, buttons and text controls will be used.
The end result will be an Artboard
similar to this:
This Artboard
has:
- A table so you can view and select your customers.
- Two text fields linked to the Name and Email of the selected customer.
- Insert Button
- Edit Button
- Save Button
- Cancel Button
- Delete Button
Creating a Data² Table
To create a Data² Table, double-click on the Builder
background, and in the menu select the Registry
tab in the left sidebar. Click Table
to add the Data² Table.
Click on the Table
name to select it. Let's change its name to know what it is about. In the Inspector
, at the top there is a text box to change the Display Name
of the table, put Customer table
.
It is necessary to configure which fields we will save from our customers. Let's create two fields: name
and email
.
- With the
Table
selected, in theInspector
click onSchema
and theInspector
will "navigate" into that property. - Click on
Add Item
. - In
Title
writename
. - Under
Component
, selectdata2-core-primitives@String
. - Add another field, this time with
email
asTitle
, and usedata2-core-primitives@String
asComponent
again.
Creating a DataSet
We interact with the Table
through the DataSet
. To create a DataSet
for the first time, click on the bottom of the table. To create other DataSets, click on the +
at the bottom of the table.
Select the DataSet
you are going to use, clicking on its name at the bottom of the table (next to the +
), and in the Inspector
change the name to All Customers
.
Creating a page linked to DataSet
Add a new Artboard
by double clicking on the background and selecting Artboard
from the menu.
Adding the table block
Select the Block Tool
and draw a larger block to contain the table, inputs and buttons.
Draw a block to be the table inside the previously drawn block. To change the block type, go to the Inspector
and click on the top part where there is the text data2-core-primitives@Block
. A menu will open, look for DataGridBlock
and select that option.
To configure the DataGridBlock
parameters, hiding the Block
parameters makes it easier, you can do that by clicking on the arrow in the section titled Block
.
Enable the Dataset
parameter and select the All Customers
DataSet.
Adding the text fields
Now use the Block Tool
to draw two blocks below the table.
Select both blocks at the same time by selecting one, holding SHIFT
and clicking on the second. Then, switch data2-core-primitives@Block
to DataFieldBlock
.
Still with both DataFieldBlock
s selected, let's set the DataSet
parameter to All Customers
.
Using the Selection Tool
, click on another block to deselect the two DataFieldBlock
. Select the first text field and set the Field
to be All Customers -name
. In the second DataFieldBlock
, set Field
to be All Customers - email
.
Adding the buttons
Add another block with the text Insert
. This time, let's configure the On Click
parameter, that is, what should happen when the block is clicked. In Module
select All Customers
, and in Interaction
select Insert
.
Do the same process to create buttons for the Interaction
s:
Save
Edit
Cancel
Delete
Tip: You can hold ALT
and drag a block to duplicate it.
At the end of this process, we have:
Testing what we've built so far
Click the Preview
button under Artboard
and try to:
- Click on insert, add information and save. When refreshing the page, the saved data should appear in the table.
- Insert another customer and save.
- Selecting a customer from the table should change the value of the text fields.
- Select a customer, click on
Edit
, change a field and save.
- Selecting a customer, clicking on
Edit
, changing a field and clicking the Cancel button should return to the original value.
- Select a customer and delete.
If all these cases worked, the page is working fine!
Routing
Now that we have two Artboards
, we would like the /
route to show our home page, and the /customers
route to show the client administration Artboard
. For this, we will need to:
- Create a
Router
, which will be used for creating routes. - Use the
Router
as theEntry Point
of theApplication
.
Adding the Router
To add a Router
, double click on the background of Builder
. In the Ui
tab, the same one where the Artboard
and Application
are located, click on Router
.
Configuring the Router
Give your Router
a name like Main Router
by changing the Display Name
in the Inspector
.
Then put /
in its Initial Path
.
Click on Routes
to configure your Router
routes. Double-click Add Item
to add two routes, one for the homepage, and one for the customer administration page.
Let's configure the home page route, to do that, click on the first item that was added. Two parameters will be configured:
- In the
Path
parameter put/
- In the
Content
parameter, click on the circle to enable the parameter and select theHomepage
Artboard
.
To get back to the list of routes, click on Routes
just above the Route
section and then click on the second route.
- In the
Path
parameter put/customers
- In the
Content
parameter, click on the circle to enable the parameter and select theCustomer Administration
Artboard
.
To save, deselect your Router by clicking on the Builder
background.
Using the Router
as the Entry Point
of the Application
Select the Application
and change its Entry Point
:
- In the
Component
parameter, changedata2-core-primitives@Artboard
todata2-core-primitives@Router
. - In
reference
, select the name of theRouter
created previously, in this case,Main Router
.
Preview
the Application
. Initially it should open the homepage. When changing the URL from /?d2_pl-prv=true
to /customers?d2_pl-prv=true
, the customer administration page should appear.
Adding a link between pages
To add a link from the home page to the customer administration page:
- Draw a block that will be used as a link
- In the
On Click
parameter, select yourRouter Main
asModule
, and in theInteraction
selectgoTo
. - In the
Path to Route
parameter, put thePath
of your client administration page, which in this case is/customers
. - Put a text also to know where the link goes when clicked, you can double click on the block to write directly on it.
Do the same to add a client admin Artboard
link to the home page, with one difference:
- In the parameter
Path to Route
put/
instead of/customers
.
Preview
your Application
and test if the links are working!