vue 实现购物车

利用计算属性实现购物车功能,计算属性总是会依赖于自身使用的数组,在和购物车进行交互(全选、加减数量、删除)时总是应该对数组的元素进行操作,在对数组操作后,计算属性则会进行重新渲染视图,因而你只需要关心你的数据即可。

代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购物车</title>
    <style>
        [v-cloak]{
            display: none;
        }
        table{
            border: 1px solid #e9e9e9;
            border-collapse: collapse;
            border-spacing: 0;
            empty-cells: show;
        }
        th,td{
            padding: 8px 16px;
            border: 1px solid #e9e9e9;
            text-align: left;
        }
        th{
            background: #f7f7f7;
            color: #5c6b77;
            font-weight: 600;
            white-space: normal;
        }
    </style>
</head>
<body>
    <div id="app" v-cloak>
        <template v-if="goods.length">
        <table>
            <tr>
                <th>序号</th>
                <th>商品名称</th>
                <th>商品单价</th>
                <th>购买数量</th>
                <th>操作</th>
            </tr>
        
            <tr v-for='(goods,index) in goods'>
                <td>
                    <input type="checkbox" :checked="goods.is_true"  @click="handlerCancel(index,$event)" >
                    {{ index + 1 }}
                </td>
                <td>{{ goods.name }}</td>
                <td>{{ goods.price }}</td>
                <td>
                    <button @click='handlerReduce(index)'
                    :disabled="goods.count === 1"
                    >-</button>   
                    {{ goods.number }}
                    <button @click='handlerAdd(index)'>+</button>
                </td>
                <td>
                    <button type="button" @click="handlerRemove(index)">移除</button>
                </td>
            </tr>
        </table>
        全选<input type="checkbox" :checked="is_all" @click="handleAllSelect" >
        <span>商品总价:{{ total_val }}</span>
        </template>
    </div>
    <script src="https://unpkg.com/vue/dist/vue.min.js"></script>
    <script>
        var app = new Vue({
            el:'#app',
            data:{
                goods:[
                    {id:1,name:'apple',price:10.00,number:1,is_true:1}, //is_true 是否选中
                    {id:2,name:'banana',price:8.00,number:10,is_true:1},
                    {id:3,name:'orange',price:19.00,number:5,is_true:1}
                ]
            },
            computed: {
                total_val:function(){
                    var prices = 0;
                    for(var i = 0;i<this.goods.length;i++){
                        var item  = this.goods[i];
                        if(item.is_true){ //如果选中则增加到总价中
                            prices += item.price*item.number
                        }
                    }
                    return prices.toString().replace(/B(?=(d{3})+$)/g,',');   
                },
                is_all:function(){
                    return this.goods.every(function(val){return val.is_true}); //遍历返回数组中的是否选中的值
                }
            },
            mounted () {
            },
            methods: {
                handlerAdd:function(index){
                    this.goods[index].number++;
                },
                handlerReduce:function(index){
                    if(this.goods[index].number === 1) return;
                    this.goods[index].number--;
                },
                handlerRemove:function(index){
                    this.goods.splice(index,1);
                },
                handlerCancel:function(index,event){
                    this.goods[index].is_true = event.target.checked;
                },
                handleAllSelect:function(){
                    this.goods.map(function(item,index){ //map函数更改是否全选
                        item.is_true = event.target.checked; 
                    })
                }
            }
        })
    </script>
</body>
</html>
原文地址:https://www.cnblogs.com/ikai/p/13049525.html