Question T17255
Visible to All Users

Accessing a question from Nested Panel

created 10 months ago (modified 10 months ago)

In Survey JS is there a way how i can access a question from a nested panel & Set data to it? & other doubt i have is if there are duplicates with in nested panel how i can differentiate & Set value to particular question.

Show previous comments (4)

    Hi,
    Thank you for the update. If you wish to set a matrix column value in SurveyJS Creator, you can activate a default value dialog and set the target column value.

    Clipboard-File-1.png

    If you wish to set a matrix column value from JavaScript code, you can call the survey.setValue('matrixName', matrixValue) function. The matrixValue is an object which contains a matrix value. Note that the matrix value should be represented by a JSON object.

    Let me know if you have further questions.

      Hi Jane, so you mean to say even if the dynamic matrix is inside a dynamic panel, i can Set it's Value using this survey.setValue('matrixName', matrixValue) function (or) if i want to Set value to this dynamic matrix within dynamic panel or panel should i need to use another way?

      I have one more thing to Say i.e. for all the list of questions within a questionnaire, i will have paths like this as in the image. what is this path means for index 8 in the image you can see name & text 'DM[0].Column 1' or 'DM[0].DM1' where DM[0] means Dynamic matrix & inside this we have column that can be DM1 or Column 1.

      So, is there a way by using code can i get this DM1 column of Dynamic Matrix within Dynamic Panel & Set value to it?

      Clipboard-File-1.png

        Hello Ramumkar,

        Hi Jane, so you mean to say even if the dynamic matrix is inside a dynamic panel, i can Set it's Value using this survey.setValue('matrixName', matrixValue) function (or) if i want to Set value to this dynamic matrix within dynamic panel or panel should i need to use another way?

        You can actually set a value of a dynamic matrix which resides in a dynamic panel. You can do so using the survey.data, survey.setValue, and question.value properties. Consider the following demo: View Plunker.

        JSON
        { "pages": [ { "name": "page1", "elements": [ { "type": "paneldynamic", "name": "question1", "templateElements": [ { "type": "matrixdynamic", "name": "question2", "columns": [ { "name": "Column 1" }, { "name": "Column 2" }, { "name": "Column 3" } ], "choices": [ 1, 2, 3, 4, 5 ] } ] } ] } ] }
        JavaScript
        const panelValue = [ { "question2": [ { "Column 1": 1, "Column 2": 2, "Column 3": 3 }, { "Column 1": 1, "Column 2": 2, "Column 3": 3 } ] } ]; survey.setValue('question1', panelValue)

        It is important to note that each survey question has a specific data format. For instance, a Dynamic Panel's value is an array of objects. Each object corresponds to a specific panel. For more information on a question value structure, please visit our documentation.

        I have one more thing to Say i.e. for all the list of questions within a questionnaire, i will have paths like this as in the image. what is this path means for index 8 in the image you can see name & text 'DM[0].Column 1' or 'DM[0].DM1' where DM[0] means Dynamic matrix & inside this we have column that can be DM1 or Column 1.
        So, is there a way by using code can i get this DM1 column of Dynamic Matrix within Dynamic Panel & Set value to it?

        To set a matrix row value, you can use the QuestionMatrixDynamicModel.setRowValue function. However, the thing is that it may require to implement complex logic to parse a full path to determine which particular column and row value needs to be set.

        Please let me elaborate on your overall goal.
        In SurveyJS, we have the mechanism which allows you to share values between different survey entities. The valueName property allows you to copy values from one question to another. For instance, you may want to copy single-input values. Or, you may wish to copy matrix array-like values. An example is available at Merge Question Values.

        However, it seems that you somehow want to set values of dynamic panels and matrices which is located within. It may be an untrivial task provided the complexity of such a form structure.
        I would be grateful if you could clarify why you need to build such a complex logic and automatically set survey values from code. Would you share an example of a form/survey which you build and clarify how exactly/based on which conditions you wish to populate a matrix.

        Thanks