From 47514f2095c32d15ee5adfb0e1d56e4e782e5e24 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 20 Sep 2024 10:45:16 +0300 Subject: [PATCH] A matrix located within a dynamic panel doesn't have a default row value applied fix #8819 (#8833) --- .../survey-core/src/question_matrixdynamic.ts | 6 ++++ .../survey-core/src/question_paneldynamic.ts | 8 +++-- .../tests/question_paneldynamic_tests.ts | 36 +++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/packages/survey-core/src/question_matrixdynamic.ts b/packages/survey-core/src/question_matrixdynamic.ts index e51849b7eb..f89e2ed7db 100644 --- a/packages/survey-core/src/question_matrixdynamic.ts +++ b/packages/survey-core/src/question_matrixdynamic.ts @@ -823,6 +823,12 @@ export class QuestionMatrixDynamicModel extends QuestionMatrixDropdownModelBase } updateValueFromSurvey(newValue: any, clearData: boolean = false): void { this.setRowCountValueFromData = true; + if(this.minRowCount > 0 && Helpers.isValueEmpty(newValue) && !Helpers.isValueEmpty(this.defaultRowValue)) { + newValue = []; + for(let i = 0; i < this.minRowCount; i ++) { + newValue.push(Helpers.createCopy(this.defaultRowValue)); + } + } super.updateValueFromSurvey(newValue, clearData); this.setRowCountValueFromData = false; } diff --git a/packages/survey-core/src/question_paneldynamic.ts b/packages/survey-core/src/question_paneldynamic.ts index cbdbab7bb9..a278785eb5 100644 --- a/packages/survey-core/src/question_paneldynamic.ts +++ b/packages/survey-core/src/question_paneldynamic.ts @@ -1018,7 +1018,11 @@ export class QuestionPanelDynamicModel extends Question var value = this.value; if (!value || !Array.isArray(value)) value = []; if (value.length == this.panelCount) return; - for (var i = value.length; i < this.panelCount; i++) value.push({}); + for (var i = value.length; i < this.panelCount; i++) { + const panelValue = this.panels[i].getValue(); + const val = !Helpers.isValueEmpty(panelValue) ? panelValue : {}; + value.push(val); + } if (value.length > this.panelCount) { value.splice(this.panelCount, value.length - this.panelCount); } @@ -2219,7 +2223,7 @@ export class QuestionPanelDynamicModel extends Question } private isSetPanelItemData: HashTable = {}; private static maxCheckCount = 3; - setPanelItemData(item: ISurveyData, name: string, val: any) { + setPanelItemData(item: ISurveyData, name: string, val: any): void { if (this.isSetPanelItemData[name] > this.maxCheckCount) return; if (!this.isSetPanelItemData[name]) { diff --git a/packages/survey-core/tests/question_paneldynamic_tests.ts b/packages/survey-core/tests/question_paneldynamic_tests.ts index 7d1748b27f..b5c58d3554 100644 --- a/packages/survey-core/tests/question_paneldynamic_tests.ts +++ b/packages/survey-core/tests/question_paneldynamic_tests.ts @@ -4574,6 +4574,7 @@ QUnit.test("templateTitle test + survey.onValueChanged", function(assert) { QUnit.test("defaultValue & survey.onValueChanged on adding new panel", function(assert) { const survey = new SurveyModel({ questions: [ + { type: "text", name: "val1" }, { name: "panel", type: "paneldynamic", @@ -7437,3 +7438,38 @@ QUnit.test("getFirstQuestionToFocus, Bug#8764", function (assert) { panel.validate(true); assert.equal(panel.getFirstQuestionToFocus(true).name, "panel", "#5"); }); +QUnit.test("defaultRowValue in dynamic panel, Bug#8819", function (assert) { + const survey = new SurveyModel({ + "elements": [ + { + "type": "paneldynamic", + "name": "panel1", + "templateElements": [ + { + "type": "matrixdynamic", + "name": "matrix1", + "columns": [ + { + "name": "col1", + "cellType": "text" + } + ], + "rowCount": 1, + "minRowCount": 1, + "defaultRowValue": { + "col1": "abc" + } + } + ], + "minPanelCount": 1 + } + ] + }); + const panel = survey.getQuestionByName("panel1"); + assert.deepEqual(panel.value, [{ matrix1: [{ "col1": "abc" }] }], "#1"); + panel.addPanel(); + assert.deepEqual(panel.value, [{ matrix1: [{ "col1": "abc" }] }, { matrix1: [{ "col1": "abc" }] }], "#2"); + panel.panels[1].questions[0].addRow(); + assert.deepEqual(panel.value, [{ matrix1: [{ "col1": "abc" }] }, { matrix1: [{ "col1": "abc" }, { "col1": "abc" }] }], "#3"); +}); +