vue----Shopcar 大小选-加减-总价-总件数-功能总结

vue----Shopcar 大小选-加减-总价-总件数-功能总结


一:大小选

二:计算总价和总件数

三:商品数量的加减功能

  1. 大小选**
<input
        type="checkbox"
        class="custom-control-input"
        id="footerCheck"
        v-model="ck"//此处用v-model
      />



 // 小选与大选
  props: ["list"],
  computed: {
    ck: {
      //大选决定小选
      set(val) {
        this.list.forEach((e) => (e.goods_state = val));
      },
      // 小选决定大选
      get() {
        return this.list.every((item) => item.goods_state == true);
      },
    },
  },         

二:计算总价和总件数

 
    computed: {
    // 计算总价 
    allprice() {
      let sum = 0;
      this.list.forEach((item) => {
        if (item.goods_state == true) {
          sum += item.goods_price * item.goods_count;
        }
      });
      return sum;
    },
    // 计算总件数, reduce((sum,item)=>{},0)用法
    allcount() {
      return this.list.reduce((sum, item) => {
        if (item.goods_state == true) {
          sum += item.goods_count;
        }
        return sum;
      }, 0);
    },
  }

三:商品数量的加减功能

<div class="my-counter">
  //减功能
            <button
              type="button"
              class="btn btn-light"
              @click="item.goods_count > 1 && item.goods_count--"//减功能
            >
              -
            </button>
            <input
              type="number"
              class="form-control inp"
              v-model="item.goods_count"
              @change="change" //优化数量不能为负
            />
   //加功能
            <button
              type="button"
              class="btn btn-light"
              @click="item.goods_count++" //加功能
            >
              +
            </button>
</div>

//优化数量不能为空
<script>
export default {
  props: ["item"],
  methods: {
    change() {
      if (this.item.goods_count < 1) {
        this.item.goods_count = 1;
      }
    },
  },
};
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值