From e377ae59604ac79b616b95fee0637002faf699e2 Mon Sep 17 00:00:00 2001 From: Andrew Telnov Date: Mon, 14 Oct 2024 14:37:39 +0300 Subject: [PATCH] Support "rowName" together vs "rowValue" for matrix dropdown row processing --- .../src/question_matrixdropdown.ts | 20 +++++----- .../src/question_matrixdropdownbase.ts | 4 +- .../tests/question_matrixdynamictests.ts | 40 +++++++++++-------- 3 files changed, 37 insertions(+), 27 deletions(-) diff --git a/packages/survey-core/src/question_matrixdropdown.ts b/packages/survey-core/src/question_matrixdropdown.ts index ebcedef40a..ae130ffbb8 100644 --- a/packages/survey-core/src/question_matrixdropdown.ts +++ b/packages/survey-core/src/question_matrixdropdown.ts @@ -99,19 +99,19 @@ export class QuestionMatrixDropdownModel extends QuestionMatrixDropdownModelBase var res = {}; if (!rows) return res; for (var i = 0; i < rows.length; i++) { - var rowValue = rows[i].rowName; - var val = value[rowValue]; + var rowName = rows[i].rowName; + var val = value[rowName]; if (!val) continue; if (keysAsText) { var displayRowValue = ItemValue.getTextOrHtmlByValue( this.rows, - rowValue + rowName ); if (!!displayRowValue) { - rowValue = displayRowValue; + rowName = displayRowValue; } } - (res)[rowValue] = this.getRowDisplayValue(keysAsText, rows[i], val); + (res)[rowName] = this.getRowDisplayValue(keysAsText, rows[i], val); } return res; } @@ -162,10 +162,10 @@ export class QuestionMatrixDropdownModel extends QuestionMatrixDropdownModelBase } super.clearGeneratedRows(); } - private getRowValueForCreation(val: any, rowValue: any): any { - const res = val[rowValue]; + private getRowValueForCreation(val: any, rowName: any): any { + const res = val[rowName]; if(!res) return res; - const names = this.defaultValuesInRows[rowValue]; + const names = this.defaultValuesInRows[rowName]; if(!Array.isArray(names) || names.length === 0) return res; names.forEach(name => { delete res[name]; @@ -207,8 +207,8 @@ export class QuestionMatrixDropdownModel extends QuestionMatrixDropdownModelBase if(!val) val = {}; for(var i = 0; i < this.rows.length; i ++) { const row = this.rows[i]; - const rowValue = val[row.value]; - this.updateProgressInfoByRow(res, !!rowValue ? rowValue : {}); + const rowName = val[row.value]; + this.updateProgressInfoByRow(res, !!rowName ? rowName : {}); } } } diff --git a/packages/survey-core/src/question_matrixdropdownbase.ts b/packages/survey-core/src/question_matrixdropdownbase.ts index 9b67e0e12b..fa0f356d7b 100644 --- a/packages/survey-core/src/question_matrixdropdownbase.ts +++ b/packages/survey-core/src/question_matrixdropdownbase.ts @@ -218,7 +218,7 @@ class MatrixDropdownRowTextProcessor extends QuestionTextProcessor { textValue.value = this.row.rowIndex; return true; } - if (textValue.name == MatrixDropdownRowModelBase.RowValueVariableName) { + if ([MatrixDropdownRowModelBase.RowValueVariableName, MatrixDropdownRowModelBase.RowNameVariableName].indexOf(textValue.name) > -1) { textValue.isExists = true; textValue.value = this.row.rowName; return true; @@ -232,6 +232,7 @@ export class MatrixDropdownRowModelBase implements ISurveyData, ISurveyImpl, ILo public static OwnerVariableName = "self"; public static IndexVariableName = "rowIndex"; public static RowValueVariableName = "rowValue"; + public static RowNameVariableName = "rowName"; private static idCounter: number = 1; private static getId(): string { @@ -412,6 +413,7 @@ export class MatrixDropdownRowModelBase implements ISurveyData, ISurveyImpl, ILo private applyRowVariablesToValues(res: any, rowIndex: number): void { res[MatrixDropdownRowModelBase.IndexVariableName] = rowIndex; res[MatrixDropdownRowModelBase.RowValueVariableName] = this.rowName; + res[MatrixDropdownRowModelBase.RowNameVariableName] = this.rowName; } public runCondition(values: HashTable, properties: HashTable, rowsVisibleIf?: string): void { if(!this.data) return; diff --git a/packages/survey-core/tests/question_matrixdynamictests.ts b/packages/survey-core/tests/question_matrixdynamictests.ts index 147f0edd00..e34461bbba 100644 --- a/packages/survey-core/tests/question_matrixdynamictests.ts +++ b/packages/survey-core/tests/question_matrixdynamictests.ts @@ -2656,7 +2656,7 @@ QUnit.test("rowValue variable, in text processing", function (assert) { ); }); QUnit.test("rowValue variable in expression", function (assert) { - var json = { + var survey = new SurveyModel({ elements: [ { type: "matrixdropdown", @@ -2667,22 +2667,30 @@ QUnit.test("rowValue variable in expression", function (assert) { rows: ["Row 1", "Row 2"], }, ], - }; - var survey = new SurveyModel(json); - var question = survey.getQuestionByName("q1"); - var rows = question.visibleRows; - assert.equal( - rows[0].cells[0].question.value, - "Row 1", - "The first row has rowValue 'Row 1'" - ); - assert.equal( - rows[1].cells[0].question.value, - "Row 2", - "The first row has rowValue 'Row 2'" - ); + }); + const question = survey.getQuestionByName("q1"); + const rows = question.visibleRows; + assert.equal(rows[0].cells[0].question.value, "Row 1", "The first row has rowValue 'Row 1'"); + assert.equal(rows[1].cells[0].question.value, "Row 2", "The first row has rowValue 'Row 2'"); +}); +QUnit.test("rowName variable in expression", function (assert) { + var survey = new SurveyModel({ + elements: [ + { + type: "matrixdropdown", + name: "q1", + columns: [ + { name: "column1", cellType: "expression", expression: "{rowName}" }, + ], + rows: ["Row 1", "Row 2"], + }, + ], + }); + const question = survey.getQuestionByName("q1"); + const rows = question.visibleRows; + assert.equal(rows[0].cells[0].question.value, "Row 1", "The first row has rowName 'Row 1'"); + assert.equal(rows[1].cells[0].question.value, "Row 2", "The first row has rowName 'Row 2'"); }); - QUnit.test("row property in custom function", function (assert) { var rowCustomFunc = function (params: any) { var val = this.row.getValue(params[0]);