Change the color automatically using a state collection
This example shows how to use a state collection to automatically change the background color of a control depending on the state represented by a variable value.
In this example, you are going to add a StateIndicator control to a page and add a VariableTrigger with the desired variable and a SetPropertyAction with a reference to a state collection to the StateIndicator. To change the variable value at runtime and to observe the effect on the StateIndicator, you are going to add a NumericVarIn to the page and bind the same Variable to the control as was indicated in the VariableTrigger of the StateIndicator.
As a result, the control will show the color associated with the respective value in the state collection.
The background color of the StateIndicator changes depending on the variable value.
To achieve this result, do the following:
-
Start EASY UI Designer and create a new project.
-
Create a new internal variable that will represent the state.
In a realistic scenario, you would rather use a process variable from the controller.-
In the Project Explorer, expand the nodes [Project name]_Server > Variables > Channels and double-click on the Internal node to open the table editor for variables.
-
To create a new numeric variable, right-click on the table editor and select New from the context menu.
-
-
Add a new state collection to colorcollections.json and define the following states:
Value Theme color <0 ErrorColor
0 ChromeMediumHigh
>0 SuccessColor -
In the Project Explorer, expand the nodes [ProjectName]_Client > Design > pages and open Page1.html in the designer.
-
Add a StateIndicator to the page in the designer.
-
Add a VariableTrigger to the StateIndicator and configure the trigger as follows:
Property Value VariableName Variable1
VariableProperty Value
-
Add a SetPropertyAction to the trigger and configure the action using the settings in the Condition category as follows:
-
From the set property drop-down list, select BackgroundColor.
-
Click on the advanced options symbol (
) to the right of the to field and select Select State Collection... from the menu. -
In the dialog that appears, select the desired state collection and click OK to confirm.
-
-
Add a NumericVarIn to the page and select the same variable in the VariableName property as you have indicated in the trigger, which is Variable1.
-
Press Ctrl+S to save the changes.
You can skip this step if you enable the Save documents automatically option, which you can find in the Options dialog under Designer > Documents. -
Press F5 to test the application.
If you type a value below zero into the NumericVarIn field on Page1, the color of the StateIndicator switches to red, if you type a value above 0, the color is green; if you type 0, it is gray, as indicated in the state collection.
Related topics: