Using client state parameters in UI Builder
Create a simple counter by adding the stylized text component and two buttons to an experience page. Use a client state parameter to implement the functionality so that when the buttons are selected the count increases or decreases.
Avant de commencer
Role required: ui_builder_admin
Procédure
- Navigate to All > Now Experience Framework > UI Builder.
-
Open an experience to work in or create an experience by selecting Create > Experience.
See Configure how users interact with your applications in UI Builder for more information on creating experiences.
- Create a page in UI Builder or open a page.
- Add a column layout by selecting + Add content in the content tree under Body.
- On the Layouts tab, select Two columns.
-
Add the first component by selecting + Add content in the content tree under Column 1.
- In Search, type sty.
-
Select the Stylized text component.
-
In the configuration panel, select None - Configure the component manually.
-
Add the second component by selecting + Add content in the content tree under Column 2.
- In Search, type button.
-
Select Button Iconic.
- In the configuration panel, select None - Configure the component manually.
-
In Icon, select Square Arrow Up Fill.
-
Add the third component by pointing to Button iconic 1 in the content tree, selecting the Menu icon, and selecting Add after.
- In Search, type button as you did in the previous step.
- Select Button Iconic as you did in the previous step.
- In the configuration panel, select None - Configure the component manually as you did in the previous step.
-
In Icon, select a different icon this time, the one named Square Arrow Down Fill.
-
Select Save.
The stylized text component and the two button iconic components are saved and appear on the stage.
-
Add a client state parameter by going to Data and scripts, pointing to Client state parameters, and selecting the Add new (plus) icon.
- Change the Name by entering count.
- Change the Type to Number.
- Set the Initial value by entering the number 0.
-
Select the X to close the window.
-
Bind the stylized text component to the client state parameter.
- Select the stylized text component on the stage.
-
In the configuration panel, point to the Text field and select the Bind data or use scripts icon.
- On the Data types tab, select Client states.
- Double-click on the count pill.
-
Select Apply.
The Text field changes to count.
-
Now configure the buttons to increase or decrease the number.
- Select the top button on the stage.
- In the configuration panel, select the Events tab.
- Select + Add event handler.
- Under Page-level event handlers, select Update client state parameter.
- In Client State Parameter Name, select count.
-
Point to the New Value field and select the Bind data or use scripts icon.
- On the Data types tab, select Client states.
- Double-click on the count pill.
- Select the Formulas tab.
- In the list, select Math.
- Double-click on the ADD pill.
-
In the right pill at the top, remove the text and enter 1.
- Select Apply.
- Select Add.
-
Now configure the second button by following the same process that you used for the first button, but select the SUB pill instead of the ADD pill.
- Select the bottom button on the stage.
- In the configuration panel, select the Events tab.
- Select + Add event handler.
- Under Page-level event handlers, select Update client state parameter.
- In Client State Parameter Name, select count.
- Point to the New Value field and select the Bind data or use scripts icon.
- On the Data types tab, select Client states.
- Double-click on the count pill.
- Select the Formulas tab.
- In the list, select Math.
- Double-click on the SUB pill.
- In the right pill at the top, remove the text and type 1.
- Select Apply.
- Select Add.
- Test the counter by selecting Preview.
-
Select the up arrow button to increase the count and the down arrow button to decrease the count.
Résultats
You added a button component to increase the count by one and another button component to decrease the count by one. You added the stylized text component to show the count as it increased and decreased.
For detailed, technical information about the button component, see Button Overview & API.
For detailed, technical information about the stylized text component, see Stylized Text Overview.