Skip to content

Commit

Permalink
A matrix located within a dynamic panel doesn't have a default row va…
Browse files Browse the repository at this point in the history
…lue applied fix #8819 (#8833)
  • Loading branch information
andrewtelnov authored Sep 20, 2024
1 parent 314787d commit 47514f2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/survey-core/src/question_matrixdynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 6 additions & 2 deletions packages/survey-core/src/question_paneldynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -2219,7 +2223,7 @@ export class QuestionPanelDynamicModel extends Question
}
private isSetPanelItemData: HashTable<number> = {};
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]) {
Expand Down
36 changes: 36 additions & 0 deletions packages/survey-core/tests/question_paneldynamic_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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 = <QuestionPanelDynamicModel>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");
});

0 comments on commit 47514f2

Please sign in to comment.