This example shows how to show or hide an attribute control based on the value of another attribute control without the need for submitting the screen.
First create a project that comprises a screen with two questions so we can hide/show one question based on the value of the other question.
To create the rulebase, do the following:
the person is eligible for maternity leave if
the person is pregnant
shortcut rule
the person is not pregnant if
the person's gender = "male"
To create the screens, do the following:
We will now add some custom properties that will tell us how the visibility of one attribute is dictated by another one. Based on whether the person chooses their gender as male or female, the question about whether the person is pregnant or nor either hidden or shown. If male, the question about being pregnant is no longer applicable. We do this by adding the following three custom properties.
Next, go to File -> Project Properties -> Screen and add the following property name-value pairs to the screen:
Name |
Default_value |
---|---|
AttributeToCheck | gender |
AttributeToShowOrHide | person_pregnant |
ExpectedAttributeValue |
female |
The sample rulebase can be found at the following location in either the Java or .NET runtime zip file: examples\rulebases\source\ShowHideAttributeExample.zip.
The Velocity templates need to be modified to recognize when a person chooses answer to one question, and based on that either hide or show the second question. The files for this example can be found at examples\web-determinations\custom-show-hide-control in both the Java and .NET runtime zip files
The customization comprises the following files:
File |
Description |
---|---|
templates/investigation/form.vm | This is the modified version of form.vm from the web-determinations template. This collects the custom properties set for a screen. If the control being displayed is the same as the one associated with AttributeToCheck, it attaches a click handler to the control. When an attribute value is selected, the click handler gets fired and sets the visibility of the control associated with AttributeToShowOrHide based on the selected value and the expected value. |
templates/javascript/clickHandler.js | Javascript code for attaching the click handler and controlling the visibility. |
templates/question_screen.vm |
Modified to include the javascript file. |
To run the example use the ShowHideAttributeExample project under examples\rulebases\source in the runtime zip file, do the following: