vue實現可改變購物數量的購物車

本文實例為大傢分享瞭vue實現改變購物數量的購物車,供大傢參考,具體內容如下

效果圖:

知識點:

1.computed 計算屬性
2.filters 過濾器

實現代碼:

<!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>Document</title>
</head>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<style>
    table {
        border-collapse: collapse;
        border-spacing: 0;
        border: 1px solid #ccc;
    }
    
    td,
    th {
        padding: 8px 16px;
        border: 1px solid #ccc;
        text-align: left;
    }
    
    th {
        background-color: #f7f7f7;
        color: #5c6b77;
    }
</style>

<body>
    <div id="box">
        <div v-if="books.length">
            <table>
                <thead>
                    <tr>
                        <th></th>
                        <th>書籍名字</th>
                        <th>出版日期</th>
                        <th>價格</th>
                        <th>購買數量</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(item,index) in books">
                        <td>{{item.id}}</td>
                        <td>{{item.name}}</td>
                        <td>{{item.date}}</td>
                        <td>{{item.price | toprice}}</td>
                        <td>
                            <button @click='down(index)' :disabled="item.aunt<=1">-</button> {{item.aunt}}
                            <button @click='add(index)'>+</button>
                        </td>
                        <td>
                            <button @click="remove(index)">移除</button>
                        </td>
                    </tr>
                </tbody>
            </table>
            <h2>總價:{{getallprice | toprice}}</h2>
        </div>
        <h2 v-else>您沒有購物信息</h2>
    </div>
    <script>
        const vm = new Vue({
            el: "#box",
            data: {
                books: [{
                    id: 1,
                    name: "《vue.js實戰》",
                    date: "2010.2.4",
                    price: 82.00,
                    aunt: 1
                }, {
                    id: 2,
                    name: "《javascript實戰》",
                    date: "2010.2.4",
                    price: 108.00,
                    aunt: 1
                }, {
                    id: 3,
                    name: "《html+css實戰》",
                    date: "2010.2.4",
                    price: 42.50,
                    aunt: 1
                }, {
                    id: 4,
                    name: "《axios實戰》",
                    date: "2010.2.4",
                    price: 82.00,
                    aunt: 1
                }, {
                    id: 5,
                    name: "《jquery實戰》",
                    date: "2010.2.4",
                    price: 65.20,
                    aunt: 1
                }, ]
            },
            methods: {
                add(index) {
                    this.books[index].aunt++;
                },
                down(index) {
                    this.books[index].aunt--;
                },
                remove(index) {
                    this.books.splice(index, 1)
                },

            },
            computed: {
                getallprice() {
                    let all = 0;
                    for (let i = 0; i < this.books.length; i++) {
                        all += this.books[i].price * this.books[i].aunt
                    }
                    return all
                }
            },
            filters: {
                toprice(price) {
                    return '¥' + price.toFixed(2)
                },

            }
        })
    </script>
</body>

</html>

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: