Skip to content

Commit

Permalink
Add value as a second parameter into 'displayValue' function fix #8576 (
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov authored Jul 18, 2024
1 parent b3fde22 commit 43ea8ed
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/functionsfactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,9 @@ function getQuestionValueByContext(context: any, name: string): any {
}
function displayValue(params: any[]): any {
const q = getQuestionValueByContext(this, params[0]);
return q ? q.displayValue : "";
if(!q) return "";
if(params.length > 1 && !!params[1]) return q.getDisplayValue(true, params[1]);
return q.displayValue;
}
FunctionFactory.Instance.register("displayValue", displayValue);

Expand Down
39 changes: 38 additions & 1 deletion tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18778,7 +18778,7 @@ QUnit.test("Use variables as default values in expression", function (assert) {
const q1 = survey.getQuestionByName("q1");
assert.equal(q1.value, 2, "Get data from survey");
});
QUnit.test("Test getDisplayValue() function", function (assert) {
QUnit.test("Test displayValue() function", function (assert) {
const survey = new SurveyModel({
elements: [
{
Expand Down Expand Up @@ -18846,6 +18846,43 @@ QUnit.test("Test getDisplayValue() function", function (assert) {
q3: [{ col1: [1, 3], col1_exp: "Item 1, Item 3", q3_q1: [1, 2, 3], q3_q1_exp: "Item 1, Item 2, Item 3" }]
}, "displayValue works correctly");
});
QUnit.test("Test displayValue() function with value parameter", function (assert) {
const survey = new SurveyModel({
elements: [
{
type: "checkbox",
name: "q1",
choices: [{ value: 1, text: "Item check 1" }, { value: 2, text: "Item check 2" }, { value: 3, text: "Item check 3" }]
},
{
type: "dropdown",
name: "q2",
choices: [{ value: 1, text: "Item 1" }, { value: 2, text: "Item 2" }, { value: 3, text: "Item 3" }]
},
{
type: "matrixdynamic",
name: "matrix",
columns: [
{ cellType: "text", name: "col1" },
{ cellType: "expression", name: "col2", expression: "displayValue('q1', {row.col1})" },
{ cellType: "expression", name: "col3", expression: "displayValue('q2', {row.col1})" }
]
}
]
});
const matrix = survey.getQuestionByName("matrix");
matrix.rowCount = 3;
const rows = matrix.visibleRows;
rows[0].cells[0].value = 2;
rows[1].cells[0].value = 3;
rows[2].cells[0].value = 1;
assert.equal(rows[0].cells[1].value, "Item check 2", "cells[0,1].value");
assert.equal(rows[0].cells[2].value, "Item 2", "cells[0,2].value");
assert.equal(rows[1].cells[1].value, "Item check 3", "cells[1,1].value");
assert.equal(rows[1].cells[2].value, "Item 3", "cells[1,2].value");
assert.equal(rows[2].cells[1].value, "Item check 1", "cells[2,1].value");
assert.equal(rows[2].cells[2].value, "Item 1", "cells[2,2].value");
});
QUnit.test("Test propertyValue() function", function (assert) {
const survey = new SurveyModel({
elements: [
Expand Down

0 comments on commit 43ea8ed

Please sign in to comment.