bootstrap select插件封裝成Vue2.0組件
更新時間:2017年04月17日 10:12:07 作者:vivid_renzaijianghu
這篇文章主要為大家詳細介紹了bootstrap select插件封裝成Vue2.0組件的相關方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
因為bootstrap-select功能比較強大,而且樣式還不錯,所以在項目使用了vue,所以,覺得對bootstrap-select進行封裝。
html
復制代碼 代碼如下:
<my-select :options="input.options" v-model="input.value" ref="typeSelect" :index="index" :childidx="childIdx" :load="load" :multiple="input.multiple" :method="change"></my-select>
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ā)現是vue自身的問題:因為vue對象有在重新渲染html的過程中會復用原來相同的vue對象,所以導致會導致selcet對象錯位。解決方案:將每個select對象打上一個標簽key。雖然可能導致性能的下降,但是不會導致錯誤。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
scroll事件實現監(jiān)控滾動條并分頁顯示(zepto.js)
這篇文章主要為大家詳細介紹了scroll事件實現監(jiān)控滾動條并分頁顯示示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12一文詳解requestAnimationFrame請求動畫幀
requestAnimationFrame是一個用于動畫效果的 API,它使用瀏覽器的刷新率來執(zhí)行回調函數,通常每秒鐘執(zhí)行60次,這篇文章主要給大家介紹了關于requestAnimationFrame請求動畫幀的相關資料,需要的朋友可以參考下2023-12-12