首页 > 其他分享 >【vue讲解:购物车案例】

【vue讲解:购物车案例】

时间:2024-08-12 16:26:18浏览次数:12  
标签:count vue name price checkGoodList 购物车 good 讲解 id

1 购物车案例

1.1 基本购物车

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="./js/vue.js"></script>
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css"
          integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
</head>
<body>
<div id="app">
    <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 goodList">
                    <th scope="row">{{good.id}}</th>
                    <td>{{good.name}}</td>
                    <td>{{good.price}}</td>
                    <td>{{good.count}}</td>
                    <td><input type="checkbox" v-model="checkGoodList" :value="good"></td>
                </tr>
                </tbody>
            </table>
            <hr>
            <h3>选中的商品:{{checkGoodList}}</h3>
            <h3> 总价格:{{getPrice()}}</h3>

        </div>
    </div>

</div>
</body>
<script>
    new Vue({
        el: '#app',
        data: {
            goodList: [
                {id: 1, name: '短袖', price: 99, count: 2},
                {id: 2, name: '短裤', price: 39, count: 1},
                {id: 3, name: '短裙', price: 189, count: 6},
                {id: 4, name: '短袜', price: 8, count: 8},
                {id: 5, name: '长筒袜', price: 4, count: 88},
                {id: 6, name: '过膝袜', price: 5, count: 90},

            ],
            checkGoodList: []
        },
        methods: {
            getPrice() {
                // 计算选中的总价格
                var total = 0
                // 循环选中的,计算总价格this.checkGoodList
                for (var item of this.checkGoodList) {
                    // console.log(item)
                    total += item.price * item.count
                }
                return total
            }
        }
    })
</script>
</html>

1.2 全选全不选

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="./js/vue.js"></script>
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css"
          integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
</head>
<body>
<div id="app">
    <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 goodList">
                    <th scope="row">{{good.id}}</th>
                    <td>{{good.name}}</td>
                    <td>{{good.price}}</td>
                    <td>
                        {{good.count}}

                    </td>
                    <td><input type="checkbox" v-model="checkGoodList" :value="good" @change="handleCheckOne"></td>
                </tr>
                </tbody>
            </table>
            <hr>
            <h3>选中的商品:{{checkGoodList}}</h3>
            <h3>是否全选:{{checkAll}}</h3>
            <h3> 总价格:{{getPrice()}}</h3>

        </div>
    </div>

</div>
</body>
<script>
    new Vue({
        el: '#app',
        data: {
            goodList: [
                {id: 1, name: '短袖', price: 99, count: 2},
                {id: 2, name: '短裤', price: 39, count: 1},
                {id: 3, name: '短裙', price: 189, count: 6},
                {id: 4, name: '短袜', price: 8, count: 8},
                {id: 5, name: '长筒袜', price: 4, count: 88},
                {id: 6, name: '过膝袜', price: 5, count: 90},

            ],
            checkGoodList: [],
            checkAll: false
        },
        methods: {
            getPrice() {
                // 计算选中的总价格
                var total = 0
                // 循环选中的,计算总价格this.checkGoodList
                for (var item of this.checkGoodList) {
                    // console.log(item)
                    total += item.price * item.count
                }
                return total
            },
            handleCheckAll() {
                if (this.checkAll) {
                    //全选了
                    this.checkGoodList = this.goodList
                } else {
                    //全不选
                    this.checkGoodList = []
                }
            },
            handleCheckOne() {
                // 如果checkGoodList的长度等于商品总长度,说明全选了,让checkAll =true
                if (this.checkGoodList.length == this.goodList.length) {
                    this.checkAll = true
                } else {
                    // 只要长度不相等,都是false
                    this.checkAll = false
                }
            },

        }
    })
</script>
</html>

1.3 带加减

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="./js/vue.js"></script>
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css"
          integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
</head>
<body>
<div id="app">
    <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 goodList">
                    <th scope="row">{{good.id}}</th>
                    <td>{{good.name}}</td>
                    <td>{{good.price}}</td>
                    <td><span class="btn" @click="handleJian(good)">-</span>
                        <input type="text" v-model="good.count">
                        <span class="btn"@click="handleAdd(good)">+</span>
                    </td>
                    <td><input type="checkbox" v-model="checkGoodList" :value="good" @change="handleCheckOne"></td>
                </tr>
                </tbody>
            </table>
            <hr>
            <h3>选中的商品:{{checkGoodList}}</h3>
            <h3>是否全选:{{checkAll}}</h3>
            <h3> 总价格:{{getPrice()}}</h3>

        </div>
    </div>

