首页 > 其他分享 >Vue购物车展示功能

Vue购物车展示功能

时间:2023-06-05 20:34:09浏览次数:37  
标签:good name 展示 price number 购物车 Vue checkGroup id

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>

标签:good,name,展示,price,number,购物车,Vue,checkGroup,id
From: https://www.cnblogs.com/XxMa/p/17458858.html

相关文章

  • Vue基础之表单控制 ,v-model进阶,箭头函数,JS循环
    目录一、表单控制1.checkbox选中2.radio单选3、checkbox多选4.购物车案例-结算二、v-model进阶三、箭头函数es6的语法1无参数,无返回值2有一个参数,没有返回值,可以省略括号3多个参数,不能省略括号4多个参数,不能省略括号,一个返回值5一个参数,一个返回值四、补充:JS循环一、表......
  • vue3+vant4+vuex4实现todolist备忘录案例
    案例图片如下:  1<van-cell-group>2<van-cell>3<van-row>4<van-colspan="20">5<van-field6:value="content"7@change="handl......
  • 初识vue3——第一天
    api查询地址api请查阅vue3全部API初始化实例每个Vue应用都是通过createApp函数创建一个新的应用实例://index.html<divid="app"></div><scripttype="module"src="/src/main.js"></script>//main.jsimport{createApp}from'vu......
  • Vue——表单控制、购物车案例、v-model进阶、与后端交互三种方式、箭头函数
    表单控制//1checkbox 单选 多选//2radio 单选<body><divid="app"><h1>checkbox单选</h1><p>用户名:<inputtype="text"v-model="username"></p><p>密码:<inputtype="p......
  • 表单控制,购物车案例,v-model进阶,与后端交互的三种方式
    1表单控制#1checkebox: -单选-多选#2radio -单选<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title><scriptsrc="./js/vue.js"></scrip......
  • Vue3 setup语法糖下的axios全局设置教程
    Vue3setup语法糖下的axios全局设置教程前言在Vue3的组件式API开发下,this关键词不再适用,网上很多配置axios教程都是以Vue2为基础的,在Vue3下不再适用。近期尝试用组件式API风格写项目,在配置全局axios就遇到了这个问题。经过我反复尝试,查阅官网的文档,终于有了以下解决方法:Vue2......
  • vue3学前准备
    vue3学前准备学习vue3的原因Vue3是一个面向未来的框架:Vue3相比Vue2有更好的性能和更好的扩展性,将来会成为前端开发的主流。改进的响应式系统:Vue3采用了Proxy对象来实现响应式系统,使得性能更高、内存消耗更小、支持动态添加属性等。同时,在Vue3中,开发者也可以更容易地......
  • 在DevExpress的GridView的列中,使用RepositoryItemSearchLookUpEdit控件实现产品列表信
    有时候,我们为了方便,我们往往使用扩展函数的代码方式创建很多GridView的操作功能,如在随笔《在DevExpress中使用BandedGridView表格实现多行表头的处理》中介绍过多行表头的创建及绑定处理,在《基于DevExpress的GridControl实现的一些界面处理功能》也介绍了一些特殊的展示效果,本篇随......
  • 完成第一个 Vue3.2 项目后,使用体会
    第一次CompositionAPI在vue3.2中,正式支持了scriptsetup的写法,这样可以大大简化组件的代码量,减少一些重复操作,我认为当你写vue3时,应该把这当作默认写法。在vue3.2之前,一般会这样写。<script>exportdefault{setup(props,ctx){consta=ref(0)//必须......
  • 完成第一个 Vue3.2 项目后,使用体会
    第一次CompositionAPI在vue3.2中,正式支持了scriptsetup的写法,这样可以大大简化组件的代码量,减少一些重复操作,我认为当你写vue3时,应该把这当作默认写法。在vue3.2之前,一般会这样写。<script>exportdefault{setup(props,ctx){consta=ref(0)//必须......