Vue实现购物车

序号 商品名称 商品价格 购买数量 操作
{{ iphone.id }} {{ iphone.name }} {{ iphone.price }} {{ iphone.count }}
总价:¥ {{totalPrice()}}元

代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Vue购物车</title>

    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

    <style>
        #app {
            text-align: center;
            background-color: rgba(50, 99, 91, 0.65);
            color: white;
        }
        
        table {
            width: 100%;
        }
        
        th {
            width: 159px;
            height: 50px;
        }
        
        th,
        td {
            border: 1px solid #ddd;
        }
    </style>
</head>

<body>
    <div id="app">
        <table>
            <tr>
                <th>序号</th>
                <th>商品名称</th>
                <th>商品价格</th>
                <th>购买数量</th>
                <th>操作</th>
            </tr>
            <tr v-for="(iphone, index) in Ip_Json">
                <td style="text-align: center;">{{ iphone.id }}</td>
                <td>{{ iphone.name }}</td>
                <td>{{ iphone.price }}</td>

                <td>
                    <button v-bind:disabled="iphone.count === 0" v-on:click="sub(index)">-</button> {{ iphone.count }}
                    <button v-bind:disabled="iphone.count === 5" v-on:click="add(index)">+</button>
                </td>

                <td>
                    <button v-on:click="iphone.count=0">移除</button>
                </td>
            </tr>
        </table>
        总价:¥ {{totalPrice()}}元
    </div>
</body>

<script>
    var vm = new Vue({
        el: '#app',
        data: {
            a: 5,
            i: 0,
            flag: true,
            Ip_Json: [{
                id: 1,
                name: 'iphone 8',
                price: 1000,
                count: 1
            }, {
                id: 2,
                name: 'iphone xs',
                price: 1000,
                count: 1
            }, {
                id: 3,
                name: 'iphone xr',
                price: 1000,
                count: 1
            }]
        },
        methods: {
            totalPrice() {
                var totalP = 0;

                for (var i = 0, len = this.Ip_Json.length; i < len; i++) {
                    totalP += this.Ip_Json[i].price * this.Ip_Json[i].count;
                }
                return totalP;
            },
            add(val) {
                this.Ip_Json[val].count += 1;
                console.log(this.Ip_Json[val].count)
                if (this.Ip_Json[val].count === 5) {
                    alert("限购五件");
                }
            },
            sub(val) {
                this.Ip_Json[val].count -= 1;
                if (this.Ip_Json[val].count === 0) {
                    alert("商品数量不能小于零");
                }
            }
        },
    });
</script>

</html>

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×