Skip to content

Commit

Permalink
Expressions agains matrix dynamic question doesn't work correctly if …
Browse files Browse the repository at this point in the history
…it is bind to a checkbox with valuePropertyName setup fix #8143
  • Loading branch information
andrewtelnov committed Apr 19, 2024
1 parent d085cb8 commit 004c2ae
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/question.ts
Expand Up @@ -1518,6 +1518,7 @@ export class Question extends SurveyElement<Question>
}
public get hasFilteredValue(): boolean { return false; }
public getFilteredValue(): any { return this.value; }
public getFilteredName(): any { return this.getValueName(); }
public get valueForSurvey(): any {
if (!!this.valueToDataCallback) {
return this.valueToDataCallback(this.value);
Expand Down
7 changes: 7 additions & 0 deletions src/question_checkbox.ts
Expand Up @@ -250,6 +250,13 @@ export class QuestionCheckboxModel extends QuestionCheckboxBase {
}
public get selectedItems(): Array<ItemValue> { return this.selectedChoices; }
public get hasFilteredValue(): boolean { return !!this.valuePropertyName; }
public getFilteredName(): any {
let res = super.getFilteredName();
if(this.hasFilteredValue) {
res += "#value";
}
return res;
}
public getFilteredValue(): any {
if(this.hasFilteredValue) return this.renderedValue;
return super.getFilteredValue();
Expand Down
2 changes: 1 addition & 1 deletion src/survey.ts
Expand Up @@ -3031,7 +3031,7 @@ export class SurveyModel extends SurveyElementCore
}
this.getAllQuestions().forEach(q => {
if (q.hasFilteredValue) {
values[q.getValueName()] = q.getFilteredValue();
values[q.getFilteredName()] = q.getFilteredValue();
}
});

Expand Down
2 changes: 1 addition & 1 deletion tests/question_baseselecttests.ts
Expand Up @@ -714,7 +714,7 @@ QUnit.test("checkbox vs valuePropertyName, use in expression", (assert) => {
{
type: "text",
name: "q2",
visibleIf: "{q1} allof ['apple', 'orange']"
visibleIf: "{q1#value} allof ['apple', 'orange']"
}
]
});
Expand Down
41 changes: 41 additions & 0 deletions tests/question_matrixdynamictests.ts
Expand Up @@ -9149,6 +9149,47 @@ QUnit.test("matrix dynamic expression & checkbox ValuePropertyName", function (a
assert.equal(matrix.visibleRows.length, 2, "matrix rows #2");
assert.deepEqual(matrix.value, [{ testItem: "Item 1", col1: "Item 1 - matrix" }, { testItem: "Item 2", col1: "Item 2 - matrix" }], "matrix value #2");
});
QUnit.test("matrix dynamic expression & checkbox valuePropertyName & sumInArray function", function (assert) {
const survey = new SurveyModel({
"elements": [
{
"type": "checkbox",
"name": "q1",
"choices": [
"Item 1",
"Item 2"
],
"valuePropertyName": "testItem"
},
{
"type": "matrixdynamic",
"name": "q2",
"valueName": "q1",
"columns": [
{
"name": "col1",
"cellType": "text",
"inputType": "text"
}
],
"rowCount": 0
},
{
"type": "expression",
"name": "q3",
"expression": "sumInArray({q1}, 'col1')"
}
]
});
const checkbox = <QuestionCheckboxModel>survey.getQuestionByName("q1");
const matrix = <QuestionMatrixDynamicModel>survey.getQuestionByName("q2");
const expression = survey.getQuestionByName("q3");
checkbox.renderedValue = ["Item 1", "Item 2"];
const rows = matrix.visibleRows;
rows[0].getQuestionByColumnName("col1").value = 5;
rows[1].getQuestionByColumnName("col1").value = 7;
assert.equal(expression.value, 12, "Calculate values correctly");
});

QUnit.test("Totals alingment", function (assert) {
var json = {
Expand Down

0 comments on commit 004c2ae

Please sign in to comment.