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 an API method that would return survey data with or without pages… #8485

Merged
merged 2 commits into from
Jul 5, 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
2 changes: 1 addition & 1 deletion src/jsonobject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1714,7 +1714,7 @@ export class JsonObject {
options?: ISaveToJSONOptions | boolean
): any {
if (!obj || !obj.getType) return obj;
if (typeof obj.getData === "function") return obj.getData();
if (!obj.isSurvey && typeof obj.getData === "function") return obj.getData();
var result = {};
if (property != null && !property.className) {
(<any>result)[JsonObject.typePropertyName] = property.getObjType(
Expand Down
41 changes: 20 additions & 21 deletions src/survey-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ export abstract class SurveyElementCore extends Base implements ILocalizableOwne
protected createLocTitleProperty(): LocalizableString {
return this.createLocalizableString("title", this, true);
}
/**
* Returns `true` if the survey element is a page.
* @see Base.getType
*/
public get isPage(): boolean { return false; }
/**
* Returns `true` if the survey element is a panel.
* @see Base.getType
*/
public get isPanel(): boolean { return false; }
/**
* Returns `true` if the survey element is a question.
* @see Base.getType
*/
public get isQuestion(): boolean { return false; }
/**
* Returns `true` if the element is a survey.
* @see Base.getType
*/
public get isSurvey(): boolean { return false; }
/**
* A title for the survey element. If `title` is undefined, the `name` property value is displayed instead.
*
Expand Down Expand Up @@ -671,27 +691,6 @@ export class SurveyElement<E = any> extends SurveyElementCore implements ISurvey
public setVisibleIndex(index: number): number {
return 0;
}
/**
* Returns `true` if the survey element is a page.
* @see Base.getType
*/
public get isPage(): boolean {
return false;
}
/**
* Returns `true` if the survey element is a panel.
* @see Base.getType
*/
public get isPanel(): boolean {
return false;
}
/**
* Returns `true` if the survey element is a question.
* @see Base.getType
*/
public get isQuestion(): boolean {
return false;
}
public delete(doDispose: boolean): void { }
//ILocalizableOwner
locOwner: ILocalizableOwner;
Expand Down
19 changes: 19 additions & 0 deletions src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2901,6 +2901,25 @@ export class SurveyModel extends SurveyElementCore
this.runConditions();
this.updateAllQuestionsValue(clearData);
}
public get isSurvey(): boolean { return true; }
/**
* Returns an object with survey results.
*
* If you want to get a survey results object that mirrors the survey structure, call the `getData()` method with an object that has the `includePages` and `includePanels` properties enabled. Without this object, the `getData()` method returns the [`data`](https://surveyjs.io/form-library/documentation/api-reference/survey-data-model#data) property value.
*
* ```js
* import { Model } from "survey-core";
*
* const surveyJson = { ... };
* const survey = new Model(surveyJson);
* survey.getData({ includePages: true, includePanels: true });
* ```
*/
public getData(options?: { includePages?: boolean, includePanels?: boolean }): any {
const opt = options || { includePages: false, includePanels: false };
if(!opt.includePages && !opt.includePanels) return this.data;
return this.getStructuredData(!!opt.includePages, !opt.includePanels ? (opt.includePages ? 1 : 0) : -1);
}
public getStructuredData(includePages: boolean = true, level: number = -1): any {
if (level === 0) return this.data;
const data: any = {};
Expand Down
24 changes: 24 additions & 0 deletions tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17956,6 +17956,30 @@ QUnit.test("getStructuredData function", function (assert) {
}, "includePages: false, level: 3");

});
QUnit.test("getData function", function (assert) {
const survey = new SurveyModel(structedDataSurveyJSON);
survey.setValue("q1", 100);
survey.setValue("q2", 200);
survey.setValue("q3", 300);
survey.setValue("q21", 2100);
survey.setValue("q22", 2200);
const data = survey.data;
assert.deepEqual(survey.getData(), data, "survey.getData()");
assert.deepEqual(survey.getData({}), data, "survey.getData({})");
assert.deepEqual(survey.getData({ includePages: false, includePanels: false }), data, "survey.getData({ includePages: false, includePanels: false })");
assert.deepEqual(survey.getData({ includePages: true, includePanels: false }), {
page1: { q1: 100, q2: 200, q3: 300 },
page2: { q21: 2100, q22: 2200 },
}, "survey.getData({ includePages: true, includePanels: false })");
assert.deepEqual(survey.getData({ includePages: true, includePanels: true }), {
page1: { q1: 100, panel1: { q2: 200, panel2: { q3: 300 } } },
page2: { q21: 2100, panel21: { q22: 2200 } },
}, "survey.getData({ includePages: true, includePanels: true })");
assert.deepEqual(survey.getData({ includePages: false, includePanels: true }), {
q1: 100, panel1: { q2: 200, panel2: { q3: 300 } },
q21: 2100, panel21: { q22: 2200 },
}, "survey.getData({ includePages: true, includePanels: true })");
});
QUnit.test("setStructuredData function", function (assert) {
const survey = new SurveyModel(structedDataSurveyJSON);
survey.setStructuredData({
Expand Down