vue实现购物车全选 批量删除价格计算等功能

10 篇文章 0 订阅
7 篇文章 0 订阅
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0
        }

        table {
            width: 1000px;
            border-collapse: collapse;
            text-align: center;
        }

        tr {
            height: 140px;
        }

        .title {
            height: 70px;
            border: 1px solid #333
        }

        .good-desc {
            display: flex;
        }

        ul,
        li {
            list-style: none;
            text-align: left;
            font-size: 14px;
        }

        img {
            width: 100px;
            height: 100px;
        }

        .num input {
            width: 30px;
            height: 30px;
            text-align: center;
            border: 0
        }

        .num button {
            width: 25px;
        }

        .footer {
            width: 1000px;
            display: flex;
            justify-content: space-between;
        }

        .left span {
            color: #aaa;
            font-size: 14px;
            margin-left: 30px
        }

        .right {
            color: #333;
        }

        .right b {
            color: red
        }

        .right button {
            border: 0;
            width: 60px;
            height: 40px;
            background: orangered;
            color: #fff
        }
    </style>
</head>

<body>
    <div id="app">
        <h2>购物清单</h2>
        <table>
            <tr class="title">
                <th><input type="checkbox" v-model='statusAll' @change='chooseAll'>全选</th>
                <th v-for='item in title'>{{item}}</th>
            </tr>
            <tr v-for='item,index in datalist'>
                <td><input type="checkbox" @change='chooseHandle()' v-model='item.status'></td>
                <td class="good-desc">
                    <img :src="item.img" alt="">
                    <ul>
                        <li>{{item.title}}</li>
                        <li>品牌:{{item.brand}}</li>
                        <li>产地:{{item.place}}</li>
                        <li>规格:{{item.norms}}</li>
                        <li>起定量:{{item.startnum}}</li>
                        <li>配送仓库:{{item.store}}</li>
                    </ul>
                </td>
                <td class="num">
                    <button @click='changeNum("-",item)'>-</button>
                    <input type="text" v-model='item.num'>
                    <button @click='changeNum("+",item)'>+</button>
                </td>
                <td>{{item.price}}</td>
                <td>{{item.total}}</td>
                <td>
                    <button @click='delHandle(index)'>删除</button>
                </td>
            </tr>
        </table>
        <div class="footer">
            <div class="left">
                <span @click='delAll'>删除所选商品</span>
                <span>继续购物</span>
            </div>
            <div class="right">
                <span><b>{{num}}</b>件商品总计(不含运费):<b>{{priceAll}}</b></span>
                <button>去结算</button>
            </div>
        </div>
    </div>
    <script>
        let vm = new Vue({
            el: "#app",
            data: {
                title: ['商品', '数量', '单价', '金额(元)', '操作'],
                datalist: [
                    {title: "11111",brand: "哈哈",place: "中国",norms: "大",startnum: "216千克",store: "上海沧海仓库",num: 4,price: 200,total: 800,img: "./goods.png",status: false},
                    {title: "22222",brand: "哈哈",place: "中国",norms: "大",startnum: "216千克",store: "上海沧海仓库",num: 4,price: 200,total: 800,img: "./goods.png",status: false},
                    {title: "33333",brand: "哈哈",place: "中国",norms: "大",startnum: "216千克",store: "上海沧海仓库",num: 4,price: 200,total: 800,img: "./goods.png",status: false}
                ],
                statusAll: false,
                indexList: []
            },
            computed: {
                //所选商品总价
                priceAll() {
                    var totalprice = 0;
                    this.datalist.forEach((item) => {
                        if (item.status === true) {
                            totalprice += item.total
                        }
                    })
                    return totalprice
                },
                //所选商品总数
                num() {
                    var list = this.datalist.filter((item, index, arr) => {
                        return item.status === true
                    })
                    return list.length
                }
            },
            methods: {
                //单选
                chooseHandle() {
                    this.statusAll = this.datalist.every(item => {
                        return item.status === true
                    })
                },
                //全选
                chooseAll() {
                    console.log(this.statusAll)
                    this.datalist.forEach((item) => {
                        item.status = this.statusAll
                    })
                },
                //改变数值
                changeNum(type, item) {
                    if (type === '+') {
                        item.num++
                    } else {
                        item.num--
                    }
                    if(item.num<=0){
                        item.num=0
                    }
                    item.total = item.num * item.price
                    this.chooseHandle()
                },
                //删除单个
                delHandle(index) {
                    this.datalist.splice(index, 1)
                },
                //删除所选
                delAll() {
                    this.datalist = this.datalist.filter((item) => {
                        return item.status !== true
                    })
                }

            }
        })
    </script>
</body>

</html>

在这里插入图片描述

  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
当然可以!以下是一个带有全选计算商品价格功能Vue购物车示例: HTML部分: ```html <div id="app"> <h2>购物车</h2> <label> <input type="checkbox" v-model="selectAll" @change="toggleSelectAll">全选 </label> <ul> <li v-for="item in cartItems" :key="item.id"> <label> <input type="checkbox" v-model="item.selected" @change="updateTotalPrice"> {{ item.name }} - {{ item.price }} </label> <button @click="removeFromCart(item)">删除</button> </li> </ul> <h3>总价: {{ totalPrice }}</h3> <h2>商品列表</h2> <ul> <li v-for="item in products" :key="item.id"> <label> <input type="checkbox" v-model="item.selected" @change="updateTotalPrice"> {{ item.name }} - {{ item.price }} </label> <button @click="addToCart(item)">加入购物车</button> </li> </ul> </div> ``` JavaScript部分: ```javascript new Vue({ el: '#app', data: { cartItems: [], products: [ { id: 1, name: '商品1', price: 10, selected: false }, { id: 2, name: '商品2', price: 20, selected: false }, { id: 3, name: '商品3', price: 30, selected: false }, ], selectAll: false, totalPrice: 0 }, methods: { addToCart(item) { item.selected = true; this.cartItems.push(item); this.updateTotalPrice(); }, removeFromCart(item) { const index = this.cartItems.indexOf(item); if (index > -1) { this.cartItems.splice(index, 1); item.selected = false; this.updateTotalPrice(); } }, toggleSelectAll() { for (let item of this.products) { item.selected = this.selectAll; } for (let item of this.cartItems) { item.selected = this.selectAll; } this.updateTotalPrice(); }, updateTotalPrice() { this.totalPrice = 0; for (let item of this.cartItems) { if (item.selected) { this.totalPrice += item.price; } } } } }); ``` 在上述示例中,我们添加了一个`selectAll`属性来表示是否全选。当点击全选复选框时,我们通过`toggleSelectAll`方法来更新所有商品的选中状态,并重新计算总价。 通过`updateTotalPrice`方法,我们遍历购物车中的商品,如果商品被选中,就将其价格累加到`totalPrice`属性中。 希望这个示例对你有所帮助!如有任何疑问,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胡肖一

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值