vue 购物车案例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.js"></script>
    <link rel="stylesheet" href="bootstrap-3.4.1-dist/css/bootstrap.min.css">
    <script src="bootstrap-3.4.1-dist/js/bootstrap.min.js"></script>
     <script src="js/vue.js"></script>
</head>
<body>
<div id="app">

 <div class="container-fluid">
     <div class="col-md-6 col-md-offset-3">
         <table class="table table-hover">
      <thead>
        <tr>
          <th>id</th>
          <th>商品名</th>
          <th>价格</th>
          <th>数量</th>
            <!--绑定一个change事件,对应一个双向数据 checkall为false,事件内判断如果不为false 那么就全选-->
          <th>全选 <input type="checkbox" v-model="checkall" @change="change"></th>
            <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item,index) in goodlist">
          <th scope="row">{{item.id}}</th>
          <td>{{item.name}}</td>
          <td>{{item.price}}</td>
          <td><button @click="handleDown(item)">-</button>
              <input type="text" :value="item.number" >
              <button @click="handleup(item)">+</button></td>
            <!--把itme循环出来的对象放到value值中会把itme放到checkGroup数组内,绑定个change事件如果有一个被点了,那么全选取消-->
          <td><input type="checkbox" v-model="checkGroup" :value="item" @change="handlecheckone"></td>
            <td><button @click="clickdelete(item,index)">删除</button></td>
        </tr>
      </tbody>
    </table>
         <hr>
         <p>{{checkGroup}}--{{checkall}}</p>
         <p>总价格{{getprice()}}</p>
     </div>
 </div>

</div>
</body>
<script>
 var vm = new Vue({
     el:'#app',
     data:{
         goodlist:[
             {id:1,name:'可乐',price:'3',number:2,kc:10},
             {id:2,name:'雪碧',price:'3',number:1,kc:5},
             {id:3,name:'红牛',price:'6',number:4,kc:4},
         ],
         //定义一个用户选择的商品购物车用于计算价格
         checkGroup:[],
         checkall:false,

     },
     methods:{
         getprice(){
             var total = 0
             for (item of this.checkGroup){
                 total += item.price * item.number
             }
             return total
         },
         change(){
             // checkall如果为true说明勾选上了
             if(this.checkall){
                 // 选择的表 = 商品表 =全选
                 this.checkGroup = this.goodlist
             }else{
                 this.checkGroup = []
             }
         },
         handlecheckone(){
             // 判断长度是否不一致,如不一致说明并没有全选
             if (this.checkGroup.length==this.goodlist.length){
                 this.checkall = true
             }else {
                 this.checkall = false
             }
         },
         handleDown(item){
            if(item.number>1){
                item.number--
            }else{
                alert('不能低于1')
            }
         },
         handleup(item){
             if(item.number >= item.kc){
                 alert('超出库存了')
             }else{
                 item.number++
             }
         },
         clickdelete(item,index){
            this.goodlist.splice(index,1)
             var text = this.checkGroup.indexOf(item)

             if (text>=0){
                 this.checkGroup.splice(text,1)
             }




         }


     }
 })
</script>
</html>
posted @ 2023-02-16 21:10  李阿鸡  阅读(23)  评论(0编辑  收藏  举报
Title