Low-code designer > Set up interfaces > Scripts in widgets / Script use cases

Script use cases

Widgets are a convenient tool for setting up dynamic forms, for instance, if some information needs to be displayed when certain conditions are met.

Let’s consider an example of how you can use scripts in widgets. We’ll create a page with a test consisting of several questions. The page will include expanding panels containing the questions, an information box with a warning, and a modal window displaying the test results.

widget-scripts-1

Expanding panel

By default, the questions will be collapsed, but as soon as the user clicks Start Test, the first question panel should expand.

To configure the panel, do the following:

  1. Open the page and go to the interface designer.
  2. On the Context tab, create a variable of the Yes/No switch type called Expand Question 1 (expand_question_1).
  3. Add the Panel with header widget to the modeling canvas.
  4. In the window that opens, set the following parameters:
    • Title*. Enter the panel’s name, Question 1.
    • Collapsible. Select Yes.
    • Expanded. Specify the variable Expand Question 1 that you created in step 2. With this variable in the condition, the panel will expand when the user clicks the button that is going to be added to the page.

widget-scripts-2

  1. Click Save.
  2. Add the Button widget to the modeling canvas.
  3. In the settings window that opens, set the following parameters:
    • Title/Tooltip*. Enter the button’s name, Start Test.
    • On click handler. Add a script that will change the value of the Expand Question 1 variable (expand_question_1) to true. To do that, click Create, then Open.

widget-scripts-3

On the Scripts tab that opens, write the following script:

async function expand_panel(): Promise<void>{
   Context.data.expand_question_1 = true
}

  1. Save the settings.

Information box

In the test, there is only one possible answer for each question. If the user selects several answers, an Information box with a warning will be displayed. The box will disappear when the user hovers the mouse over it.

To set up the information box, do the following:

  1. Open the page and go to the interface designer.
  2. On the Context tab, create a variable of the Yes/No switch type called One answer? (one_answer).
  3. Add the Information box widget to the modeling canvas. In the settings window that opens, in the Hide field, select Show on condition. Then specify the One answer? variable. Whether the information box is shown will depend on the value of this variable.

widget-scripts-4

  1. The number of answers selected by the user needs to be checked. To set up the checking, do the following:
    • Add a context property containing the question and the possible answers to the Panel with header widget. On the page, the property will be displayed as the Field widget.
    • Select the Field widget and open its settings by clicking the gear icon:

widget-scripts-5

    • Go to the Events tab and add a script that will check the number of selected answers and change the value of the One answer? variable. To do that, click Create, then Open.
    • The Scripts tab in the interface designer will open. Add a script that will define the value of the One answer? (one_answer) variable, for example:

async function checkQ1(): Promise<void> {
   if (Context.data.n1 && Context.data.n2 && Context.data.n3) {
       Context.data.one_answer = false
   }
       else if (Context.data.n1 && Context.data.n2) {
       Context.data.one_answer = false
       }
           else if (Context.data.n1 && Context.data.n3) {
           Context.data.one_answer = false
           }
               else if (Context.data.n2 && Context.data.n3) {
               Context.data.one_answer = false
               }
                   else if (!Context.data.n1 && !Context.data.n2 && !Context.data.n3) {
                   Context.data.one_answer = false
                   }
                       else {
                       Context.data.expand_panel_v1 = false;
                       Context.data.expand_panel_v2 = true;    
                       }
}

  1. In the settings of the Information box widget, go to the Events tab and add a script to the On mouse enter handler. This script should change the value of the One answer? (one_answer) variable to true:

async function closeInformationBox(): Promise<void> {
   Context.data.one_answer = true  
}

Pop-up window

When the user finishes the test, they click the Check Results button. After that, the page will show a pop‑up window with the test results.

To configure the pop-up window, do the following:

  1. Open the page and go to the interface designer.
  2. On the Context tab, create a variable of the Yes/No switch type called Correct? (correct). It will store the test results.
  3. Add the Modal window widget to the modeling canvas.
  4. In the widget’s settings, in the Show window field, specify the variable created in step 2. Whether the modal window is shown will depend on the value of this variable.

widget-scripts-6

  1. Add the Button widget to the modeling canvas.
  2. In the button’s settings, in the On click handler, add a script that will change the value of the Correct? (correct) variable to true:

async function testResult(): Promise<void> {
   if (Context.data.n2 && Context.data.n2_2008 && Context.data.c_11 && Context.data.elma_365 && Context.data.rpa) {
       Context.data.correct = true
   } else {
       Context.data.incorrect = true
   }  
}

Found a typo? Highlight the text, press ctrl + enter and notify us