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

Conditions in dynamic questions


                        
        
            Survey.StylesManager.applyTheme("defaultV2");
        



                
                    var json =  {
    questions:[
      {
        type: "matrixdynamic",
        name: "frameworksRateMatrix",
        "minRowCount": 4,
        "maxRowCount": 4,
        "valueName": "frameworksRate",
        title: "Please tells us about JavaScript frameworks you are using (Matrix Dynamic)",
        columns: [
          {
            "name": "framework",
            "title": "Framework",
            "cellType": "text",
            "readOnly": true
          },
          {
            name: "using",
            title: "Do you use it?",
            "isRequired": true,
            choices: [
              "Yes",
              "No"
            ],
            cellType: "radiogroup"
          },
          {
            name: "experience",
            title: "How long do you use it?",
            cellType: "dropdown",
            "visibleIf": "{row.using} = 'Yes'",
            "isRequired": true,
            choices: [
              {
                value: 5,
                text: "3-5 years"
              },
              {
                value: 2,
                text: "1-2 years"
              },
              {
                value: 1,
                text: "less then a year"
              }
            ]
          }
        ]
      },
        {
            type: "paneldynamic",
          name: "frameworksRatePanel",
          "minPanelCount":  4, "maxPanelCount":  4,
          "valueName":  "frameworksRate",
            title: "Please tells us about JavaScript frameworks you are using (Panel Dynamic)",
          templateElements: [
            {
              "name": "framework",
              "title": "Framework",
              "type": "text",
              "readOnly":  true
            },
            {
              name: "using",
              title: "Do you use it?",
              "type": "radiogroup",
              "colCount":  0,
              "startWithNewLine": false,
              "isRequired": true,
              choices: [
                "Yes",
                "No"
              ],
              cellType: "radiogroup"
            },
            {
              name: "experience",
              title: "How long do you use it?",
              "type": "dropdown",
              "visibleIf": "{panel.using} = 'Yes'",
              "isRequired": true,
              "startWithNewLine": false,
              choices: [
                {
                  value: 5,
                  text: "3-5 years"
                },
                {
                  value: 2,
                  text: "1-2 years"
                },
                {
                  value: 1,
                  text: "less then a year"
                }
              ]
            }
          ]
        }

    ]
};

                    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.data = {'frameworksRate': [{'framework':'angular v1'},{'framework':'angular v2+'},{'framework':'react'}, {'framework':'vue'}]};
            
                survey.render("surveyElement");
            

                    
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Conditions in dynamic questions, 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 this example we are using two questions bound to the same data. Their valueName property equals to "frameworksRate".
"How long do you use it?" cell in matrix and question in panel are shown based on "Do you use it?" question in corresponded row or panel.
To access the value of the current row/panel you have to use prefix row. or panel..
Example for a matrix column:

visibleIf: "{row.using} = 'Yes'"
Example for a question in dynamic panel
visibleIf: "{panel.using} = 'Yes'"