Skip to content

Commit

Permalink
Setting 0 as default value for matrix dynamic rowCount property doesn…
Browse files Browse the repository at this point in the history
…'t work as expected fix #8258 (#8259)
  • Loading branch information
andrewtelnov committed May 13, 2024
1 parent 5374892 commit 0a8407c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/question_matrixdynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ export class QuestionMatrixDynamicModel extends QuestionMatrixDropdownModelBase
sender: QuestionMatrixDynamicModel
) => any;
private rowCounter = 0;
private initialRowCount: number = 2;
private initialRowCount: number;
private setRowCountValueFromData: boolean = false;

constructor(name: string) {
super(name);
this.initialRowCount = this.getDefaultPropertyValue("rowCount");
this.createLocalizableString("confirmDeleteText", this, false, "confirmDelete");
var locAddRowText = this.createLocalizableString("addRowText", this);
locAddRowText.onGetTextCallback = (text: string): string => {
Expand Down Expand Up @@ -827,6 +828,11 @@ export class QuestionMatrixDynamicModel extends QuestionMatrixDropdownModelBase
this.setPropertyValueDirectly("rowCount", val.length);
return true;
}
updateValueFromSurvey(newValue: any, clearData: boolean = false): void {
this.setRowCountValueFromData = true;
super.updateValueFromSurvey(newValue, clearData);
this.setRowCountValueFromData = false;
}
protected onBeforeValueChanged(val: any) {
if (!val || !Array.isArray(val)) return;
var newRowCount = val.length;
Expand Down
52 changes: 51 additions & 1 deletion tests/question_matrixdynamictests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9384,4 +9384,54 @@ QUnit.test("table: check animation options", function (assert) {
assert.equal(questionHtmlElement.style.getPropertyValue("--animation-height"), "40px");

tableHtmlElement.remove();
});
});
QUnit.test("set data from the survey", function (assert) {
const survey = new SurveyModel({
elements: [
{
type: "matrixdynamic",
name: "matrix",
columns: [{ name: "col1" }]
}
]
});
survey.data = { matrix: [{ col1: 1 }] };
const matrix = <QuestionMatrixDynamicModel>survey.getQuestionByName("matrix");
assert.equal(matrix.visibleRows.length, 1, "There one row");
});
QUnit.test("set data from the survey and default row count is 0", function (assert) {
const prop = Serializer.findProperty("matrixdynamic", "rowCount");
const prevValue = prop.defaultValue;
assert.equal(prevValue, 2, "The default rowCount value is 2");
prop.defaultValue = 0;
const survey = new SurveyModel({
elements: [
{
type: "matrixdynamic",
name: "matrix",
columns: [{ name: "col1" }]
}
]
});
survey.data = { matrix: [{ col1: 1 }] };
const matrix = <QuestionMatrixDynamicModel>survey.getQuestionByName("matrix");
assert.equal(matrix.visibleRows.length, 1, "There one row");
Serializer.findProperty("matrixdynamic", "rowCount").defaultValue = 2;
prop.defaultValue = prevValue;
});
QUnit.test("set data from the defaultValue and ignore rowCount", function (assert) {
const survey = new SurveyModel({
elements: [
{
type: "matrixdynamic",
name: "matrix",
rowCount: 3,
defaultValue: [{ col1: 1 }],
columns: [{ name: "col1" }]
}
]
});
survey.data = { matrix: [{ col1: 1 }] };
const matrix = <QuestionMatrixDynamicModel>survey.getQuestionByName("matrix");
assert.equal(matrix.visibleRows.length, 1, "There one row");
});

0 comments on commit 0a8407c

Please sign in to comment.