Created
June 2, 2019 07:31
-
-
Save tyler-shaw/4ad5903fda6fb47f201e28fca5171d7d to your computer and use it in GitHub Desktop.
Vue Select2 Wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div> | |
Select an option: | |
<select2 v-model="fruit"> | |
<option value="">None</option> | |
<option value="apple">Apple</option> | |
<option value="orange">Orange</option> | |
</select2> | |
</div> | |
</template> | |
<script> | |
import Select2 from './Select2.vue'; | |
export default { | |
components: { | |
'select2': Select2, | |
}, | |
data() { | |
return { | |
fruit: '', | |
}; | |
}, | |
}; | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<select> | |
<slot></slot> | |
</select> | |
</template> | |
<script> | |
export default { | |
props: ['options', 'value'], | |
mounted: function () { | |
let vm = this | |
$(this.$el) | |
.select2({ data: this.options }) | |
.val(this.value) | |
.trigger('change') | |
.on('change', function (e) { | |
vm.$emit('input', this.value); | |
vm.$emit('change', e); | |
}) | |
}, | |
watch: { | |
value: function (value) { | |
$(this.$el).val(value).trigger('change'); | |
}, | |
options: function (options) { | |
$(this.$el).empty().select2({ data: options }) | |
} | |
}, | |
destroyed: function () { | |
$(this.$el).off().select2('destroy') | |
} | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment