Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add value as a second parameter into 'displayValue' function fix #8576 #8577

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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