Vue实现简易购物车功能

效果演示:

 代码展示:

<!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>Document</title>
    <script src="./js/vue.js"></script>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        li{
            list-style: none;
            display: flex;
            align-items: center;
            justify-content: space-between;
            width: 500px;
            height: 100px;
        }
        ul li input{
            width: 15px;
            text-align: center;
        }
        ul li img{
            width: 80px;
            height: 80px;
        }
    </style>
</head>
<body>
    <div id="app">
        <!-- 全选框 -->
        <input type="checkbox" v-model="checkedAll" @change="checkboxAll">
        <ul>
            <!-- 循环遍历数组goods -->
            <li v-for="(item,index) in goods">
                <input type="checkbox" v-model="checkedOther" :value="item.id" @change="checkboxOther">
                <img :src="item.pic" alt="">
                <span>
                <p>商品名:{{item.name}}</p>
                <p>商品价格:{{item.price}}</p>
                </span>
                <!-- :disabled ="item.num === 1" 设置禁用属性 最小值 -->
                <button @click="subtract(item)" :disabled ="item.num === 1" >-</button>
                <input type="text" v-model="item.num">
                <!-- :disabled = "item.limit === item.num" 最大值 -->
                <button @click="add(item)" :disabled = "item.limit === item.num">+</button>
                <p>小计:{{subtotal(item)}}</p>
                <button @click="del(index)">删除</button>
            </li>
        </ul>
        <h2>总计:{{total}} </h2>
    </div>
    <script>
        new Vue({
            el:'#app',
            data:{
                checkedAll:false,
                total:0,
                checkedOther:[],
                goods:[
                {
                        "id":0,
                        "name":"商品1",
                        "price":10,
                        "num":1,
                        "limit":5,            
                        "pic":"https://static.maizuo.com/pc/v5/usr/movie/44dc08914d508fc47c8267c6ca73f2d8.jpg"
                    },
                    {
                        "id":1,
                        "name":"商品2",
                        "price":20,
                        "num":2,
                        "limit":5,
                        "pic":"https://static.maizuo.com/pc/v5/usr/movie/44dc08914d508fc47c8267c6ca73f2d8.jpg"
                    },
                    {
                        "id":2,
                        "name":"商品3",
                        "price":30,
                        "num":3,
                        "limit":5,
                        "pic":"https://static.maizuo.com/pc/v5/usr/movie/44dc08914d508fc47c8267c6ca73f2d8.jpg"
                    }
                ]
                
            },
            methods:{
                // 全选单选函数
                checkboxAll(){
                    console.log(this.checkedAll);
                    // 如果this.checkedAll为true 则循环遍历goods数组中的id属性写入 this.checkedOther
                    // 全选
                    if(this.checkedAll){
                        this.goods.forEach(element => {
                            this.checkedOther.push(element.id)
                        });
                    }else{
                        // 如果this.checkedAll为false,则赋值空数组
                        // 全不选
                        this.checkedOther = [];
                    }
                    // 单选全选改变,重新计算总价
                    this.getTotal();
                },
                // 单选函数
                checkboxOther(){
                    //  当this.checkedOther数组的长度等于this.goods数组的长度
                    //  则说明全部选中 
                    if(this.checkedOther.length === this.goods.length){
                        this.checkedAll = true;
                    }else{
                        // 否则就是未选中
                        this.checkedAll = false;
                    }
                    // 单选全选改变,重新计算总价
                    this.getTotal();

                },
                // 购买数量增加 
                add(item){
                    item.num++
                    // 一旦数值改变,重新计算总价
                    this.getTotal();
                },
                // 购买数量减少
                subtract(item){
                    item.num--
                    // 一旦数值改变,重新计算总价
                    this.getTotal();
                },
                // 计算总价函数
                getTotal() {
                    this.total = 0;
                    // 循环遍历数组中选中的商品
                    this.goods.forEach(item => {
                        // 查询this.checkedOther 中是否有id属性,有说明被选中
                        // 执行this.total的加等操作
                        if (this.checkedOther.indexOf(item.id) != -1) {
                            this.total += item.price * item.num;
                        }
                    })
                },
                // 删除
                del(index){
                    if(confirm('确定删除这个商品吗?'))
                    this.goods.splice(index,1)
                }

            },
            // computed计算属性
            computed: {
                // 小计 这里可以用函数的形式去写
                // 但博主想联系一下计算属性传参
                // 因此用计算属性去写
                subtotal(){
                    return function(item){
                        return item.price*item.num 
                    }
                },
            }
        })
    </script>
</body>
</html>
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大聪明码农徐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值