Skip to content

Instantly share code, notes, and snippets.

@themsaid
Created November 17, 2015 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save themsaid/c0548e19f247b832ac4f to your computer and use it in GitHub Desktop.
Save themsaid/c0548e19f247b832ac4f to your computer and use it in GitHub Desktop.
Wrapping the select2 plugin into a Vue.js custom directive.
Vue.directive('select', {
twoWay: true,
priority: 1000,
params: ['options'],
bind: function () {
var self = this;
$(this.el)
.select2({
data: this.params.options
})
.on('change', function () {
if ($(this).val()) {
self.set($(this).val());
} else {
self.set($(this).attr('multiple') ? [] : $(self.el).val());
}
})
},
update: function (value, oldVal) {
var el = $(this.el);
if (el.val()) {
this.set(el.val());
} else {
this.set(el.attr('multiple') ? [] : "");
}
if (!value && oldVal != undefined) {
el.select2('val', '');
}
},
unbind: function () {
$(this.el).off().select2('destroy')
}
});
@kmukku
Copy link

kmukku commented Dec 3, 2017

@themsaid How to make this work with current spark 5 and prerendered select options on blade template?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment