vue购物车-0920

注意:

1、浮点四舍五入问题。moneyCount.toFixed(2);

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>购物车-Vue</title>
    <script>

    </script>

</head>
<body>

  <div id="div1" style="border: 1px">

      <fieldset>
          <legend>{{userName}}的购物车</legend>

       <table border="1">
          <thead>
            <tr>
                <th>行号</th>
                <th>编码</th>
                <th>名称</th>
                <th>单价</th>
                <th>数量</th>
                <th>操作</th>
            </tr>

          </thead>

          <tbody>
            <tr v-for="(goods,index) in goodsWillPay">
                <td>{{index+1}}</td>
                <td>{{goods.goodsNo}}</td>
                <td>{{goods.goodsName}}</td>
                <td>{{goods.goodsPrice}}</td>
                <td><button @click="changeRowQuantity(index,'-')">-</button>
                    {{goods.goodsQuantity}}
                    <button @click="changeRowQuantity(index,'+')">+</button>
                </td>
                <td :class="'text-center'"><button @click="DeleteGoodsRow(index)">移除</button>
            </td>
              
            </tr>
          </tbody>
       </table>
       <label>合计金额:</label><span>{{countGoodsMoney()}}</span>

      </fieldset>

  </div>





</body>
<script src="js/vue.js"></script>
<script>
    var goodsList={
        userName:"“杨胜利”",
        goodsWillPay:[{goodsNo:"sp001",goodsName:"iPhone X",goodsPrice:7999.99,goodsQuantity:1},
                        {goodsNo:"sp008",goodsName:"荣耀 10", goodsPrice:2399.00,goodsQuantity:1},
                        {goodsNo:"sp009",goodsName:"华为 P20",goodsPrice:3399.00,goodsQuantity:1},
                        {goodsNo:"sp012",goodsName:"小米 8",  goodsPrice:3999.00,goodsQuantity:1}]
    };

    var v1=  new Vue({
        el:'#div1',
        data:goodsList,
        methods:{
            // 统计购物车中的商品金额之和
            countGoodsMoney:function(){
                let moneyCount=0;
                for (var i=0;i<this.goodsWillPay.length;i++){
                    console.log((i+1) +"行 moneyCount:"+ this.goodsWillPay[i].goodsPrice * this.goodsWillPay[i].goodsQuantity);
                    moneyCount=moneyCount + this.goodsWillPay[i].goodsPrice* this.goodsWillPay[i].goodsQuantity;
                    moneyCount.toFixed(2);
                }
                return  moneyCount.toFixed(2);
            },

            // 修改指定商品行的数量:加1或减1
            changeRowQuantity:function (index,addOrSubtraction) {
                console.log("index:"+ index);
                console.log("addOrSubtraction:"+ addOrSubtraction);
                if (addOrSubtraction=="-" ){
                    if (this.goodsWillPay[index].goodsQuantity>0){
                        this.goodsWillPay[index].goodsQuantity = this.goodsWillPay[index].goodsQuantity -1;
                    }
                }

                if (addOrSubtraction=="+" ){
                        this.goodsWillPay[index].goodsQuantity = this.goodsWillPay[index].goodsQuantity + 1;
                }
            },


            //从购物车,移除一行商品
            DeleteGoodsRow:function (index) {
                 this.goodsWillPay.splice(index,1);
            },

        }
    });



</script>


</html>

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值