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对象的时候,加载的值会发生改变,纠结了半天发现是vue自身的问题:因为vue对象有在重新渲染html的过程中会复用原来相同的vue对象,所以导致会导致selcet对象错位。解决方案:将每个select对象打上一个标签key。虽然可能导致性能的下降,但是不会导致错误。

这里写图片描述

vue官网

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值