几个选项共用一个多选按钮(样式修改记录)

这篇文章展示了如何在Vue.js应用中创建全选和反选功能。通过使用`<template>`、`<el-button>`、`<el-checkbox-group>`等组件,结合`v-model`进行数据绑定和`@click`事件处理,实现了对选项的全选和反选操作。CSS部分则定制了勾选框的样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

htm:

<template>
  <div>
	<div>
        <el-button class="sure-btn" @click="selectAll">全选</el-button>
        <el-button class="cancel-btn" @click="selectOther">反选</el-button>
	</div>
    <el-checkbox-group v-model="checkedType" @change="changeCheck">
       <el-checkbox label="1">
       	  <div>大型商品1类<span class="item-info">洗衣机、彩电</span></div>
          <div>大型商品2类<span class="item-info">冰箱、抽油烟机</span></div>
    	</el-checkbox>
    	<el-checkbox label="2">
       	   <div>小型商品1类<span class="item-info">玻璃杯、瓷碗、勺筷</span></div>
           <div>小型商品2类<span class="item-info">毛巾、袜子、手套</span></div>
           <div>小型商品3类<span class="item-info">中性笔、胶带、笔记本</span></div>
     	</el-checkbox>
     </el-checkbox-group>
   </div>
</template>

js:

export default {
  data(){
    return {
      checkedType:[],
      checkedRange:['1','2','3','4','5','6']
    }
  },
  methods:{
    changeCheck(val){
      this.checkedType = val;
    },
    selectAll(){
      this.checkedType = this.checkedRange.map(item => item);
    },
    selectOther(){
      this.checkedType = this.checkedRange.filter(item => !this.checkedType.some(v => item == v))
    }
 }
}

scss:

.down-check{	//整体调整
  .el-checkbox{    
    display: inline-block;    //为多选小方块垂直居中做准备
    width:100%;
    .el-checkbox__label{//文字样式自定义
      font-size:16px;
      color:#1F2325;
      line-height:44px;
    }
    .el-checkbox__input.is-checked .el-checkbox__inner{//选中后,勾选框样式自定义
      background-color:#2B67FF;
      border-color:#2B67FF;
    }
  }
  label.el-checkbox{               //多选小方块垂直居中
    position:relative;
    span.el-checkbox__input{
      position:absolute;
      height: 100%;
      display: flex;
      align-items: center;
    }
    span.el-checkbox__label{
      margin-left:20px;     //多选内容label间距微调
      padding-left:40px;   
    }
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值