91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發布時間:2022-03-24 11:11:25 來源:億速云 閱讀:241 作者:iii 欄目:web開發

本篇內容主要講解“vue怎么實現可改變購物數量的購物車”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“vue怎么實現可改變購物數量的購物車”吧!

效果圖:

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

知識點:

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

實現代碼:

<!DOCTYPE html>
<html>

<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>

到此,相信大家對“vue怎么實現可改變購物數量的購物車”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

vue
AI

吐鲁番市| 汾西县| 锡林浩特市| 新宁县| 株洲市| 万年县| 晋中市| 岐山县| 汤原县| 扶绥县| 宁武县| 莆田市| 个旧市| 富川| 镇赉县| 松江区| 麻城市| 江城| 阿鲁科尔沁旗| 五家渠市| 安平县| 顺平县| 绍兴市| 望奎县| 乌拉特中旗| 溧水县| 东丰县| 财经| 大邑县| 新津县| 驻马店市| 濮阳市| 介休市| 阿坝县| 广丰县| 城市| 福安市| 岳普湖县| 武陟县| 剑川县| 惠安县|