Skip to content

Commit

Permalink
Features/6874 displayvalue function (#6953)
Browse files Browse the repository at this point in the history
* Add displayValue function #6874

* Add proeprtyValue function #6874
  • Loading branch information
andrewtelnov committed Sep 14, 2023
1 parent 4b14d15 commit 52ff9f8
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/functionsfactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,28 @@ function weekday(params: any[]): any {
return date.getDay();
}
FunctionFactory.Instance.register("weekday", weekday);

function getQuestionValueByContext(context: any, name: string): any {
if(!context || !name) return undefined;
const keys = ["row", "panel", "survey"];
for(let i = 0; i < keys.length; i ++) {
const ctx = context[keys[i]];
if(ctx && ctx.getQuestionByName) {
const res = ctx.getQuestionByName(name);
if(res) return res;
}
}
return null;
}
function displayValue(params: any[]): any {
const q = getQuestionValueByContext(this, params[0]);
return q ? q.displayValue : "";
}
FunctionFactory.Instance.register("displayValue", displayValue);

function propertyValue(params: any[]): any {
if(params.length !== 2 || !params[0] || !params[1]) return undefined;
const q = getQuestionValueByContext(this, params[0]);
return q ? q[params[1]] : undefined;
}
FunctionFactory.Instance.register("propertyValue", propertyValue);
118 changes: 118 additions & 0 deletions tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17754,3 +17754,121 @@ 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) {
const survey = new SurveyModel({
elements: [
{
type: "checkbox",
name: "q1",
choices: [{ value: 1, text: "Item 1" }, { value: 2, text: "Item 2" }, { value: 3, text: "Item 3" }]
},
{
type: "expression",
name: "q1_exp",
expression: "displayValue('q1')"
},
{
type: "paneldynamic",
name: "q2",
templateElements: [{
type: "checkbox",
name: "q2_q1",
choices: [{ value: 1, text: "Item 1" }, { value: 2, text: "Item 2" }, { value: 3, text: "Item 3" }]
},
{
type: "expression",
name: "q2_q1_exp",
expression: "displayValue('q2_q1')"
}]
},
{
type: "matrixdynamic",
name: "q3",
rowCount: 0,
columns: [{
cellType: "checkbox",
name: "col1",
choices: [{ value: 1, text: "Item 1" }, { value: 2, text: "Item 2" }, { value: 3, text: "Item 3" }]
},
{
cellType: "expression",
name: "col1_exp",
expression: "displayValue('col1')"
}],
detailPanelMode: "underRow",
detailElements: [{
type: "checkbox",
name: "q3_q1",
choices: [{ value: 1, text: "Item 1" }, { value: 2, text: "Item 2" }, { value: 3, text: "Item 3" }]
},
{
type: "expression",
name: "q3_q1_exp",
expression: "displayValue('q3_q1')"
}],
}
]
});
survey.data = { q1: [1, 2], q2: [{ q2_q1: [2, 3] }] };
const matrix = survey.getQuestionByName("q3");
matrix.addRow();
const row = matrix.visibleRows[0];
row.showDetailPanel();
row.getQuestionByName("col1").value = [1, 3];
row.getQuestionByName("q3_q1").value = [1, 2, 3];
assert.deepEqual(survey.data, {
q1: [1, 2], q1_exp: "Item 1, Item 2",
q2: [{ q2_q1: [2, 3], q2_q1_exp: "Item 2, Item 3" }],
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 propertyValue() function", function (assert) {
const survey = new SurveyModel({
elements: [
{
type: "text",
name: "q1",
title: "Q1"
},
{
type: "expression",
name: "q1_exp",
expression: "propertyValue('q1', 'title')"
},
{
type: "paneldynamic",
name: "q2",
templateElements: [{
type: "text",
name: "q2_q1",
title: "Q2_Q1"
},
{
type: "expression",
name: "q2_q1_exp",
expression: "propertyValue('q2_q1', 'title')"
}]
},
{
type: "matrixdynamic",
name: "q3",
rowCount: 0,
columns: [{
cellType: "text",
title: "Column 1",
name: "col1"
},
{
cellType: "expression",
name: "col1_exp",
expression: "propertyValue('col1', 'title')"
}]
}
]
});
survey.getQuestionByName("q2").addPanel();
survey.getQuestionByName("q3").addRow();
assert.deepEqual(survey.data, {
q1_exp: "Q1",
q2: [{ q2_q1_exp: "Q2_Q1" }],
q3: [{ col1_exp: "Column 1" }] }, "propertyValue works correctly");
});

0 comments on commit 52ff9f8

Please sign in to comment.