Check-in rule editor: Fix reactivity issue in select2 component

This commit is contained in:
Raphael Michel
2023-02-07 16:43:02 +01:00
parent 98d290992c
commit accb9f8b13

View File

@@ -10,9 +10,14 @@
' <slot></slot>\n' + ' <slot></slot>\n' +
' </select>'), ' </select>'),
mounted: function () { mounted: function () {
this.build();
},
methods: {
build: function () {
var vm = this; var vm = this;
var multiple = this.multiple; var multiple = this.multiple;
$(this.$el) $(this.$el)
.empty()
.select2(this.opts()) .select2(this.opts())
.val(this.value || "") .val(this.value || "")
.trigger("change") .trigger("change")
@@ -28,7 +33,6 @@
} }
$(vm.$el).trigger("change"); $(vm.$el).trigger("change");
}, },
methods: {
opts: function () { opts: function () {
return { return {
theme: "bootstrap", theme: "bootstrap",
@@ -60,17 +64,29 @@
}, },
watch: { watch: {
placeholder: function (val) { placeholder: function (val) {
$(this.$el).empty().select2(this.opts()); $(this.$el).select2("destroy");
this.build(); this.build();
}, },
required: function (val) { required: function (val) {
$(this.$el).empty().select2(this.opts()); $(this.$el).select2("destroy");
this.build(); this.build();
}, },
url: function (val) { url: function (val) {
$(this.$el).empty().select2(this.opts()); $(this.$el).select2("destroy");
this.build(); this.build();
}, },
value: function (newval, oldval) {
if (JSON.stringify(newval) !== JSON.stringify(oldval)) {
$(this.$el).empty();
if (newval) {
for (var i = 0; i < newval["objectList"].length; i++) {
var option = new Option(newval["objectList"][i]["lookup"][2], newval["objectList"][i]["lookup"][1], true, true);
$(this.$el).append(option);
}
}
$(this.$el).trigger("change");
}
},
}, },
destroyed: function () { destroyed: function () {
$(this.$el) $(this.$el)