-
Templates
-
Simple Questions
-
Text Entry
-
Date-Time Entry
-
Numeric Entry
-
Color Input
-
Radio Button Group
-
Radio group with Custom Item Template
-
Drop-Down Menu
-
Dropdown: Load Data from Web Services
-
Dropdown with Custom Item Template
-
Dropdown with Lazy Loading
-
Multi-Select Dropdown
-
Checkboxes
-
Carry Forward Responses
-
Image Picker
-
Yes/No Question
-
Signature Pad
-
Multiple Text
-
Rating
-
Ranking
-
Comment
-
Image
-
Html
-
File
-
Expression
-
Expression (using async functions)
-
-
Matrix Table Questions
-
Panel & Dynamic Panel
-
Survey
-
Title and Logo
-
Questions in one line
-
Survey Options
-
Auto-Populate Form Fields
-
Share data between questions
-
Edit saved survey
-
Read-only/display mode
-
Show Preview before complete
-
Pop-Up Survey
-
Context actions in element titles
-
Modify title tags
-
Custom render of survey elements
-
Go next page automatically
-
File - custom preview
-
File - delayed upload
-
Lazy questions rendering
-
-
Quizzes and Scored Surveys
-
Integration with Third-Party Libraries
-
Appearance customization
-
Navigation
-
Conditions and Triggers
-
VisibleIf
-
Simplify Cascade Conditions
-
Complex questions in expressions
-
Use custom function in expressions
-
Create custom condition/expression properties
-
Conditions in dynamic questions
-
Show/Hide choices in radiogroup/checkbox/dropdown
-
Show/Hide individual items in radiogroup/checkbox/dropdown
-
Show/Hide columns/rows in matrix question
-
Show/Hide rows in matrix dropdown question
-
EnableIf
-
Complete Trigger
-
CopyValue Trigger
-
SetValue Trigger
-
Run Expression Trigger
-
-
Text Formatting
-
Survey Localization
-
Input Validation
-
SurveyJS Service
Simplify Cascade Conditions
Survey.StylesManager.applyTheme("defaultV2");
var json = {
"clearInvisibleValues": "onHidden",
"elements": [
{
"type": "radiogroup",
"name": "age18",
"title": "Are you 18 years old or older?",
"isRequired": true,
"choices": [ "Yes", "No" ],
"colCount": 0
},
{
"type": "radiogroup",
"name": "haveKids",
"title": "Do you have children?",
"visibleIf": "{age18} = 'Yes'",
"isRequired": true,
"choices": [ "Yes", "No" ],
"colCount": 0
},
{
"type": "dropdown",
"name": "kidCount",
"title": "How many children do you have?",
"visibleIf": "{haveKids} = 'Yes'",
"isRequired": true,
"choices": [ 1, 2, 3, 4, 5 ]
},
{
"type": "dropdown",
"name": "kid1Age",
"title": "How old is your first child?",
"visibleIf": "{kidCount} >= 1",
"isRequired": true,
"choicesMax": 18
},
{
"type": "dropdown",
"name": "kid2Age",
"title": "How old is your second child?",
"visibleIf": "{kidCount} >= 2",
"isRequired": true,
"startWithNewLine": false,
"choicesMax": 18
},
{
"type": "dropdown",
"name": "kid3Age",
"title": "How old is your third child?",
"visibleIf": "{kidCount} >= 3",
"isRequired": true,
"choicesMax": 18
},
{
"type": "dropdown",
"name": "kid4Age",
"title": "How old is your fourth child?",
"visibleIf": "{kidCount} >= 4",
"isRequired": true,
"startWithNewLine": false,
"choicesMax": 18
},
{
"type": "dropdown",
"name": "kid5Age",
"title": "How old is your fifth child?",
"visibleIf": "{kidCount} >= 5",
"isRequired": true,
"choicesMax": 18
}
],
"showQuestionNumbers": false
};
window.survey = new Survey.Model(json);
survey.onComplete.add(function(sender) {
document.querySelector('#surveyResult').textContent =
"Result JSON:\n" + JSON.stringify(sender.data, null, 3);
});
ReactDOM.render(
<SurveyReact.Survey model={survey} />, document.getElementById("surveyElement"));
<!DOCTYPE html>
<html lang="en">
<head>
<title>Simplify Cascade Conditions, Reactjs Survey Library Example</title>
<meta name="viewport" content="width=device-width" />
<script src="https://unpkg.com/react@17.0.1/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@17.0.1/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/@babel/standalone@7.2.5/babel.min.js"></script>
<script src="/DevBuilds/survey-core/survey.core.min.js"></script>
<script src="/DevBuilds/survey-core/survey.i18n.min.js"></script>
<script src="/DevBuilds/survey-react-ui/survey-react-ui.min.js"></script>
<link href="/DevBuilds/survey-core/defaultV2.min.css" type="text/css" rel="stylesheet" />
<link rel="stylesheet" href="./index.css">
</head>
<body style="margin: 0">
<div id="surveyElement" style="display:inline-block;width:100%;">
</div>
<div id="surveyResult"></div>
<script type="text/babel" src="./index.js"></script>
</body>
</html>
In a cascade condition, a question's visibility depends on the previous question, whose visibility, in turn, depends on a question before it, and so on. Cascade conditions may result in lengthy Boolean expressions because each expression must include conditions from previous cascade levels. In this example, questions form the following multi-level cascade:
"age18"
→ "haveKids"
→ "kidCount"
→ "kid1Age"
, "kid2Age"
,"kid3Age"
, ...
Each question beginning with "haveKids"
depends on the answer given to the previous question. Typical visibleIf
expressions in this case look as follows:
{
"elements": [{
"name": "age18",
"title": "Are you 18 years old or older?"
}, {
"name": "haveKids",
"title": "Do you have children?",
"visibleIf": "{age18} = 'Yes'"
}, {
"name": "kidCount",
"title": "How many children do you have?",
"visibleIf": "{age18} = 'Yes' and {haveKids} = 'Yes'"
}, {
"name": "kid1Age",
"title": "How old is your first child?",
"visibleIf": "{age18} = 'Yes' and {haveKids} = 'Yes' and {kidCount} >= 1"
}, {
"name": "kid2Age",
"title": "How old is your second child?",
"visibleIf": "{age18} = 'Yes' and {haveKids} = 'Yes' and {kidCount} >= 2"
}, {
"name": "kid3Age",
"title": "How old is your third child?",
"visibleIf": "{age18} = 'Yes' and {haveKids} = 'Yes' and {kidCount} >= 3"
}, {
"name": "kid4Age",
"title": "How old is your fourth child?",
"visibleIf": "{age18} = 'Yes' and {haveKids} = 'Yes' and {kidCount} >= 4"
}, {
"name": "kid5Age",
"title": "How old is your fifth child?",
"visibleIf": "{age18} = 'Yes' and {haveKids} = 'Yes' and {kidCount} >= 5"
}]
}
As you can see, cascade conditions accumulate, and the visibleIf
expressions for the lowest-level questions ("kid1Age"
, "kid2Age"
, etc.) include conditions of the higher-level questions.
To shorten expressions, you can set the clearInvisibleValues property to "onHidden"
. With this setting, expressions can take into account only the previous-level question, as shown in this example. Without clearInvisibleValues
set to "onHidden"
, such behavior is impossible because hidden questions retain their values. If you used shorter expressions in this case, a change in a higher-level question wouldn't propagate down the cascade, and lowest-level questions would remain visible even though questions upon which they depend were hidden.
Note that this simplification also slightly changes the behavior. If a question had a value before hiding, this value will not reappear after the question becomes visible again. If this is not the desired behavior, we recommend using lengthy expressions.
-
Templates
-
Simple Questions
-
Text Entry
-
Date-Time Entry
-
Numeric Entry
-
Color Input
-
Radio Button Group
-
Radio group with Custom Item Template
-
Drop-Down Menu
-
Dropdown: Load Data from Web Services
-
Dropdown with Custom Item Template
-
Dropdown with Lazy Loading
-
Multi-Select Dropdown
-
Checkboxes
-
Carry Forward Responses
-
Image Picker
-
Yes/No Question
-
Signature Pad
-
Multiple Text
-
Rating
-
Ranking
-
Comment
-
Image
-
Html
-
File
-
Expression
-
Expression (using async functions)
-
-
Matrix Table Questions
-
Panel & Dynamic Panel
-
Survey
-
Title and Logo
-
Questions in one line
-
Survey Options
-
Auto-Populate Form Fields
-
Share data between questions
-
Edit saved survey
-
Read-only/display mode
-
Show Preview before complete
-
Pop-Up Survey
-
Context actions in element titles
-
Modify title tags
-
Custom render of survey elements
-
Go next page automatically
-
File - custom preview
-
File - delayed upload
-
Lazy questions rendering
-
-
Quizzes and Scored Surveys
-
Integration with Third-Party Libraries
-
Appearance customization
-
Navigation
-
Conditions and Triggers
-
VisibleIf
-
Simplify Cascade Conditions
-
Complex questions in expressions
-
Use custom function in expressions
-
Create custom condition/expression properties
-
Conditions in dynamic questions
-
Show/Hide choices in radiogroup/checkbox/dropdown
-
Show/Hide individual items in radiogroup/checkbox/dropdown
-
Show/Hide columns/rows in matrix question
-
Show/Hide rows in matrix dropdown question
-
EnableIf
-
Complete Trigger
-
CopyValue Trigger
-
SetValue Trigger
-
Run Expression Trigger
-
-
Text Formatting
-
Survey Localization
-
Input Validation
-
SurveyJS Service