bootstrap select插件封裝成Vue2.0組件
因?yàn)閎ootstrap-select功能比較強(qiáng)大,而且樣式還不錯(cuò),所以在項(xiàng)目使用了vue,所以,覺得對(duì)bootstrap-select進(jìn)行封裝。
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='請(qǐng)選擇' 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');
}
});
不得不提一下,在使用多個(gè)select的時(shí)候,在刪除某一個(gè)selcet對(duì)象的時(shí)候,加載的值會(huì)發(fā)生改變,糾結(jié)了半天發(fā)現(xiàn)是vue自身的問題:因?yàn)関ue對(duì)象有在重新渲染html的過程中會(huì)復(fù)用原來相同的vue對(duì)象,所以導(dǎo)致會(huì)導(dǎo)致selcet對(duì)象錯(cuò)位。解決方案:將每個(gè)select對(duì)象打上一個(gè)標(biāo)簽key。雖然可能導(dǎo)致性能的下降,但是不會(huì)導(dǎo)致錯(cuò)誤。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
knockoutjs模板實(shí)現(xiàn)樹形結(jié)構(gòu)列表
這篇文章主要介紹了knockoutjs模板實(shí)現(xiàn)樹形結(jié)構(gòu)列表的實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-07-07
bootstrap fileinput完整實(shí)例分享
這篇文章主要為大家分享文件上傳組件bootstrap fileinput完整實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
JS request函數(shù) 用來獲取url參數(shù)
項(xiàng)目中經(jīng)常會(huì)遇到這種問題 下面代碼解決問題!2010-05-05
JavaScript 阻塞方式實(shí)現(xiàn)異步任務(wù)隊(duì)列
本文主要介紹了JavaScript 阻塞方式實(shí)現(xiàn)異步任務(wù)隊(duì)列,主要介紹了兩種方案,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05

