Simple Toggle Buttons in FileMaker 14
Recently I had a need for a simple toggle button. In the past I would have used object visibility (FMP13) or conditional formatting (FMP12 and earlier) to show or hide two or more buttons based on a variable or field. This time I took a minute to consider how I could use the new features in FileMaker 14, particularly the Button Bar.
Download the demo file:
Example 1. Toggle using a global variable
I started by creating a simple script that would toggle between Boolean values. The formula goes something like this:
Abs ( $$simpleToggle - 1 )
This formula will not only toggle between zero (0) and one (1) but will also take care of a Null value when the variable is empty. After all, the absolute value of nothing minus one is one.
”” - 1 //equals -1
Abs ( -1 ) //equals 1
I created a Button Bar with two segments and attached the script to both buttons. I selected the empty star and filled star SVG icons to display the toggled state.
The next step was to make sure that only one of these two segments was visible at any given time. I used object visibility to handle this. One of the great things about Button Bars in FileMaker 14 is that when one segment is hidden, other segments move in to fill up its space.
At this point there was only one problem left. Global variables do not trigger object visibility calculations to update, so I added a Refresh Window script step. The script now looks like this:
Set Variable [ $$simpleToggle ; Abs ( $$simpleToggle - 1 ) ]
Refresh Window []
Example 2. Toggle using a global field
In the second example I used the same concepts above, but this time I created a global field to house the Boolean value. I duplicated the script from Example 1 and replaced the Set Variable step with a Set Field step. Using a field instead of global variable allowed me to remove the Refresh Window script step, as fields DO trigger object visibility calculations to update. I created a second button bar modeled after the first one and set it up to work with the new global field.
Example 3. Toggle a normal number field
A toggle based on a global variable or global field is ok, but it has limited utility. In this example I used the script and Button Bar from example 2 as a template. I replaced the global field with a number field from a normal table, which is what I was after from the beginning.
TL;DR
Boolean values + button bar + object visibility = Simple toggle buttons in FileMaker.
Clear & concise. Thanks. I particularly appreciate the explanation of what does & does not trigger the “hide when” calc refresh.
A (slightly) simpler formula that I have been using is
not Table::ToggleField
I’m sure my version is not measurably faster or slower, but it is easier to type.
Thanks Marc. For a more detailed explanation of the calculation dependency tree see this article by Darren Terry.
http://filemakerhacks.com/2014/08/08/shaking-the-dependency-tree/
I liked your explanation but it only works when you have one button on the layout.
What would you do if you have 15 buttons?
Would you use repeating functions to accomplish it?
Hi Zillo, Are you talking about having 15 toggle buttons on a layout? You could use the field level version from example 3 with a different field backing each button. You could also do this with repeating variables. For example:
Set Variable [ $$simpleToggle[2] ; Abs ( $$simpleToggle[2] - 1 ) ]
I would lean toward the field level version, assuming that you have 15 toggles for which you need to store data.