diff --git a/src/functionsfactory.ts b/src/functionsfactory.ts index d706ece887..0e7405a4f1 100644 --- a/src/functionsfactory.ts +++ b/src/functionsfactory.ts @@ -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); diff --git a/tests/surveytests.ts b/tests/surveytests.ts index 5855f5b6e5..764fccc6ed 100644 --- a/tests/surveytests.ts +++ b/tests/surveytests.ts @@ -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: [ { @@ -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: [