</div>
</body>
<script>
    new Vue({
        el: '#app',
        data: {
            goodList: [
                {id: 1, name: '短袖', price: 99, count: 2},
                {id: 2, name: '短裤', price: 39, count: 1},
                {id: 3, name: '短裙', price: 189, count: 6},
                {id: 4, name: '短袜', price: 8, count: 8},
                {id: 5, name: '长筒袜', price: 4, count: 88},
                {id: 6, name: '过膝袜', price: 5, count: 90},

            ],
            checkGoodList: [],
            checkAll: false
        },
        methods: {
            getPrice() {
                // 计算选中的总价格
                var total = 0
                // 循环选中的,计算总价格this.checkGoodList
                for (var item of this.checkGoodList) {
                    // console.log(item)
                    total += item.price * item.count
                }
                return total
            },
            handleCheckAll() {
                if (this.checkAll) {
                    //全选了
                    this.checkGoodList = this.goodList
                } else {
                    //全不选
                    this.checkGoodList = []
                }
            },
            handleCheckOne() {
                // 如果checkGoodList的长度等于商品总长度,说明全选了,让checkAll =true
                if (this.checkGoodList.length == this.goodList.length) {
                    this.checkAll = true
                } else {
                    // 只要长度不相等,都是false
                    this.checkAll = false
                }
            },
            handleAdd(good) {
                good.count++
            },
            handleJian(good) {
                if (good.count > 1) {
                    good.count--
                } else {
                    alert('不能再少了,受不了了')
                }

            }
        }
    })
</script>
</html>

标签:count,vue,name,price,checkGoodList,购物车,good,讲解,id
From: https://blog.csdn.net/weixin_50556117/article/details/141135198

相关文章

  • SSM基于+Vue的学生实践管理平台开发vcjxj 试题
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表系统内容:班级,学生,教师,企业,竞赛学科,竞赛信息,竞赛报名,竞赛成绩,课题信息,选题信息,开题报告,论文提交,论文成绩,岗位分类,行业领域,招聘信息,应聘申请,学生......
  • Vue3+vite+axios+.net api 配置
    exportdefaultdefineConfig({plugins:[vue()],resolve:{alias:{"@":fileURLToPath(newURL("./src",import.meta.url)),},},server:{host:"0.0.0.0",open:true,//启动项目自动弹出浏览器port:&qu......
  • vue3面试题
    1.Vue3.0里为什么要用ProxyAPI替代definePropertyAPI?——响应式优化(高频,重点!!!)Vue更新的重点。definePropertyAPI的局限性最大原因是它只能针对单例属性做监听。Vue2中的响应式实现正是基于defineProperty中的descriptor,对data中的属性做了遍历+递归,为每个属......
  • vue 多时间段范围选择及回显 组件封装
     <template><divclass="flex-col"><ul><liv-for="(item,index)inclassActiveList":key="index"><!--@click="toggleSelection(item.id)"-->......
  • Django项目与Vue的集成
    Django项目与Vue的集成在现代Web开发领域,前后端分离已成为一种主流趋势。Django,作为一个强大的PythonWeb框架,以其丰富的功能和高度的可扩展性而受到开发者的青睐。而Vue.js,作为一个轻量级的渐进式JavaScript框架,以其简洁的API和灵活的组件系统成为前端开发的热门选择。将Django......
  • 基于SpringBoot+MySQL+SSM+Vue.js的物业管理系统(附论文)
    获取见最下方名片信息获取见最下方名片信息获取见最下方名片信息演示视频基于SpringBoot+MySQL+SSM+Vue.js的物业管理系统(附论文)技术描述开发工具:Idea/Eclipse数据库:MySQLJar包仓库:Maven前端框架:Vue/ElementUI后端框架:Spring+SpringMVC+Mybatis+Sprin......
  • 基于SpringBoot+MySQL+SSM+Vue.js的大学生兼职系统(附论文)
    获取见最下方名片信息获取见最下方名片信息获取见最下方名片信息演示视频基于SpringBoot+MySQL+SSM+Vue.js的大学生兼职系统(附论文)技术描述开发工具:Idea/Eclipse数据库:MySQLJar包仓库:Maven前端框架:Vue/ElementUI后端框架:Spring+SpringMVC+Mybatis+Spr......
  • 基于SpringBoot+MySQL+SSM+Vue.js的线上教育培训办公系统(附论文)
    获取见最下方名片信息获取见最下方名片信息获取见最下方名片信息演示视频基于SpringBoot+MySQL+SSM+Vue.js的线上教育培训办公系统(附论文)技术描述开发工具:Idea/Eclipse数据库:MySQLJar包仓库:Maven前端框架:Vue/ElementUI后端框架:Spring+SpringMVC+Mybat......
  • Vue + ElementUI表格内实现图片点击放大效果的两种方式
    方式一:使用el-popover弹出框trigger属性用于设置何时触发Popover(弹出框)属性值有:hover、click、focus和manualstyle="cursor:pointer":当鼠标放上去时让img标签出现小手状态<el-table-columnlabel="物品图片"header-align="center"align="center"><templates......
  • 大模型备案过程讲解
    以下是一般情况下大模型备案的大致过程讲解(不同地区和具体要求可能存在差异和细化等情况):一、备案前准备了解法规和政策熟悉《生成式人工智能服务管理暂行办法》《互联网信息服务深度合成管理规定》等相关法律法规对大模型备案的要求和规定。明确备案的主管部门(通常是网信......