<!DOCTYPE html>
<html>
<head>
<title>Select2 Widget, jQuery Survey Library Example</title>
<script src="https://unpkg.com/jquery"></script>
<script src="https://surveyjs.azureedge.net/1.0.22/survey.jquery.js"></script>
<link href="https://surveyjs.azureedge.net/1.0.22/survey.css" type="text/css" rel="stylesheet"/>
<link rel="stylesheet" href="./index.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/js/select2.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/css/select2.min.css" rel="stylesheet"/>
<script src="./widgets.js"></script>
</head>
<body>
<div id="surveyElement"></div>
<div id="surveyResult"></div>
<script type="text/javascript" src="./index.js"></script>
</body>
</html>
Survey
.StylesManager
.applyTheme("default");
var json = {
"elements": [
{
"type": "dropdown",
"renderAs": "select2",
"select2Config": {
"allowClear": true,
"placeholder": "Choose country..."
},
"choicesByUrl": {
"url": "https://restcountries.eu/rest/v1/all"
},
"name": "countries",
"title": "Please select the country you have arrived from:"
}
]
};
window.survey = new Survey.Model(json);
survey
.onComplete
.add(function (result) {
document
.querySelector('#surveyResult')
.innerHTML = "result: " + JSON.stringify(result.data);
});
$("#surveyElement").Survey({model: survey});
function init(Survey, $) {
$ = $ || window.$;
var widget = {
activatedBy: "property",
name: "select2",
htmlTemplate: "<select style='width: 100%;'></select>",
widgetIsLoaded: function () {
return typeof $ == "function" && !!$.fn.select2;
},
isFit: function (question) {
if (widget.activatedBy == "property")
return (
question["renderAs"] === "select2" &&
question.getType() === "dropdown"
);
if (widget.activatedBy == "type")
return typeof question.getType() === "dropdown";
if (widget.activatedBy == "customtype")
return question.getType() === "select2";
return false;
},
activatedByChanged: function (activatedBy) {
if (!this.widgetIsLoaded()) return;
widget.activatedBy = activatedBy;
Survey.JsonObject.metaData.removeProperty("dropdown", "renderAs");
if (activatedBy == "property") {
Survey.JsonObject.metaData.addProperty("dropdown", {
name: "renderAs",
default: "standard",
choices: ["standard", "select2"]
});
Survey.JsonObject.metaData.addProperty("dropdown", {
name: "select2Config",
default: null
});
}
if (activatedBy == "customtype") {
Survey.JsonObject.metaData.addClass("select2", [], null, "dropdown");
Survey.JsonObject.metaData.addProperty("select2", {
name: "select2Config",
default: null
});
}
},
afterRender: function (question, el) {
var settings = question.select2Config;
var $el = $(el).is("select") ? $(el) : $(el).find("select");
var othersEl = document.createElement("input");
othersEl.type = "text";
othersEl.style.marginTop = "3px";
othersEl.style.display = "none";
othersEl.style.width = "100%";
$el
.parent()
.get(0)
.appendChild(othersEl);
var updateValueHandler = function () {
$el.val(question.value).trigger("change");
othersEl.style.display = !question.isOtherSelected ? "none" : "";
};
var updateCommentHandler = function () {
othersEl.value = question.comment ? question.comment : "";
};
var othersElChanged = function () {
question.comment = othersEl.value;
};
var updateChoices = function () {
$el.select2().empty();
if (settings) {
if (settings.ajax) {
$el.select2(settings);
} else {
settings.data = question.visibleChoices.map(function (choice) {
return {
id: choice.value,
text: choice.text
};
});
$el.select2(settings);
}
} else {
$el.select2({
theme: "classic",
data: question.visibleChoices.map(function (choice) {
return {
id: choice.value,
text: choice.text
};
})
});
}
updateValueHandler();
updateCommentHandler();
};
question.choicesChangedCallback = updateChoices;
updateChoices();
$el.on("select2:select", function (e) {
question.value = e.target.value;
});
othersEl.onchange = othersElChanged;
question.valueChangedCallback = updateValueHandler;
question.commentChangedCallback = updateCommentHandler;
updateValueHandler();
updateCommentHandler();
},
willUnmount: function (question, el) {
$(el)
.find("select")
.off("select2:select")
.select2("destroy");
}
};
Survey.CustomWidgetCollection.Instance.addCustomWidget(widget);
}
if (typeof Survey !== "undefined") {
init(Survey, window.$);
}