Question T19903
Visible to All Users

Add a default value to a Dynamic Matrix

created 4 months ago (modified 4 months ago)

[Ticket cloned from T19896: decide if a column is readOnly based on a row index]

Ok thank you that works.
Regarding a matrixdynamic question how can I add a defaultValue to this question?
When I do something like this:

Code
"defaultValue":[ { "test": "test1" }, { "test": "test2" } ]

It dowsn't work

Answers approved by surveyjs Support

created 4 months ago

Hello Amit,
To set a default value for a dynamic matrix, set the defaultValue property to an array object which matches a matrix response.

JSON
{ "pages": [ { "name": "page1", "elements": [ { "type": "matrixdynamic", "name": "question1", "defaultValue": [ { "Column 1": 1, "Column 2": 1, "Column 3": 1 }, { "Column 1": 2, "Column 2": 2, "Column 3": 2 } ], "columns": [ { "name": "Column 1" }, { "name": "Column 2" }, { "name": "Column 3" } ], "choices": [ 1, 2, 3, 4, 5 ] } ] } ] }

For the record: you can also set the default value for an individual row using the defaultRowValuesetting.

Let me note that further technical Help Desk assistance is available only under a commercial developer license. If you're on a free plan, please feel free to submit a GitHub issue at https://github.com/surveyjs/survey-library/issues.

Thank you

    Comments (3)

      please have a look at this json:

      JSON
      const json = { "elements": [ { "type": "matrixdynamic", "name": "crp-ext-cut-and-sew-production-range-percentage-question", "title": "matrix dynamic", "rowCount": 7, "minRowCount": 7, "allowAddRows": true, "allowRemoveRows": true, "isRequired": true, "displayMode": "table", "choices": [1, 2, 3, 4, 5, 6, 7], "defaultValue": [ { "test": "some text", "total": 0 }, { "test": "some text", "total": 0 }, { "test": "some text", "total": 0 }, { "test": "some text", "total": 0 }, { "test": "some text", "total": 0 }, { "test": "some text", "total": 0 }, { "test": "some text", "total": 0 } ], "columns": [ { "cellType": "text", "name": "test", "enableIf": "{rowIndex} > 7" }, { "cellType": "text", "maskType": "currency", "defaultValue": "0", "max": "100", "min": "0", "maskSettings": { "suffix": "%" }, "name": "total", "title": "Total", "totalType": "sum", "totalFormat": "{0}%" } ] }, ], "showQuestionNumbers": "off" };

      The default value is not presented. What do I miss?

        Hello,
        Thank you for sharing a survey. I confirm that a default value appears correctly:
        Clipboard-File-1.png

        Do you programmatically set the survey.data property? Or, is your matrix located within another element (e.g., a dynamic panel)?

          Ah I see. Thank you