diff --git a/src/question_matrixdropdownbase.ts b/src/question_matrixdropdownbase.ts index 92f421099c..bd81690fab 100644 --- a/src/question_matrixdropdownbase.ts +++ b/src/question_matrixdropdownbase.ts @@ -202,8 +202,12 @@ export class QuestionMatrixDropdownModelBase extends Question implements IMatrix } return result; } + protected onBeforeValueChanged(val: any) { + } protected onValueChanged() { - if (this.isRowChanging || !(this.generatedVisibleRows) || this.generatedVisibleRows.length == 0) return; + if (this.isRowChanging) return; + this.onBeforeValueChanged(this.value); + if(!(this.generatedVisibleRows) || this.generatedVisibleRows.length == 0) return; this.isRowChanging = true; var val = this.createNewValue(this.value); for (var i = 0; i < this.generatedVisibleRows.length; i++) { diff --git a/src/question_matrixdynamic.ts b/src/question_matrixdynamic.ts index e40c48a2be..9857046fbb 100644 --- a/src/question_matrixdynamic.ts +++ b/src/question_matrixdynamic.ts @@ -99,6 +99,14 @@ export class QuestionMatrixDynamicModel extends QuestionMatrixDropdownModelBase protected createMatrixRow(value: any): MatrixDynamicRowModel { return new MatrixDynamicRowModel(this.rowCounter ++, this, value); } + protected onBeforeValueChanged(val: any) { + var newRowCount = val && Array.isArray(val) ? val.length : 0; + if (newRowCount <= this.rowCount) return; + this.rowCountValue = newRowCount; + if (this.generatedVisibleRows) { + this.generatedVisibleRows = this.visibleRows; + } + } protected createNewValue(curValue: any): any { var result = curValue; if (!result) result = []; diff --git a/tests/surveyquestiontests.ts b/tests/surveyquestiontests.ts index 7909db4b4c..c71daef57e 100644 --- a/tests/surveyquestiontests.ts +++ b/tests/surveyquestiontests.ts @@ -459,6 +459,19 @@ QUnit.test("Matrixdynamic hasOther column", function (assert) { rows[0].cells[0].question.value = "other"; assert.equal(question.hasErrors(), true, "Should set other value"); }); +QUnit.test("Matrixdynamic adjust rowCount on setting the value", function (assert) { + var question = new QuestionMatrixDynamicModel("matrixDymanic"); + question.rowCount = 0; + question.columns.push(new MatrixDropdownColumn("column1")); + question.columns.push(new MatrixDropdownColumn("column2")); + question.value = [{}, { 'column1': 2 }, {}]; + assert.equal(question.rowCount, 3, "It should be 3 rowCount"); + var rows = question.visibleRows; + question.value = [{}, { 'column1': 2 }, {}, {}]; + assert.equal(question.rowCount, 4, "It should be 4 rowCount"); + question.value = [{ 'column1': 2 }]; + assert.equal(question.rowCount, 4, "Keep row count equals 4"); +}); QUnit.test("Matrixdropdown different cell types", function (assert) { var question = new QuestionMatrixDropdownModel("matrixDropdown");