因為bootstrap-select功能比較強大,而且樣式還不錯,所以在項目使用了vue,所以,覺得對bootstrap-select進行封裝。
html
js
// select 插件Vue.component('vm-select', { props : ['options', 'value', 'multiple', 'method', 'load', 'index', 'childidx'], template : "<select :multiple='multiple' class='selectpicker' data-live-search='true' title='請選擇' data-live-search-placeholder='搜索'><option :value='option.value' v-for='option in options'>{{ option.label }}</option></select>", mounted : function () { var vm = this; $(this.$el).selectpicker('val', this.value != null ? this.value : null); $(this.$el).on('changed.bs.select', function () { vm.$emit('input', $(this).val()); if (typeof(vm.method) != 'undefined') { vm.method(vm.index, vm.childidx, this.value); } }); $(this.$el).on('show.bs.select', function () { if (typeof(vm.load) != 'undefined') { vm.load(vm.index, vm.childidx); } }); }, updated : function () { $(this.$el).selectpicker('refresh'); }, destroyed : function () { $(this.$el).selectpicker('destroy'); }});不得不提一下,在使用多個select的時候,在刪除某一個selcet對象的時候,加載的值會發(fā)生改變,糾結了半天發(fā)現(xiàn)是vue自身的問題:因為vue對象有在重新渲染html的過程中會復用原來相同的vue對象,所以導致會導致selcet對象錯位。解決方案:將每個select對象打上一個標簽key。雖然可能導致性能的下降,但是不會導致錯誤。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答