Skip to content

Instantly share code, notes, and snippets.

@kl3sk
Forked from jasonlfunk/vue2-select2.js
Last active March 14, 2018 10:44
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 kl3sk/84f70dacea4e5218280442c0639a3dae to your computer and use it in GitHub Desktop.
Save kl3sk/84f70dacea4e5218280442c0639a3dae to your computer and use it in GitHub Desktop.
...
data () {
...
newType: {},
select2Options: {
parent: '#manager-modal'
}
}
...
<select2 v-model="newType" :conf="select2Options" :val="currentId">
<option v-for="type in typesData.children" :value="type.id">{{ type.name }}</option>
</select2>
Vue.component('select2', {
props: {
options: {
type: Object,
default: () => {}
},
conf: {
type: Object,
default: () => {}
},
value: {
type: null,
default: null
},
multiple: {
type: Boolean,
default: false
},
palceholder: {
type: String,
default: ''
}
},
template: `
<div>
<select :multiple="multiple" ref='select' :data-placeholder="placeholder">
<slot></slot>
</select>
</div>
`,
computed: {
opt () {
return {
data: this.options,
dropdownParent: this.conf.parent ? $(this.conf.parent) : $(window.document.body)
}
}
},
mounted () {
let vm = this;
$(this.$refs.select)
.select2(this.opt)
.on('change', function (ev, args) {
if (!(args && "ignore" in args)) {
vm.$emit('input', $(this).val())
}
});
Vue.nextTick(() => {
const S2 = $(this.$refs.select)
if(this.val) {
S2.val(this.val)
}
S2.trigger('change', {ignore: true})
});
},
watch: {
value (value, oldValue) {
// update value
$(this.$refs.select)
.val(this.value)
.trigger('change', {ignore: true});
},
options (options) {
// update options
$(this.$refs.select).select2({data: options})
}
},
destroyed () {
$(this.$refs.select).off().select2('destroy')
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment