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>购物车</title>
</head>

<body>

    <div id="root">
        <div>
            商品名称:<input type="text" name="productName" v-model="Newproduce.productName"> 商品价格:
            <input type="text" name="productPrice" v-model="Newproduce.productPrice">
            <br>
            <button type="button" @click="addProduct">添加商品</button>
        </div>
        <ul>
            <li v-for="(item,index) in productList" :key="index">
                商品名称: {{item.productName}}---商品单价:{{item.productPrice}}
                <button @click="addToCart(index)">添加到购物车</button>
            </li>
        </ul>
        <Cart :cartlist="cartlist"></Cart>
    </div>
    <!-- 购物车组件 -->
    <template id="cart">
    
       <div>
        <table border="1">
           <tr>
            <td>选择<input type="checkbox" @click="allchange" v-model="changeCount"></td>
            <td>商品名称</td>
            <td>商品单价</td>
            <td>商品数量</td>
            <td>价格</td>
           </tr>
           <tr v-for="(item,index) in cartlist" :key="index">
               <td><input type="checkbox" v-model="item.active" ></td>
               <td>{{item.paoNam}}</td>
               <td>{{item.proPrice}}</td>
               <td><button type="button" @click="reduceCount(index)">-</button>{{item.proCount}}<button type="button" @click="addCount(index)">+</button></td>
               <td>{{item.proPrice*item.proCount}}</td>
           </tr>
           <tr>
               <td colspan="3">选中的商品:{{checkNum}}/{{this.cartlist.length}}</td>
               <td colspan="2">需付总价格:{{totalprice}}</td>
           </tr>
            </table>
       </div>
    </template>
</body>
<script src="../js/vue.js"></script>
<script>
    // type: Boolean,
    //  default: true
    // 购物车子组件
    var Cart = {
        template: '#cart',
        // 接受来自父组件的数据
        props: ['cartlist'],
        data() {
            return {
                changeCount: true
            }
        },
        methods: {
            reduceCount(index) {
                let pro = this.cartlist[index];
                if (pro.proCount > 1) {
                    pro.proCount--;
                } else {
                    this.cartlist.splice(index, 1);
                }
            },
            addCount(index) {
                this.cartlist[index].proCount++;
            },
            allchange() {

                // var change = this.cartlist.every(item => item.active)
                // if (!change) {
                //     changeCount = false
                // }
                //若标识为true,则让所有商品不全选
                if (this.isAllSelect) {
                    this.cartlist.forEach((item) => (item.active = false));
                } else {
                    //若标识为false,则让所有商品全选
                    this.cartlist.forEach((item) => (item.active = true));
                }
            },
        },
        computed: {
            //返回被选中的商品列表
            selectList() {
                return this.cartlist.filter((item) => item.active);
            },
            //动态判断是否全选
            isAllSelect() {
                return this.cartlist.length === this.selectList.length;
            },
            // 选中的商品数量
            checkNum() {
                // let checkProList = this.cartlist.filter(item => {
                //     return item.active;
                // })
                return this.selectList.length;
            },
            // 计算总金额
            totalprice() {
                let checkProList = this.cartlist.filter(item => {
                    return item.active;
                })
                let sum = 0;
                for (pro of checkProList) {
                    sum += pro.proPrice * pro.proCount;
                }
                return sum
            },

        },
        updated() {
            if (this.isAllSelect) this.changeCount = true;
            else this.changeCount = false;
        },
    }

    var root = new Vue({
        el: '#root',
        components: {
            Cart
        },
        data() {
            return {
                // 新增商品属性
                Newproduce: {
                    productName: '',
                    productPrice: 0,
                },
                // 商品列表
                productList: [{
                    productName: '小酥肉',
                    productPrice: 10
                }, {
                    productName: '炸鸡柳',
                    productPrice: 15
                }, {
                    productName: '酱烧鸡',
                    productPrice: 50
                }, ],
                cartlist: [{
                    active: true,
                    paoNam: '酸辣粉',
                    proPrice: 8,
                    proCount: 5,
                    proSum: 40
                }, {
                    active: true,
                    paoNam: '麻辣烫',
                    proPrice: 15,
                    proCount: 2,
                }, {
                    active: true,
                    paoNam: '臭豆腐',
                    proPrice: 10,
                    proCount: 3,
                }, ]
            }
        },
        methods: {
            // 添加商品
            addProduct() {
                if (this.Newproduce.productName == '') {
                    alert('名称不能为空');
                    return;
                } else if (this.Newproduce.productPrice < 0) {
                    alert('商品单价不合理');
                    return;
                }
                // 判断商品列表中是否已存在新的商品
                const pros = this.productList.filter(item => {
                    return item.productName == this.Newproduce.productName
                })
                if (pros.length > 0) {
                    alert('该商品已存在');
                    return
                } else {
                    // 添加商品,对于复杂数据烈性添加到数组的情况,注意要复制对象
                    console.log('添加商品成功');
                    this.productList.push({
                        productName: this.Newproduce.productName,
                        productPrice: this.Newproduce.productPrice
                    })
                    this.Newproduce.productName = '';
                    this.Newproduce.productPrice = '';
                }
            },
            //    添加购物车
            addToCart(index) {
                // addPro 要添加的商品数据
                let addPro = this.productList[index];
                // pro购物车中的商品
                let pro = this.cartlist.find(item => item.paoNam == addPro.productName)
                    // 购物车中已经存在此商品
                if (pro) pro.proCount++;
                else {
                    this.cartlist.push({
                        active: true,
                        paoNam: addPro.productName,
                        proPrice: addPro.productPrice,
                        proCount: 1,
                    })
                }
            },
            // 全选状态
            allchange() {
                // return this.list.every((item) => item.goods_state);
                return this.cartlist.every((item) => item.active)
            },
        },
        watch: {
            productList() {
                window.localStorage.setItem("productList", JSON.stringify(this.productList))
            }
        },
        created() {
            if (window.localStorage.getItem('productList')) {
                let proList = JSON.parse(window.localStorage.getItem("productList"));
                this.productList = proList;
            }


        },

    })
</script>

</html>

运行效果
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值