Cart Buy Licenses Contact Support Login/Register
v 1.8.6
v 1.8.6
Overview Survey Creator Examples Docs Source Download

Properties that depends on other properties

Hide/Show property or change its choices based on other properties


                        
    
        SurveyCreator.StylesManager.applyTheme("bootstrap");
    

//This property is depends on date format property of text question
//It is invisible if inputFormat property doesn't equal to "date*" values
Survey.Serializer.addProperty("text", {
name: "dateFormat",
dependsOn: ["inputType"],
visibleIf: function(obj) {
    return (
    obj.inputType == "date" ||
    obj.inputType == "datetime" ||
    obj.inputType == "datetime-local"
    );
},
category: "general",
visibleIndex: 7
});
Survey.Serializer.addProperty("question", {
    name: "targetEntity",
    choices: ["", "Account", "Developement"],
    category: "Entity",
    categoryIndex: 1
});
//This property depends from targetEntity property.
//If targetEntity property is empty then choices for targetField are empty as well.
Survey.Serializer.addProperty("question", {
    name: "targetField",
    dependsOn: "targetEntity",
    category: "Entity",
    choices: function(obj) {
        var choices = [];
        var entity = !!obj ? obj.targetEntity : null;
        //If targetEntity is empty then return the empty array
        if (!entity) return choices;
        //The correct way to set the nullable value
        choices.push({ value: null });
        choices.push(entity + " 1");
        choices.push(entity + " 2");
        choices.push(entity + " 3");
        return choices;
    }
});
Survey.Serializer.addProperty("survey", {
    name: "region",
    choices: ["Africa", "Americas", "Asia", "Europe", "Oceania"],
    category: "Geo Location",
    categoryIndex: 1
});
//This property country depends on the selected region.
//It allow to select all countries if region is empty or if it is not empty, only countries from this region
//It uses rest full service and choicesCallback function to tell Survey Creator property editor that choices are loaded from the web
Survey.Serializer.addProperty("survey", {
name: "country",
dependsOn: "region",
category: "Geo Location",
choices: function(obj, choicesCallback) {
    //We are going to use choicesCallback here
    var xhr = new XMLHttpRequest();
    //if region is empty then get all countries, otherwise get coutries by region.
    var url =
    !!obj && !!obj.region
        ? "https://restcountries.eu/rest/v2/region/" + obj.region
        : "https://restcountries.eu/rest/v2/all";
    xhr.open("GET", url);
    xhr.setRequestHeader(
    "Content-Type",
    "application/x-www-form-urlencoded"
    );
    //on load event set results into array of ItemValue and tell the Survey Creator that choices are loaded.
    xhr.onload = function() {
    if (xhr.status === 200) {
        var serverRes = JSON.parse(xhr.response);
        var res = [];
        //We will use ItemValue array here, since we want to have value different from display text
        res.push({ value: null });
        for (var i = 0; i < serverRes.length; i++) {
        var item = serverRes[i];
        res.push({ value: item.alpha2Code, text: item.name });
        }
        //return the result into Survey Creator property editor
        choicesCallback(res);
    }
    };
    xhr.send();
}
});
        
            var creatorOptions = { };
            var creator = new SurveyCreator.SurveyCreator("creatorElement", creatorOptions);
            creator.showToolbox = "right";
            creator.showPropertyGrid = "right";
            creator.rightContainerActiveItem("toolbox");
        

                    
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Hide/Show property or change its choices based on other properties, Survey Creator Example</title>

<meta name="viewport" content="width=device-width" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.0/knockout-min.js"></script>
<script src="https://surveyjs.azureedge.net/1.8.6/survey.ko.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.10/ace.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.10/ext-language_tools.js" type="text/javascript" charset="utf-8"></script>
    <!-- Uncomment to enable Select2
    <script src="https://unpkg.com/jquery"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
    -->
    <link href="https://surveyjs.azureedge.net/1.8.6/survey-creator.css" type="text/css" rel="stylesheet" />
    <script src="https://surveyjs.azureedge.net/1.8.6/survey-creator.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/bootstrap@3.3.7/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="./index.css">

</head>
<body>
    
    <div id="surveyContainer">
        <div id="creatorElement"></div>
    </div>

<script type="text/javascript" src="./index.js"></script>

</body>
</html>