Question T6949
Visible to All Users

Add an id field to choices array

created 4 years ago

Hello,

I have a choices array loaded for radiogroup/dropdown, etc questions . Based on the value selected in these questions, the next subquestion is loaded (visibleIf condition). The 'visibleIf' condition here compares the value of the question:
"visibleIf" : "{question1} = 'Yes'"

Instead of comparing the values, I wish to use the id instead. If I add the 'id' value to the choices array, can you tell me how I can use the same for 'visibleIf' / 'enableIf' conditions?

Answers approved by surveyjs Support

created 4 years ago

Hello Komal,
We are checking a value and not id by default. If you need to check an id, then you will need to use a custom function here.

JavaScript
var getItemId = function (params) { //this.row property available in cells of dropdown and dynamic matrices questions, otherwise get question from survey var question = !!this.row ? this .row .getQuestionByColumnName(params[0]) : this.survey.getQuestionByName(params[0]); //if we can't find a question then return undefined if (!question) return undefined; //get the selected item/choice var selItem = question.selectedItem; //return undefined if a user did not select the item yet. return !!selItem ? selItem.id : undefined; }; //Register the custom function Survey .FunctionFactory .Instance .register("getItemId", getItemId);

Now you can use it:
"visibleIf" : "getItemId('question1') = 'YourItemId'".
Please note, that you pass "question1" as a string parameter and not {question1}, it will pass the "question1" value.

Thank you,
Andrew

    Comments (2)

      Thanks Andrew.

      I applied the above logic to this plunkr - but seems to fail. Can you please have a look?
      https://plnkr.co/edit/gDVHP7PPvJ9I0XJV
      The "selItem.id" always returns undefined. Do I need to register the 'id' property as well?

      I have added 'id' field to the choices array, and invoking the 'getItemId' function in visibleIf condition to check the id value.

        You should register functions before loading JSON into Survey and you do not add "id" property into itemvalue class, so the Serializer ignores it on loading. I have modified the custom function slightly as well.
        Here is the updated example.

        Thank you,
        Andrew
        SurveyJS Team