蔬菜购物车案例

<!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>
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
    <style>
        body {
            background: url("image/huawenbg.jpg") repeat center;
        }
        
        .img-box {
            margin: 0 auto;
            text-align: center;
            margin-top: 50px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="img-box">
            <img src="./image/vettable.png" alt="蔬菜图片">
        </div>
        <div style="margin:0 30px;">
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th>选中</th>
                        <th>蔬菜</th>
                        <th>单价</th>
                        <th>个数</th>
                        <th>总计</th>
                        <th>删除</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="item,index in  cart" :key="index">
                        <td>
                            <input type="checkbox" v-model="item.selected" @change="selectAllSelect">
                        </td>
                        <td>
                            {{item.name}}
                        </td>
                        <td>
                            {{item.price}}
                        </td>
                        <td>
                            <input class="form-control" type="number" v-model.number="item.quantity" @change="calculateTotal(index)">
                        </td>
                        <td>
                            {{item.total}}
                        </td>
                        <td>
                            <button class="btn btn-danger btn-sm" @click="removeItem(index)">删除</button>
                        </td>
                    </tr>
                </tbody>

                <tfont>
                    <tr>
                        <td>
                            <input type="checkbox" @change="selectAllItems" v-model="selectAll">
                        </td>
                        <td colspan="3">
                            全选
                        </td>

                        <td>{{totalPrice}}</td>
                        <td>结算</td>
                    </tr>
                </tfont>

            </table>
        </div>
    </div>
    <script>
        new Vue({
            el: "#app",
            data() {
                return {
                    cart: [{
                            name: "西红柿",
                            price: 2.5,
                            quantity: 1,
                            total: 2.5,
                            selected: false
                        }, {
                            name: "黄瓜",
                            price: 1.8,
                            quantity: 6,
                            total: 10.8,
                            selected: false
                        }, {
                            name: "土豆",
                            price: 1.2,
                            quantity: 4,
                            total: 4.8,
                            selected: false
                        }, {
                            name: "白菜",
                            price: 2.2,
                            quantity: 3,
                            total: 6.6,
                            selected: false
                        },

                    ],
                    selectAll: false
                }
            },
            created() {
                const savedCart = JSON.parse(localStorage.getItem("cart"));
                if (savedCart) {
                    this.cart = savedCart;
                }
            },
            computed: {
                totalPrice() {
                    return this.cart.reduce((sum, item) => sum + (item.selected ? item.total : 0), 0).toFixed(2);
                }
            },
            watch: {
                cart: {
                    deep: true,
                    handler(newVal) {
                        localStorage.setItem('cart', JSON.stringify(newVal))
                    }
                }
            },
            methods: {
                calculateTotal(index) {
                    this.cart[index].total = (this.cart[index].quantity * this.cart[index].price).toFixed(2);
                },
                removeItem(index) {
                    this.cart.splice(index, 1);
                },
                selectAllItems() {
                    this.cart.forEach(item => {
                        item.selected = this.selectAll;
                    });
                },
                selectAllSelect() {
                    if (this.cart.reduce((sum, item) => sum + item.selected, 0) === this.cart.length) {

                    } else {
                        this.selected;
                    }
                    return this.cart.reduce((sum, item) => sum + item.selected, 0) === this.cart.length
                }
            }

        })
    </script>
</body>

</html>

在这里插入图片描述

蔬菜购物车案例_第1张图片

蔬菜购物车案例_第2张图片

你可能感兴趣的:(javascript,前端,vue.js)