购物车

6 篇文章 0 订阅
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet"  href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.0/dist/css/bootstrap.min.css"/>
    <script src="./js/vue.js"></script>
</head>
<body>
    <div  id="app">
        <table class="table">
            <thead class="thead-dark">
              <tr>
                <th scope="col">
                    全选
                  <input type="checkbox" @click="allcheck" v-model="allchecked"/>
                  <!-- <img v-show="" />
                  <img  v-show="!" /> -->
                </th>
                <th scope="col">商品</th>
                <th scope="col">单价</th>
                <th scope="col">数量</th>
                <th scope="col">小计</th>
                <th scope="col">操作</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="(item,index) in cartarr"  :key="item.id">
                <th scope="row">
                    <!-- 单选 -->
                    <input type="checkbox" v-model="item.check" @click="onecheck(index)"  />
                </th>
                <td>{{item.proname}}</td>
                <td>{{item.price}}</td>
                <td>
                    <button type="button" class="btn btn-primary" @click="plus(index)">+</button>
                      {{item.num}}
                    <button type="button" class="btn btn-primary" @click="reduce(index)">-</button>
                
                </td>
                <td>{{item.price*item.num}}</td>
                <td>
                    <button type="button" class="btn btn-primary" @click="del(item.id)">删除</button>
                </td>
              </tr>
              <tr>
                  <td colspan="6">总价:{{allsum}}</td>
              </tr>
             
            </tbody>
          </table>


    </div>
</body>
<script>

var app=new Vue({
    el:"#app",   
    data:{   
       cartarr:[
           {id:1,proname:"iphone11",price:5600,num:1,check:false},
           {id:2,proname:"vivo",price:7800,num:1,check:false},
           {id:3,proname:"华为",price:9400,num:1,check:false},
           {id:4,proname:"oppo",price:4500,num:1,check:false}
       ],
       allchecked:false
    //    allsum:0
    },
    computed:{
        allsum(){  //总价   依赖属性  this.cartarr
            var sum=0;
            this.cartarr.forEach(item=>{
                 if(item.check){
                    sum += item.price*item.num
                 }
             }) 
             return sum;
        }
    },
    methods:{
        allcheck(){   //全选
        //    1自身的值发生改变
              this.allchecked=!this.allchecked; 
            //   console.log(this.allchecked)
        //    2  让所有的产品全选或者不选
             this.cartarr.forEach(item=>{
                 item.check=this.allchecked;
             })
            //  3:总价
            //  this.cartarr.forEach(item=>{
            //      this.allsum+=item.price*item.num
            //  })
        },
        onecheck(ind){  //单选
            //  1自身的值发生改变
            this.cartarr[ind].check=!this.cartarr[ind].check;
            //    2判断其他所有的产品是否全部选中  只要有一个不选中就不全选就不选
                //  开闭原则
            //    var flag=true;
            //    this.cartarr.forEach(item=>{
            //        if(!item.check){
            //            flag=false;
            //        }
            //    })
            //    this.allchecked=flag;

            this.allchecked=this.cartarr.every(item=>{
                return item.check;
            })

            //    some   find   findIndex   filter
        },
        plus(ind){  //数量加
           this.cartarr[ind].num++;
        },
        reduce(ind){
            this.cartarr[ind].num--;
            if(this.cartarr[ind].num<=1){
                this.cartarr[ind].num=1;
            }
        },
        del(id){  //删除
            // this.cartarr.splice(ind,1);
            //  根据id去数组中找到该id对应的数据的下标 找到返回该条数据的下标 ,找不到返回-1
            var index=this.cartarr.findIndex(item=>{
                return item.id==id
            })
            // console.log(index)
            this.cartarr.splice(index,1)

            // var obj=this.cartarr.find(item=>{
            //     return item.id==id
            // })
            // console.log(obj)
        }
    }
})

</script>
</html>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值