购物车计数实现

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购物车计数实现</title>

    <style>
        .box{
            width: 350px;
            margin: 50px auto;
            padding: 50px;
            border: 1px solid #f5f5f5;
        }
        ul{ 
            margin: 0;
            padding: 0;
        }
        ul li {
            list-style: none;
            padding: 0;
            margin: 0;
            margin-bottom: 20px;
        }
        .danjia{
            margin-left: 20px;
        }
        .info {
            margin-top: 30px; 
        }
        .info span{ 
            margin-right: 30px;
        }
        .info div{
            margin-top: 30px;
        }
        button{
            margin: 0 10px;
        }
    </style>
</head>
<body>
    <div id="app">
       <div class="box">
           <ul class="list">
               <li v-for="(item,index) in productList">
                  <button @click='handle($event,"minus",index)'>-</button>   
                   <em v-text="item.count"></em>
                   <button @click='handle($event,"plus",index)'>+</button>        
                   <span class="danjia">
                       单价: <strong v-text="item.price + '元'"></strong>
                       小计: <strong v-text="computedTotal(item.count,item.price)"></strong>
                   </span>
               </li>
           </ul>

           <div class="info">
               <span>商品共合计: <em v-html="total">0</em>件</span>
               <span>共花费了: <em v-html="totalPrice">0</em>元</span>
               <div>其中最贵的商品单价是: <em v-html="maxPrice">0</em>元</div>
           </div>
       </div>
    </div>
 
    <script src="../../node_modules/vue/dist/vue.js"></script> 
    <script> 
 
        let _DATA = [
            {
                id:1,
                count:0,
                price:12.5,
                total:0
            },
            {
                id:2,
                count:0,
                price:10.5,
                total:0
            },
            {
                id:3,
                count:0,
                price:8.5 
            },
            {
                id:4,
                count:0,
                price: 8 
            },
            {
                id: 5,
                count:0,
                price:14.5 
            }
        ]
        let vm = new Vue({
            el: "#app",
            data: { 
                productList: _DATA
            }, 
            methods:{
                computedTotal(count,price){
                    return count * price + '元'
                },
                handle(ev,type,activeIndex){
                    this.productList = this.productList.map((item,index)=>{
                        console.log(item)
                        if(activeIndex === index){
                          if(type === 'minus'){ 
                            item.count <= 0 ? item.count=0 : item.count--;
                          }else if(type === 'plus'){ 
                            item.count >= 10 ? item.count=10 : item.count++;
                          }
                        }
                        return item;
                    })
                }
            },
            computed:{
                // => 总数量
                total(){
                    // let total = 0;
                    // this.productList.forEach(item => {
                    //     total += item.count
                    // });
                    // return total;
                   return this.productList.reduce((accumulator,item)=>{
                        return accumulator +item.count;
                   },0);   // 第二个参数是 累加器的初始值
                },
                // => 总价格
                totalPrice(){
                    return this.productList.reduce((accumulator,item)=>{
                        return accumulator + (item.count * item.price);
                   },0);   // 第二个参数是 累加器的初始值
                },
                // => 最大单价
                maxPrice(){
                   let arr  = this.productList.filter(item=>{
                        return item.count >= 1
                    }).map(item=>{
                        return item.price
                    }) 

                    return  arr.length === 0 ? 0 : Math.max(...arr)
                }
            }
           
        });

        // let arr = [10,20,30,40]; 
        // let result = arr.reduce((accumulator,item)=>{
        //      return accumulator + item;
        // }) 
        // console.log(result)

    </script>
 
</body>
</html>
posted @ 2021-03-01 13:57  13522679763-任国强  阅读(98)  评论(0)    收藏  举报