Buy Now
Sign in

FAQ search

Creating a toggle button

Published: Sunday, 28 May 2017 07:32

A toggle button will toggle a database field value, in order to have a full functioning toggle button, we need to have 3 things:

  1. Button view to display the button itself
  2. Save data function to update the database record
  3. A toggle event to be called by the button click, an event is required whenever a function call is needed.

The button view

Go to the Views section and click "Elements" then "Task button", this will create a new view of type task button.

We want the button to have a green color and the text "Enabled" when the toggled field value is "1", but red color and the text "Disabled" when the toggled field value is "0", and so we can not use static text values, we will need to get dynamic text based on a vraiable, for this we will need to use a switch function.

Go under "Functions" and click "Logic" then "Switch", this will add a new "Switch" function, the switch function needs a data provider to use for doing the switching, and after doing the switching it will return a result wherever it has been called.

Back in our button view, we will call the switch function inside the "Content" box in order to run the switch function and return the correct the text, but we also need to provide the function with the value to use for switching, let's assume that our button view will be called inside a table, then this will be our function call: {fn:switch_text_name$enabled=(var:table1.row.Article.state)}

Back in the Switch function we can now set the data provider to {var:enabled} (because we passed the field value under that variable), and in the "Values setup" we need to set the output for each desired value:


Now its time to point our button to the toggle event, so write "toggle" in the "Event" box of the button, then crete a new event named "toggle" under "Events".

In the toggle event we need to update the record and redirect back.