Skip to content

Commit

Permalink
Support "rowName" together vs "rowValue" for matrix dropdown row proc…
Browse files Browse the repository at this point in the history
…essing
  • Loading branch information
andrewtelnov committed Oct 14, 2024
1 parent 81dc27c commit e377ae5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 27 deletions.
20 changes: 10 additions & 10 deletions packages/survey-core/src/question_matrixdropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
(<any>res)[rowValue] = this.getRowDisplayValue(keysAsText, rows[i], val);
(<any>res)[rowName] = this.getRowDisplayValue(keysAsText, rows[i], val);
}
return res;
}
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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 : {});
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion packages/survey-core/src/question_matrixdropdownbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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<any>, properties: HashTable<any>, rowsVisibleIf?: string): void {
if(!this.data) return;
Expand Down
40 changes: 24 additions & 16 deletions packages/survey-core/tests/question_matrixdynamictests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -2667,22 +2667,30 @@ QUnit.test("rowValue variable in expression", function (assert) {
rows: ["Row 1", "Row 2"],
},
],
};
var survey = new SurveyModel(json);
var question = <QuestionMatrixDynamicModel>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 = <QuestionMatrixDynamicModel>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 = <QuestionMatrixDynamicModel>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]);
Expand Down

0 comments on commit e377ae5

Please sign in to comment.