Cart Licenses Contact Support Online Creator Log in/Register
Try
Buy
v 1.9.51

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
    }
  ]
};

                    window.survey = new SurveyKnockout.Survey(json);
                
        
            survey.onComplete.add(function(sender) {
            document.querySelector('#surveyResult').textContent =
            "Result JSON:\n" + JSON.stringify(sender.data, null, 3);
            });
        
            
                survey.render("surveyElement");
            

                    
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Simplify Cascade Conditions, Knockoutjs Survey Library Example</title>
    
<meta name="viewport" content="width=device-width" />
    <script src="https://unpkg.com/knockout@3.5.1/build/output/knockout-latest.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-knockout-ui/survey-knockout-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/javascript" 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.