话不多说,直接上代码,挺简单的,但是还是记录下来吧

<template>
  <div class="container">
    <div class="cartList">

      <div class="cartItem" v-for="item in list">
        <div class="selctBtn" :class="item.checked?'checked':''" @click="selectItem(item)"></div>
        <div class="product">{{item.name}}</div>
      </div>

    </div>

    <div class="ops">
      <div class="opitem" @click="selectedAll">
        <span class="opBtn" :class="checkAllFlag?'checked':''"></span>
        <span>全选</span>
      </div>
      <div class="opitem" @click="selecteCancel">取消选择</div>
    </div>
  </div>
</template>

<script type='text/ecmascript-6'>
  export default {
    components: {},
    data() {
      return {
        checkAllFlag:false,
        list:[{
          name:'111',
          id:1,
        },{
          name:'222',
          id:2,
        }],
      };
    },
    mounted() {
      this.$nextTick(function(){
        //保证this.$el已经插入文档
      });
    },
    methods: {
      //选中一个item
      selectItem(item){
        if(typeof item.checked == 'undefined'){
          this.$set(item,'checked',true);
        }else {
          item.checked = !item.checked;
        }
        // 如果列表全被选中,则全选按钮被标红
        let maxlen = this.list.length;
        let count = 0;
        this.list.forEach(item=>{
          if(item.checked) count++;
        })
        if(count == maxlen) {
          this.checkAllFlag = true;
        }else {
          this.checkAllFlag = false;
        }
      },
      //全选
      selectedAll(){
        this.checkAllFlag = !this.checkAllFlag;
        this.list.forEach(item => {
          this.selectItem(item);
        });
      },
      //取消选择
      selecteCancel(){
        this.checkAllFlag = false;
        this.list.forEach(item => {
          item.checked = false;
        });
      }
    }
  }
</script>

<style lang='scss' scoped>
.container {
  width: 800px;
  display: flex;
  flex-flow: column;
  font-size: 16px;
  margin-left: 40px;
  .cartList{
    display: flex;
    flex-flow: column;
    .cartItem {
      display: flex;
      flex-flow: row;
      height: 50px;
      line-height: 50px;
      position: relative;
      .selctBtn{
        width: 15px;
        height: 15px;
        border: 1px solid #000;
        border-radius: 50%;
        margin-top: 17px;
        margin-right: 20px;
        cursor: pointer;
      }
      .checked {
        width: 15px;
        height: 15px;
        background: orangered;
        color: #fff;
        border: 1px solid orangered;
      }
      .checked::before {
        content: '✔';
        font-size: 14px;
        top: 0px;
        position: absolute;
        left: 2px;
      }
    }
    .cartItem:nth-child(1) {
    }
    .cartItem:nth-child(2) {
    }
    
  }
  .ops {
    display: flex;
    .opitem {
      margin-right: 20px;
      cursor: pointer;
      display: flex;
      .opBtn {
        width: 15px;
        height: 15px;
        border: 1px solid #999;
        border-radius: 50%;
        margin-right: 5px;
        margin-top: 3px;
      }
      .checked {
        content: '';
        background: red;
        border: 1px solid red;
      }
    }
  }
}
</style>

实现的效果图

Logo

前往低代码交流专区

更多推荐