vue the checkbox style custom rewriting; loop through checkbox, get different v-model binding value; and obtaining the current status checked, and Select Unselect functions.

Began to write this function, I had to Tucao original checkbox, ugly gray small box, although a single box eleUI, mintUI, other frameworks have their optimization, but still do not want this. Then we look at how to deal with it.

 <section class="box">
     <label  :for="item3" @click="chooseType($event,index3)"  v-for="(item3,index3) in type"  class="labelName">
        <input type="checkbox" :value="item3" :id="item3" v-model="checkedValue" class="checkboxList">            // for属性一定要与id一致,原因请看上图
        <div class="name">{{item3}}</div>  // label的值            // checkbox的v-model绑定值一定要是数组
        &nbsp;&nbsp;
        {{checkedValue}}   // 查看值
     </label>
     <button @click="chooseQu">全选</button>
     <button @click="chooseNo">全不选</button>

  </section>

 data: 

data(){
        return{
          checkedValue: [],
          type:['a','b','c','d']   // 测试数据,可在mounted钩子函数中将后台数据赋值
        }
},

methods:

methods:{
        chooseType(e,val){
          console.log(e.target.checked)   // 可获取当前的checked 状态
          console.log(val)                // 可获取到当前的下标。

        },
        chooseQu(){
          // document.querySelectorAll('.checkboxList').setAttribute("checked","true");
          this.checkedValue = this.type ;  //将原数据全部赋值给checkedValue,全部置为true.  
        },
        chooseNo(){
          this.checkedValue = [] ;    // checkedValue 为空即将checkedValue全部置为false,
        }

  }

样式的调整:

// 样式可根据自己的需要,通过控制外层div自行配置,
.box{
/*display: flex;*/
/*justify-content: start;*/
/*align-items: center;*/
}
.labelName{
width: 25%;
display: flex;
justify-content: center;
margin-bottom: 20px;
}
/*------新-----*/
  input[type=checkbox] {
    width: 20px;                  // 可根据自己的需要进行配置input的大小。
    height: 20px;
    border-radius: 10px;
    -webkit-appearance: none;
    background-color: transparent;
    border: 0;
    outline: 0 !important;
    color: #d8d8d8;
    position: relative;
  }
  input[type=checkbox]:before{
    content: "";
    display:block;
    width: 20px;
    height: 20px;
    border-radius: 10px;
    border: 1px solid #ddd;
    background-color: #fff;
    box-sizing:border-box;
    position: absolute;
  }

  input[type=checkbox]:disabled:before{
    content: "";
    display:block;
    width: 20px;
    height: 20px;
    border-radius: 10px;
    border: 1px solid #333;
    background-color: #333;
    box-sizing:border-box;                     // 可控制调整背景色。
    position: absolute;
  }
  input[type=checkbox]:checked:before{
    content: "";
    display:block;
    width: 20px;
    height: 20px;
    border-radius: 10px;
    border: 1px solid #D2A47E;
    background-color: #D2A47E;                 //可控制checked背景色
    box-sizing:border-box;
    position: absolute;
  }
  input[type=checkbox]:checked:after{
    content: "";
    display:block;
    /*width: .15rem;*/
    /*height: .3rem;*/
    /*border-radius:  .06rem;*/
    width: 7px;                          // 此处是控制获取checked=true 后的颜色,请注意宽高比约1:2 。 原理是通过伪类去控制样式。
    height: 15px;
    /*border-radius:3px;*/
    border-left: .07rem solid #fff;
    border-top: .07rem solid #fff;
    box-sizing:border-box;
    position: absolute;
    transform: rotate(-135deg) translate(-70%, 25%);
  }

 

Guess you like

Origin www.cnblogs.com/panax/p/10985188.html