Skip to content

Commit 61d4d45

Browse files
added custom config to select2 widget (https://surveyjs.answerdesk.io/internal/ticket/details/T645)
1 parent d8b7234 commit 61d4d45

File tree

1 file changed

+35
-19
lines changed

1 file changed

+35
-19
lines changed

src/select2.js

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ function init(Survey, $) {
44
activatedBy: "property",
55
name: "select2",
66
htmlTemplate: "<select style='width: 100%;'></select>",
7-
widgetIsLoaded: function() {
7+
widgetIsLoaded: function () {
88
return typeof $ == "function" && !!$.fn.select2;
99
},
10-
isFit: function(question) {
10+
isFit: function (question) {
1111
if (widget.activatedBy == "property")
1212
return (
1313
question["renderAs"] === "select2" &&
@@ -19,7 +19,7 @@ function init(Survey, $) {
1919
return question.getType() === "select2";
2020
return false;
2121
},
22-
activatedByChanged: function(activatedBy) {
22+
activatedByChanged: function (activatedBy) {
2323
if (!this.widgetIsLoaded()) return;
2424
widget.activatedBy = activatedBy;
2525
Survey.JsonObject.metaData.removeProperty("dropdown", "renderAs");
@@ -29,12 +29,21 @@ function init(Survey, $) {
2929
default: "standard",
3030
choices: ["standard", "select2"]
3131
});
32+
Survey.JsonObject.metaData.addProperty("dropdown", {
33+
name: "select2Config",
34+
default: {}
35+
});
3236
}
3337
if (activatedBy == "customtype") {
3438
Survey.JsonObject.metaData.addClass("select2", [], null, "dropdown");
39+
Survey.JsonObject.metaData.addProperty("select2", {
40+
name: "select2Config",
41+
default: {}
42+
});
3543
}
3644
},
37-
afterRender: function(question, el) {
45+
afterRender: function (question, el) {
46+
var settings = question.select2Config;
3847
var $el = $(el).is("select") ? $(el) : $(el).find("select");
3948
var othersEl = document.createElement("input");
4049
othersEl.type = "text";
@@ -45,32 +54,39 @@ function init(Survey, $) {
4554
.parent()
4655
.get(0)
4756
.appendChild(othersEl);
48-
var widget = $el.select2({
49-
theme: "classic"
50-
});
51-
var updateValueHandler = function() {
57+
58+
var updateValueHandler = function () {
5259
$el.val(question.value).trigger("change");
5360
othersEl.style.display = !question.isOtherSelected ? "none" : "";
5461
};
55-
var updateCommentHandler = function() {
62+
var updateCommentHandler = function () {
5663
othersEl.value = question.comment ? question.comment : "";
5764
};
58-
var othersElChanged = function() {
65+
var othersElChanged = function () {
5966
question.comment = othersEl.value;
6067
};
61-
var updateChoices = function() {
68+
var updateChoices = function () {
6269
$el.select2().empty();
63-
$el.select2({
64-
data: question.visibleChoices.map(function(choice) {
65-
return { id: choice.value, text: choice.text };
66-
})
67-
});
70+
71+
if (settings.ajax) {
72+
$el.select2(settings);
73+
} else {
74+
$el.select2({
75+
theme: "classic",
76+
data: question.visibleChoices.map(function (choice) {
77+
return {
78+
id: choice.value,
79+
text: choice.text
80+
};
81+
})
82+
});
83+
}
6884
updateValueHandler();
6985
updateCommentHandler();
7086
};
7187
question.choicesChangedCallback = updateChoices;
7288
updateChoices();
73-
$el.on("select2:select", function(e) {
89+
$el.on("select2:select", function (e) {
7490
question.value = e.target.value;
7591
});
7692
othersEl.onchange = othersElChanged;
@@ -79,7 +95,7 @@ function init(Survey, $) {
7995
updateValueHandler();
8096
updateCommentHandler();
8197
},
82-
willUnmount: function(question, el) {
98+
willUnmount: function (question, el) {
8399
$(el)
84100
.find("select")
85101
.off("select2:select")
@@ -94,4 +110,4 @@ if (typeof Survey !== "undefined") {
94110
init(Survey, window.$);
95111
}
96112

97-
export default init;
113+
export default init;

0 commit comments

Comments
 (0)