-
-
Save kl3sk/84f70dacea4e5218280442c0639a3dae to your computer and use it in GitHub Desktop.
This file contains hidden or 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
... | |
data () { | |
... | |
newType: {}, | |
select2Options: { | |
parent: '#manager-modal' | |
} | |
} | |
... |
This file contains hidden or 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
<select2 v-model="newType" :conf="select2Options" :val="currentId"> | |
<option v-for="type in typesData.children" :value="type.id">{{ type.name }}</option> | |
</select2> |
This file contains hidden or 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
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