Skip to content

#9691 Add method that maps survey data to fieldset JSON #9709

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
23 changes: 22 additions & 1 deletion packages/survey-core/src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3161,6 +3161,27 @@ export class SurveyModel extends SurveyElementCore
this.valuesHash = {};
this.setDataCore(data, !data);
}
public calcWithExpressionMap(map: any) {
const data = this.data;
const res = {};
Object.keys(map).forEach(key => {
const mapValue = (typeof (map[key]) === "object") ? map[key] : { expr: map[key] } as any;
const val = new ExpressionRunner(mapValue.expr).run(data);
let newVal = null;
if (typeof (mapValue.values) === "object") {
newVal = mapValue.values[val];
}
else {
if (mapValue.values === undefined) {
newVal = val;
} else {
newVal = Array.isArray(val) ? val.indexOf(mapValue.values) >= 0 : mapValue.values == val;
}
}
res[key] = newVal;
});
return res;
}
/**
* Merges a specified data object with the object from the [`data`](https://surveyjs.io/form-library/documentation/api-reference/survey-data-model#data) property.
*
Expand Down Expand Up @@ -8188,7 +8209,7 @@ export class SurveyModel extends SurveyElementCore
const advHeader = layoutElement && layoutElement.data as Cover;
if (this.showTOC && !(advHeader && advHeader.hasBackground)) {
if (container === "center") {
containerLayoutElements.push(layoutElement);
containerLayoutElements.push(layoutElement);
}
} else {
if (layoutElement.container === container) {
Expand Down
95 changes: 95 additions & 0 deletions packages/survey-core/tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21986,3 +21986,98 @@ QUnit.test("Don't rise onPageAdded when mooving question", function (assert) {
assert.equal(pageAddedRaisedCount, 0, "onPageAdded is not raised");
assert.equal(survey.pages[0].name, "page2", "page2 is the first page");
});
QUnit.test("test calcWithExpressionMap function", function (assert) {
var survey = new SurveyModel({
"pages": [
{
"name": "page1",
"elements": [
{
"type": "text",
"name": "question1"
},
{
"type": "text",
"name": "question2"
}
]
}
]
});
assert.deepEqual(survey.data, {}, "there is no data");
survey.data = { question1: "item1", question2: 5 };
assert.deepEqual(
survey.data,
{ question1: "item1", question2: 5 },
"set the object"
);

const map = {
"f1": "{question1}",
"f2": {
expr: "{question2}"
},
"f1_1": "question1"
};

const mappedData = survey.calcWithExpressionMap(map);

assert.deepEqual(
mappedData,
{ f1: "item1", f2: 5, f1_1: "question1" },
"set the object"
);
});

QUnit.test("test calcWithExpressionMap function for radio and checkboxes", function (assert) {
var survey = new SurveyModel({
"pages": [
{
"name": "page1",
"elements": [
{
"type": "radiogroup",
"name": "question1",
"choiсes": ["item1", "item2", "item3"]
},
{
"type": "checkboxes",
"name": "question2",
"choiсes": ["item1", "item2", "item3"]
}
]
}
]
});
survey.data = { question1: "item1", question2: ["item1", "item3"] };

const map = {
"f1": {
expr: "{question1}",
values: {
"item1": "1",
"item2": "2",
"item3": "3"
}
},
"f2_1": {
expr: "{question2}",
values: "item1"
},
"f2_2": {
expr: "{question2}",
values: "item2"
},
"f2_3": {
expr: "{question2}",
values: "item3"
}
};

const mappedData = survey.calcWithExpressionMap(map);

assert.deepEqual(
mappedData,
{ f1: "1", f2_1: true, f2_2: false, f2_3: true },
);
});