-
Notifications
You must be signed in to change notification settings - Fork 11
Updated displayif function
In order to simplify using the displayif logic, we have started using math.js the evaluate the display if logic. In order to use the new functionality, you must start your displayif with one of the new functions.
You cannot mix the old functions with these new functions.
These functions should check if a value is either in the DOM or in the previously filled values. So the current module should check the responses of previous modules (if you passed the old values in).
- exists("elementid")
- the exist function looks for a response with id=elementid and returns true if the user selected the response or the response is longer than 0 characters (javascript "truthy"). If the element does not exist, the function return false. Expert note: false/0 responses are strings so they are not falsy in this case
- doesNotExist("elementid")
- The doesNotExist function looks for a response with id=elementid and returns true if the element does not exist or if the element is not selected or has the response has a length of 0 characters
- noneExist("elementid1","elementid2",...)
- The noneExist function returns true if none of the provided ids exists (see doesNotExist("elementid"))
- someExist("elementid1","elementid2",...)
- The someExist function returns true if at least one of the elements exist (see exists("elementid"))
- allExist("elementid1","elementid2",...)
- The allExist function returns true if every of the elements exist (see exists("elementid"))
- equals("elementid","value")
valueEquals("elementid","value") - Returns true if the value of the element with id=elementid equals value. If no value is provided, the function returns false
- valueIsOneOf("elementid","value1","value2",... )
- Returns true if the value of the element with id=elementid is one of the values provided. See valueEquals
- valueIsBetween: function (lowerLimit, upperLimit, ...ids)
- checks that the value of id is between the lower and upper limits. If more than 1 id is given, all of the ids are passed into valueOrDefault to select the appropriate value
- existingValues(...args)
- This function returns a string of all the values where a condition is true along with an option separator. The arguments to this function are pairs of predicates (True/False functions) and values. Here is an example:
existingValues(exists("ID1"),"ID1 exists",exists("ID2"), sep=" | ")
which returns the string:ID1 exists | ID2 exists
- valueLength(id)
- If the value of id is a string, returns the number of characters in the value. Otherwise, returns -1
- dateCompare(month1,year1,month2,year2)
- Compares the dates month1/year1 to month2/year2. If date1 is earlier, return -1, If date2 is earlier, return 1. If the dates are the same, return 0. Dates must be numeric. Months go from 0 (Jan) to 11 (Dec). This is the browser default counting, not quest choosing.
- isSelected(id)
- Returns true if the value of the element with id=elementid is selected. Warning: Currently, this looks in the DOM for the element id. Try to only use it for the current question. To speed up quest, we would like only parse the current question. The id make not exist once you move to the next question. This issue may or may not be resolved when we make the change.
- someSelected("id1","id2"...)
- Returns true if any of the element values passed in are selected. See the warning in isSelected(id)
- selectionCount("id")
- Assuming that you have a select all that apply question, this returns the number of options selected. See the warning in isSelected(id)
- valueOrDefault("x","id1","id2"...)
- if x exist, return the value of x. Otherwise, work through the other ids looking for a valid value. If not of the ids have a valid value, return the last id as string value. Because valueOrDefault expects Ids, you cannot have expressions in the function, expect for the last value, which will be returned as a string.
For example, if x is not defined and id2=3,
valueOrDefault("x",_value("id2")-1)
returns "2" - loopValueIsOneOf(id,...values)
- for an id that is in a loop, does the response for any iteration of the loop contain any of the values.
- gridQuestionsValueIsOneOf(gridId,...values)
- for an grid, does the response for any of the grid questions contain any of the values.
[Q1] Age |__|__|id=YEARSOLD min=0 max=valueOrDefault("AGE","age",99)| [END] Thank you
In the example above, since both AGE and age are not defined, the value 99 is returned as the max for the age. Had either AGE or age been defined, the appropriate value would have been returned.
Since we are using math.js, simple math is available. However, it may not be available in all cases because the markdown is heavily processed before calling math.js AND because the markdown is only evaluated by math.js in special cases. We specifically check that the markdown contains either a math.js backed function or only basic math operators (+, -, /, *)
Three cases where the markdown is evaluated are in the 1) displayIf and for numeric 2)input min and 3) max checking. Other cases may arise as needing. Here is an example, the logic is that if you are 22 in in 2022, you could have been born in 2000 -- if your birthday passed -- or in 1999 -- if you have not had your birthday yet.
[Q1] How old are you? |__|__|min=10 max=125|
[Q2] What year where you born? |__|__|min=#currentYear-_value('Q1')-1 max=#currentYear-_value('Q1')|
You can pipe simple math to a question using {# } like {# #currentYear - 10}