From 52ff9f84e200ede46e345b5e51cc130986bddace Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 14 Sep 2023 10:27:20 +0300 Subject: [PATCH] Features/6874 displayvalue function (#6953) * Add displayValue function #6874 * Add proeprtyValue function #6874 --- src/functionsfactory.ts | 25 +++++++++ tests/surveytests.ts | 118 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+) diff --git a/src/functionsfactory.ts b/src/functionsfactory.ts index 28d86b68bd..f686d0b5cd 100644 --- a/src/functionsfactory.ts +++ b/src/functionsfactory.ts @@ -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); \ No newline at end of file diff --git a/tests/surveytests.ts b/tests/surveytests.ts index 446aebd2cf..edb175f0a2 100644 --- a/tests/surveytests.ts +++ b/tests/surveytests.ts @@ -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"); +});