​​Vue实战小项目-购物车

Vue实战小项目-购物车

前言
在很多电商网站中,都有一个非常重要的功能,那就是购物车。接下来我们将使用Vue.js实现一个简易的购物车项目。实现的功能有全选或选择部分商品、计算总价、对商品数量进行增减、删除已添加至购物车的商品。

图1

步骤

首先新建一个html文件,进行引入Vue.js与html代码编写,效果图如上。

一、单个商品的价格计算

单个商品数量可以增减,但最少数量为1,而且数量的变化也会引起价格的变化。数量的变化通过点击+或-去调用add或reduce方法,+的时候数量加1,-的时候数量减1,并且在单个商品金额的地方调用计算单个商品总结的方法。

reduce: function (index) {  //减少商品

this.list[index].count --;

},

add: function (index) { //增加商品

this.list[index].count ++;

},

ofPrice: function (index) { //计算单个商品总价

  let ofPrice = 0;

ofPrice+=(this.list[index].price*this.list[index].count);

   return ofPrice.toString();

}

二 选择商品

在购物车里,你可以选择你想要结算的商品进行最后价格结算,商品总金额为已选择的商品的金额之和。需要绑定单选按钮的选中状态,选中为true,再次点击状态取反。

<td> // 单选商品

<input type="checkbox" :checked="item.check" @click="item.check = !item.check">

</td>

<th>全选<input id="all" @click="selAll" type="checkbox" checked></th>

selAll: function () {   //商品全选

let isAll = document.querySelector('#all');

if (isAll.checked == true) {

this.list.forEach(function(item, index) {

item.check = true;

})

} else {

this.list.forEach(function(item, index) {

item.check = false;

})

}

},

上面是商品的选择,还需要计算已选择商品的价格之和。

totalPrices: function () {  //计算总价

let totalPrices = 0;

this.list.forEach(function (val, index) {

if (val.check == true) //遍历商品,如果选中就进行计算。

totalPrices += parseFloat(val.price * val.count);

})

return totalPrices.toString();

},

三 删除商品

点击每个商品后的移除后就会将该商品从商品列表中删除

remove: function (index) {  //移除商品

    this.list.splice(index, 1);

},

四 完整代码

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>Title</title>

</head>

<body>

<div id="app" v-cloak>

    <template v-if="list.length">

        <table>

            <thead>

            <tr>

                <th>全选<input id="all" @click="selAll" type="checkbox" checked></th>

                <th>商品名称</th>

                <th>商品单价</th>

                <th>购买数量</th>

                <th>金额(元)</th>

                <th>操作</th>

            </tr>

            </thead>

            <tbody>

            <template v-for="(item, index) in list">

                <tr>

                    <td>

                        <input type="checkbox" :checked="item.check" @click="item.check = !item.check">

                    </td>

                    <td>

                        {{ item.name }}

                    </td>

                    <td>

                        {{ item.price }}

                    </td>

                    <td>

                        <button @click="reduce(index)" :disabled="item.count == 1">-</button>

                        {{ item.count }}

                        <button @click="add(index)">+</button>

                    </td>

                    <td>

                        {{ ofPrice(index) }}

                    </td>

                    <td>

                        <button @click="remove(index)">移除</button>

                    </td>

                </tr>

            </template>

            </tbody>

        </table>

        <div>总价: ¥ {{ totalPrices }}</div>

    </template>

    <template v-else>

        购物车没有商品

    </template>

</div>

<script src="../js/vue.js"></script>

<script>

    var app = new Vue({

        el: '#app',

        data: {

            list: [

                {

                    id: 1,

                    name: '矿泉水',

                    price: 2,

                    count: 1,

                    check: true,

                },

                {

                    id: 2,

                    name: '口香糖',

                    price: 2.5,

                    count: 1,

                    check: false,

                },

                {

                    id: 3,

                    name: '可乐',

                    price: 3,

                    count: 1,

                    check: true,

                },

            ]

        },

        methods: {

            remove: function (index) {  //移除商品

                this.list.splice(index, 1);

            },

            reduce: function (index) {  //减少商品

                this.list[index].count --;

            },

            add: function (index) { //增加商品

                this.list[index].count ++;

            },

            selAll: function () {   //商品全选

                let isAll = document.querySelector('#all');

                if (isAll.checked == true) {

                    this.list.forEach(function(item, index) {

                        item.check = true;

                    })

                } else {

                    this.list.forEach(function(item, index) {

                        item.check = false;

                    })

                }

            },

            ofPrice: function (index) { //计算单个商品总价

                let ofPrice = 0;

                ofPrice += (this.list[index].price * this.list[index].count);

                return ofPrice.toString();

            }

        },

        computed: {

            totalPrices: function () {  //计算总价

                let totalPrices = 0;

                this.list.forEach(function (val, index) {

                    if (val.check == true)

                        totalPrices += parseFloat(val.price * val.count);

                })

                return totalPrices.toString();

            },

        }

    })

</script>

</body>

</html>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值