vue2.x实现购物车

看到网上vue实现购物车一个demo,但是发现问题挺多的,一些浏览器兼容性也不好,还有一些写法过于繁琐,所以改写了该demo重新发布,废话不多说,直接贴代码。

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="css/bootstrap.min.css">
        <script src="js/vue.min.js"></script>
        <title></title>
    </head>

    <body>
        <div class="container" id="app">
            <h2 class="page-header">购物车</h2>
            <table class="table">
                <tr>
                    <th><label><input type="checkbox" @click="check_all" :checked="check_goods.length == cart_list.length"/>全选</label></th>
                    <th>商品名称</th>
                    <th>商品价格</th>
                    <th>数量</th>
                    <th>操作</th>
                </tr>
                <tr v-for="(cart,index) in cart_list" :key="index">
                    <td><input type="checkbox" :value="cart" v-model="check_goods" /></td>
                    <td>{{cart.goods_name}}</td>
                    <td>{{cart.goods_price}}</td>
                    <td class="col-xs-3">
                        <input :disabled="cart.num <= 1" type="button" class="btn btn-default" value="-" @click="cart.num -= 1"/>                        <!--<input class="btn-group" type="number" v-model="cart.num" readonly="readonly"  />-->
                        {{cart.num}}
                        <input  type="button" class="btn btn-default" value="+" @click="cart.num += 1"/>
                    </td>
                    <td>
                        <button class="btn btn-danger" @click="delete_num">删除</button>
                    </td>
                </tr>
                <tr v-show="cart_list.length==0">
                    <td colspan="6" class="text-center text-muted">
                        <p>暂无数据....</p>
                    </td>
                </tr>
            </table>
            <div>
                总计:共 {{cart_list.length }}件商品,已选择 {{total_num}} 件
                <div class="pull-right">
                    合计:{{total_price}}元
                    <input type="button" class="btn btn-success" @click="payment()" :disabled="check_goods.length <= 0" value="去结算">
                </div>
            </div>
        </div>

        <script>
            var vm = new Vue({
                el: "#app",
                data: {
                    cart_list: [{  
                        goods_name: '小米6',
                          goods_price: '1600',
                          num: 2,
                    }, {
                        goods_name: '红米3',
                        goods_price: '700',
                        num: 1,
                    }, {
                        goods_name: '小米8',
                        goods_price: '2800',
                        num: 1,
                    }],
                    check_goods: [] //已选择的商品
                },
                methods: {
                    // 商品类减减
                    reduce: function(cart) {
                        if(cart.num <= 0) {
                            cart.num = 0
                        } else {
                            cart.num--
                        }
                    },
                    // 商品累加
                    add_num: function(cart) {
                        cart.num++
                    },
                    // 删除商品
                    delete_num: function(cart) {
                        this.check_goods.splice(this.check_goods.indexOf(cart), 1)
                        this.cart_list.splice(this.cart_list.indexOf(cart), 1)
                    },
                    //全选
                    check_all: function() {
                        if(vm.check_goods.length > 0) {
                            vm.check_goods = []
                        } else { 
                            /*this.cart_list.forEach((item,index) => {  
                                this.check_goods.push(item)
                            })*/
                            vm.cart_list.forEach(function(item,index){
                                vm.check_goods.push(item)
                            })
                        }
                    },
                    payment: function(){
                        alert("付款")
                    }
                },
                computed: {
                    // 总价
                    total_price: function() {
                        //let price = 0      
                        var price = 0;               
                        /*this.check_goods.forEach(item => {
                            // 总价 = 价格 * 数量
                            price += Number(item.goods_price) * Number(item.num)
                        })*/
                        this.check_goods.forEach(function(item){
                            price += Number(item.goods_price) * Number(item.num)
                        })
                        return price
                    },
                    // 数量
                    total_num: function() {
                        var t_num = 0;
                        this.check_goods.forEach(function(item){
                            t_num += Number(item.num);
                        })
                        return t_num
                    }
                }
            })
        </script>
    </body>

</html>

 效果如图 

 

转载于:https://www.cnblogs.com/yangqishen/p/10167884.html

Vue.js可以很方便地实现购物车功能,以下是一个简单的购物车示例: 1. 首先,创建一个Vue实例,并在data中定义购物车的数据: ``` new Vue({ el: '#app', data: { cart: [], // 购物车数组 products: [ // 商品列表 { name: '商品1', price: 10 }, { name: '商品2', price: 20 }, { name: '商品3', price: 30 }, { name: '商品4', price: 40 }, ] }, methods: { addToCart: function(product) { // 添加商品到购物车 var found = false; for (var i = 0; i < this.cart.length; i++) { if (this.cart[i].product.name === product.name) { this.cart[i].quantity++; found = true; } } if (!found) { this.cart.push({ product: product, quantity: 1 }); } }, removeFromCart: function(item) { // 从购物车中移除商品 var index = this.cart.indexOf(item); if (index !== -1) { this.cart.splice(index, 1); } }, getTotalPrice: function() { // 计算购物车总价 var total = 0; for (var i = 0; i < this.cart.length; i++) { total += this.cart[i].product.price * this.cart[i].quantity; } return total; } } }); ``` 2. 在页面中使用Vue指令绑定数据和方法: ``` <div id="app"> <h1>购物车</h1> <div> <h2>商品列表</h2> <ul> <li v-for="product in products"> {{ product.name }} - {{ product.price }}元 <button v-on:click="addToCart(product)">添加到购物车</button> </li> </ul> </div> <div> <h2>购物车</h2> <ul> <li v-for="item in cart"> {{ item.product.name }} - {{ item.product.price }}元 x {{ item.quantity }} <button v-on:click="removeFromCart(item)">移除</button> </li> </ul> <p>总价:{{ getTotalPrice() }}元</p> </div> </div> ``` 3. 运行代码,即可看到购物车功能的实现。 以上示例只是一个简单的购物车实现,实际应用中还需要考虑更多的因素,如商品库存、商品属性选择等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值