Vue购物车展示功能

发布时间 2023-06-05 20:32:22作者: 抱紧小洪

1.基本购物车

<body>
<div id="app">
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
                <h1 class="text-center">购物车</h1>
                <table class="table table-bordered">
                    <thead>
                    <tr>
                        <th>商品id</th>
                        <th>商品名</th>
                        <th>商品价格</th>
                        <th>商品数量</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr v-for="good in good_list">
                        <th>{{good.id}}</th>
                        <td>{{good.name}}</td>
                        <td>{{good.price}}</td>
                        <td>{{good.number}}</td>
                        <td><input type="checkbox" :value="good" v-model="checkGroup"></td>
                    </tr>
                    </tbody>
                </table>
                <hr>
                选中了:{{checkGroup}}
                <h3>总价格:{{getPrice()}}</h3>
                <h3>选中了checkbox,checkGroup会发生变化,页面也在变,都会重新刷新页面。函数就会重新执行</h3>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    let vm = new Vue({
        el:'#app',
        data:{
            good_list: [
                {id: 1, name: '钢笔', price: 12, number: 2},
                {id: 2, name: '脸盆', price: 20, number: 20},
                {id: 3, name: '毛笔', price: 6, number: 9},
                {id: 4, name: '圆珠笔', price: 8, number: 5},
                {id: 5, name: '铅笔', price: 1, number: 3},
            ],
            checkGroup: [],
        },
        methods:{
            // 1 根据checkGroup选中的计算
            // 循环checkGroup 拿出价格*数量 累加 最后返回
            getPrice(){
                var total = 0
                for (item of this.checkGroup){
                    total += item.price * item.number
                }
                return total
            }
        }
    })
</script>

2.带全选 / 全不选功能购物车

<body>
<div id="app">
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
                <h1 class="text-center">购物车</h1>

                <table class="table table-bordered">
                    <thead>
                    <tr>
                        <th>商品ID</th>
                        <th>商品名</th>
                        <th>商品价格</th>
                        <th>商品数量</th>
                        <th>全选/全不选 <input type="checkbox" v-model="checkAll" @change="handleCheckAll"></th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr v-for="good in good_list">
                        <th scope="row">{{good.id}}</th>
                        <td>{{good.name}}</td>
                        <td>{{good.price}}</td>
                        <td>{{good.number}}</td>
                        <td><input type="checkbox" :value="good" v-model="checkGroup" @change="handlecCheckOne"></td>
                    </tr>
                    </tbody>
                </table>
                <hr>
                选中了:{{checkGroup}}
                <h3>总价格:{{getPrice()}}</h3>
                <h3>选中了checkbox,checkGroup会发生变化,页面也在变,都会重新刷新页面,函数就会重新执行</h3>
            </div>


        </div>

    </div>


</div>
</body>
<script>
    var vm = new Vue({
        el: '#app',
        data: {
            good_list: [
                {id: 1, name: '钢笔', price: 12, number: 2},
                {id: 2, name: '脸盆', price: 20, number: 20},
                {id: 3, name: '毛笔', price: 6, number: 9},
                {id: 4, name: '圆珠笔', price: 8, number: 5},
                {id: 5, name: '铅笔', price: 1, number: 3},
            ],
            checkGroup: [],
            checkAll:false,
        },
        methods: {
            getPrice() {
                // 1 根据checkGroup选中的,计算
                //  循环checkGroup,拿出价格*数量,累加,最后返回
                var total = 0
                for (item of this.checkGroup) {
                    total += item.number * item.price
                }
                return total
            },
            handleCheckAll(){
                if (this.checkAll){
                    this.checkGroup = this.good_list
                }else {
                    this.checkGroup = []
                }
            },
            handlecCheckOne(){
                if (this.checkGroup.length===this.good_list.length){
                    this.checkAll = true
                }else {
                    this.checkAll = false
                }
            },
        }
    })
</script>

3.购物车带加减商品功能

<body>
<div id="app">
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
                <h1 class="text-center">购物车</h1>

                <table class="table table-bordered">
                    <thead>
                    <tr>
                        <th>商品ID</th>
                        <th>商品名</th>
                        <th>商品价格</th>
                        <th>商品数量</th>
                        <th>全选/全不选 <input type="checkbox" v-model="checkAll" @change="handleCheckAll"></th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr v-for="good in good_list">
                        <th scope="row">{{good.id}}</th>
                        <td>{{good.name}}</td>
                        <td>{{good.price}}</td>
                        <td>
                            <button class="btn" @click="handleJian(good)">-</button>
                            {{good.number}}
                            <button class="btn" @click="handleJia(good)">+</button>
                        </td>
                        <td><input type="checkbox" :value="good" v-model="checkGroup" @change="handlecCheckOne"></td>
                    </tr>
                    </tbody>
                </table>
                <hr>
                选中了:{{checkGroup}}
                <h3>总价格:{{getPrice()}}</h3>
                <h3>选中了checkbox,checkGroup会发生变化,页面也在变,都会重新刷新页面,函数就会重新执行</h3>
            </div>


        </div>

    </div>


</div>
</body>
<script>
    var vm = new Vue({
        el: '#app',
        data: {
            good_list: [
                {id: 1, name: '钢笔', price: 12, number: 1},
                {id: 2, name: '脸盆', price: 20, number: 1},
                {id: 3, name: '毛笔', price: 6, number: 1},
                {id: 4, name: '圆珠笔', price: 8, number: 1},
                {id: 5, name: '铅笔', price: 1, number: 1},
            ],
            checkGroup: [],
            checkAll: false,
        },
        methods: {
            getPrice() {
                // 1 根据checkGroup选中的,计算
                //  循环checkGroup,拿出价格*数量,累加,最后返回
                var total = 0
                for (item of this.checkGroup) {
                    total += item.number * item.price
                }
                return total
            },
            handleCheckAll() {
                if (this.checkAll) {
                    this.checkGroup = this.good_list
                } else {
                    this.checkGroup = []
                }
            },
            handlecCheckOne() {
                if (this.checkGroup.length === this.good_list.length) {
                    this.checkAll = true
                } else {
                    this.checkAll = false
                }
            },
            handleJian(good) {
                if (good.number > 1) {
                    good.number--
                } else {
                    alert('不能再删了')
                }
            },
            handleJia(good) {
                good.number++
            },
        }
    })
</script>