Add Survey Creator / Form Builder to a Knockout or jQuery Application
This step-by-step tutorial will help you get started with the Survey Creator component in a Knockout or jQuery application. To add Survey Creator to your application, follow the steps below:
- Link Resources
- Configure Survey Creator
- Render Survey Creator
- Save and Load Survey Model Schemas
- Manage Image Uploads
Survey Creator is powered by Knockout and does not have an individual implementation for jQuery. However, you can integrate the version for Knockout into your jQuery application by following the same instructions.
Link Resources
Survey Creator for Knockout consists of two parts: survey-creator-core
(platform-independent code) and survey-creator-knockout
(view models). Each part includes style sheets and scripts. Insert links to these resources within the <head>
tag on your HTML page as shown below.
Survey Creator also requires SurveyJS resources. Link them after the Knockout library, but before the Survey Creator resources:
<head>
<!-- ... -->
<script type="text/javascript" src="https://unpkg.com/knockout/build/output/knockout-latest.js"></script>
<!-- SurveyJS resources -->
<link href="https://unpkg.com/survey-core/defaultV2.min.css" type="text/css" rel="stylesheet">
<script src="https://unpkg.com/survey-core/survey.core.min.js"></script>
<script src="https://unpkg.com/survey-knockout-ui/survey-knockout-ui.min.js"></script>
<!-- Survey Creator resources -->
<link href="https://unpkg.com/survey-creator-core/survey-creator-core.min.css" type="text/css" rel="stylesheet">
<script src="https://unpkg.com/survey-creator-core/survey-creator-core.min.js"></script>
<script src="https://unpkg.com/survey-creator-knockout/survey-creator-knockout.min.js"></script>
<!-- ... -->
</head>
Note that although standalone surveys support multiple predefined themes, surveys within Survey Creator apply only one theme ("Default V2"). However, you can use the survey configuration (JSON object) produced by Survey Creator to build a standalone survey and apply any theme to it. You can also customize Survey Creator theme colors as shown in the following example:
Configure Survey Creator
To configure the Survey Creator component, specify its properties in a configuration object. In this tutorial, the object enables the following properties:
showLogicTab
Displays the Logic tab in the tab panel.isAutoSave
Automatically saves the survey JSON schema on every change.
const creatorOptions = {
showLogicTab: true,
isAutoSave: true
};
Pass the configuration object to the SurveyCreator
constructor as shown in the code below to instantiate Survey Creator. Assign the produced instance to a constant that will be used later to render the component:
const creator = new SurveyCreator.SurveyCreator(creatorOptions);
View Full Code
<!DOCTYPE html>
<html>
<head>
<title>Survey Creator for Knockout</title>
<meta charset="utf-8">
<script type="text/javascript" src="https://unpkg.com/knockout/build/output/knockout-latest.js"></script>
<link href="https://unpkg.com/survey-core/defaultV2.min.css" type="text/css" rel="stylesheet">
<script src="https://unpkg.com/survey-core/survey.core.min.js"></script>
<script src="https://unpkg.com/survey-knockout-ui/survey-knockout-ui.min.js"></script>
<link href="https://unpkg.com/survey-creator-core/survey-creator-core.min.css" type="text/css" rel="stylesheet">
<script src="https://unpkg.com/survey-creator-core/survey-creator-core.min.js"></script>
<script src="https://unpkg.com/survey-creator-knockout/survey-creator-knockout.min.js"></script>
<script type="text/javascript" src="index.js"></script>
</head>
<body>
</body>
</html>
const creatorOptions = {
showLogicTab: true,
isAutoSave: true
};
const creator = new SurveyCreator.SurveyCreator(creatorOptions);
Render Survey Creator
Switch to the component template. Add a page element that will serve as the Survey Creator container:
<body>
<div id="surveyCreator" style="height: 100vh;"></div>
</body>
To render Survey Creator in the page element, call the render(containerId)
method on the Survey Creator instance you created in the previous step:
document.addEventListener("DOMContentLoaded", function() {
creator.render("surveyCreator");
});
View Full Code
<!DOCTYPE html>
<html>
<head>
<title>Survey Creator for Knockout</title>
<meta charset="utf-8">
<script type="text/javascript" src="https://unpkg.com/knockout/build/output/knockout-latest.js"></script>
<link href="https://unpkg.com/survey-core/defaultV2.min.css" type="text/css" rel="stylesheet">
<script src="https://unpkg.com/survey-core/survey.core.min.js"></script>
<script src="https://unpkg.com/survey-knockout-ui/survey-knockout-ui.min.js"></script>
<link href="https://unpkg.com/survey-creator-core/survey-creator-core.min.css" type="text/css" rel="stylesheet">
<script src="https://unpkg.com/survey-creator-core/survey-creator-core.min.js"></script>
<script src="https://unpkg.com/survey-creator-knockout/survey-creator-knockout.min.js"></script>
<script type="text/javascript" src="index.js"></script>
</head>
<body>
<div id="surveyCreator" style="height: 100vh;"></div>
</body>
</html>
const creatorOptions = {
showLogicTab: true,
isAutoSave: true
};
const creator = new SurveyCreator.SurveyCreator(creatorOptions);
document.addEventListener("DOMContentLoaded", function() {
creator.render("surveyCreator");
});
Save and Load Survey Model Schemas
Survey Creator produces survey model schemas as JSON objects. You can persist these objects on your server: save updates and restore previously saved schemas. To save a JSON object, implement the saveSurveyFunc
function. It accepts two arguments:
saveNo
An incremental number of the current change. Since web services are asynchronous, you cannot guarantee that the service receives the changes in the same order as the client sends them. For example, change #11 may arrive to the server faster than change #10. In your web service code, update the storage only if you receive changes with a highersaveNo
.callback
A callback function. Call it and passsaveNo
as the first argument. Set the second argument totrue
orfalse
based on whether the server applied or rejected the change.
The following code shows how to use the saveSurveyFunc
function to save a survey model schema in a localStorage
or in your web service:
creator.saveSurveyFunc = (saveNo, callback) => {
// If you use localStorage:
window.localStorage.setItem("survey-json", creator.text);
callback(saveNo, true);
// If you use a web service:
saveSurveyJson(
"https://your-web-service.com/",
creator.JSON,
saveNo,
callback
);
};
// If you use a web service:
function saveSurveyJson(url, json, saveNo, callback) {
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=UTF-8'
},
body: JSON.stringify(json)
})
.then(response => {
if (response.ok) {
callback(saveNo, true);
} else {
callback(saveNo, false);
}
})
.catch(error => {
callback(saveNo, false);
});
}
To load a survey model schema JSON into Survey Creator, assign the schema to Survey Creator's JSON
or text
property. Use text
if the JSON object is converted to a string; otherwise, use JSON
. The following code takes a survey model schema from the localStorage
. If the schema is not found (for example, when Survey Creator is launched for the first time), a default JSON is used:
const defaultJson = {
pages: [{
name: "Name",
elements: [{
name: "FirstName",
title: "Enter your first name:",
type: "text"
}, {
name: "LastName",
title: "Enter your last name:",
type: "text"
}]
}]
};
creator.text = window.localStorage.getItem("survey-json") || JSON.stringify(defaultJson);
View Full Code
<!DOCTYPE html>
<html>
<head>
<title>Survey Creator for Knockout</title>
<meta charset="utf-8">
<script type="text/javascript" src="https://unpkg.com/knockout/build/output/knockout-latest.js"></script>
<link href="https://unpkg.com/survey-core/defaultV2.min.css" type="text/css" rel="stylesheet">
<script src="https://unpkg.com/survey-core/survey.core.min.js"></script>
<script src="https://unpkg.com/survey-knockout-ui/survey-knockout-ui.min.js"></script>
<link href="https://unpkg.com/survey-creator-core/survey-creator-core.min.css" type="text/css" rel="stylesheet">
<script src="https://unpkg.com/survey-creator-core/survey-creator-core.min.js"></script>
<script src="https://unpkg.com/survey-creator-knockout/survey-creator-knockout.min.js"></script>
<script type="text/javascript" src="index.js"></script>
</head>
<body>
<div id="surveyCreator" style="height: 100vh;"></div>
</body>
</html>
const creatorOptions = {
showLogicTab: true,
isAutoSave: true
};
const defaultJson = {
pages: [{
name: "Name",
elements: [{
name: "FirstName",
title: "Enter your first name:",
type: "text"
}, {
name: "LastName",
title: "Enter your last name:",
type: "text"
}]
}]
};
const creator = new SurveyCreator.SurveyCreator(creatorOptions);
creator.text = window.localStorage.getItem("survey-json") || JSON.stringify(defaultJson);
creator.saveSurveyFunc = (saveNo, callback) => {
window.localStorage.setItem("survey-json", creator.text);
callback(saveNo, true);
// saveSurveyJson(
// "https://your-web-service.com/",
// creator.JSON,
// saveNo,
// callback
// );
};
document.addEventListener("DOMContentLoaded", function() {
creator.render("surveyCreator");
});
// function saveSurveyJson(url, json, saveNo, callback) {
// fetch(url, {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json;charset=UTF-8'
// },
// body: JSON.stringify(json)
// })
// .then(response => {
// if (response.ok) {
// callback(saveNo, true);
// } else {
// callback(saveNo, false);
// }
// })
// .catch(error => {
// callback(saveNo, false);
// });
// }
Manage Image Uploads
When survey authors design a form or questionnaire, they can add images to use as a survey logo or background, in the survey header, or within Image and Image Picker questions. Those images are embedded in the survey and theme JSON schemas as Base64 URLs. However, this technique increases the schema size. To avoid this, you can upload images to a server and save only image links in the JSON schemas.
To implement image upload, handle the onUploadFile
event. Its options.files
parameter stores the images you should send to your server. Once the server responds with an image link, call the options.callback(status, imageLink)
method. Pass "success"
as the status
parameter and a link to the uploaded image as the imageLink
parameter.
creator.onUploadFile.add((_, options) => {
const formData = new FormData();
options.files.forEach(file => {
formData.append(file.name, file);
});
fetch("https://example.com/uploadFiles", {
method: "post",
body: formData
}).then(response => response.json())
.then(result => {
options.callback(
"success",
// A link to the uploaded file
"https://example.com/files?name=" + result[options.files[0].name]
);
})
.catch(error => {
options.callback('error');
});
});
View Full Code
<!DOCTYPE html>
<html>
<head>
<title>Survey Creator for Knockout</title>
<meta charset="utf-8">
<script type="text/javascript" src="https://unpkg.com/knockout/build/output/knockout-latest.js"></script>
<link href="https://unpkg.com/survey-core/defaultV2.min.css" type="text/css" rel="stylesheet">
<script src="https://unpkg.com/survey-core/survey.core.min.js"></script>
<script src="https://unpkg.com/survey-knockout-ui/survey-knockout-ui.min.js"></script>
<link href="https://unpkg.com/survey-creator-core/survey-creator-core.min.css" type="text/css" rel="stylesheet">
<script src="https://unpkg.com/survey-creator-core/survey-creator-core.min.js"></script>
<script src="https://unpkg.com/survey-creator-knockout/survey-creator-knockout.min.js"></script>
<script type="text/javascript" src="index.js"></script>
</head>
<body>
<div id="surveyCreator" style="height: 100vh;"></div>
</body>
</html>
const creatorOptions = {
showLogicTab: true,
isAutoSave: true
};
const defaultJson = {
pages: [{
name: "Name",
elements: [{
name: "FirstName",
title: "Enter your first name:",
type: "text"
}, {
name: "LastName",
title: "Enter your last name:",
type: "text"
}]
}]
};
const creator = new SurveyCreator.SurveyCreator(creatorOptions);
creator.text = window.localStorage.getItem("survey-json") || JSON.stringify(defaultJson);
creator.saveSurveyFunc = (saveNo, callback) => {
window.localStorage.setItem("survey-json", creator.text);
callback(saveNo, true);
// saveSurveyJson(
// "https://your-web-service.com/",
// creator.JSON,
// saveNo,
// callback
// );
};
// creator.onUploadFile.add((_, options) => {
// const formData = new FormData();
// options.files.forEach(file => {
// formData.append(file.name, file);
// });
// fetch("https://example.com/uploadFiles", {
// method: "post",
// body: formData
// }).then(response => response.json())
// .then(result => {
// options.callback(
// "success",
// // A link to the uploaded file
// "https://example.com/files?name=" + result[options.files[0].name]
// );
// })
// .catch(error => {
// options.callback('error');
// });
// });
document.addEventListener("DOMContentLoaded", function() {
creator.render("surveyCreator");
});
// function saveSurveyJson(url, json, saveNo, callback) {
// fetch(url, {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json;charset=UTF-8'
// },
// body: JSON.stringify(json)
// })
// .then(response => {
// if (response.ok) {
// callback(saveNo, true);
// } else {
// callback(saveNo, false);
// }
// })
// .catch(error => {
// callback(saveNo, false);
// });
// }