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.
Question
T17255
Visible to All Users
Hello,
Please share an example of a survey definition and specify which particular field value you wish to specify.
As per this image, i have a dynamic Panel & inside this there is a dynamic matrix. So, now how can i access the column 1 i.e., DPDM1 of this dynamic matrix & set value to it.
Hello,
Thank you for the update. Do you wish to set a matrix column/row value from code? Or, do you wish to set a matrix value when another survey field receives a value?
Please describe the usage scenario in greater detail so that I can consider an option.
Hi Jane,I wish to access matrix column/row from code & set a matrix column/row value from code. By code i mean in Survey Creator.
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.
If you wish to set a matrix column value from JavaScript code, you can call the
survey.setValue('matrixName', matrixValue)
function. ThematrixValue
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?
Hello Ramumkar,
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
, andquestion.value
properties. Consider the following demo: View Plunker.{ "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 ] } ] } ] } ] }
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.
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