首页 > 其他分享 >vue 购物车案例

vue 购物车案例

时间:2023-02-16 21:23:08浏览次数:40  
标签:checkall vue item price number 购物车 案例 checkGroup id

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.js"></script>
    <link rel="stylesheet" href="bootstrap-3.4.1-dist/css/bootstrap.min.css">
    <script src="bootstrap-3.4.1-dist/js/bootstrap.min.js"></script>
     <script src="js/vue.js"></script>
</head>
<body>
<div id="app">

 <div class="container-fluid">
     <div class="col-md-6 col-md-offset-3">
         <table class="table table-hover">
      <thead>
        <tr>
          <th>id</th>
          <th>商品名</th>
          <th>价格</th>
          <th>数量</th>
            <!--绑定一个change事件,对应一个双向数据 checkall为false,事件内判断如果不为false 那么就全选-->
          <th>全选 <input type="checkbox" v-model="checkall" @change="change"></th>
            <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item,index) in goodlist">
          <th scope="row">{{item.id}}</th>
          <td>{{item.name}}</td>
          <td>{{item.price}}</td>
          <td><button @click="handleDown(item)">-</button>
              <input type="text" :value="item.number" >
              <button @click="handleup(item)">+</button></td>
            <!--把itme循环出来的对象放到value值中会把itme放到checkGroup数组内,绑定个change事件如果有一个被点了,那么全选取消-->
          <td><input type="checkbox" v-model="checkGroup" :value="item" @change="handlecheckone"></td>
            <td><button @click="clickdelete(item,index)">删除</button></td>
        </tr>
      </tbody>
    </table>
         <hr>
         <p>{{checkGroup}}--{{checkall}}</p>
         <p>总价格{{getprice()}}</p>
     </div>
 </div>

</div>
</body>
<script>
 var vm = new Vue({
     el:'#app',
     data:{
         goodlist:[
             {id:1,name:'可乐',price:'3',number:2,kc:10},
             {id:2,name:'雪碧',price:'3',number:1,kc:5},
             {id:3,name:'红牛',price:'6',number:4,kc:4},
         ],
         //定义一个用户选择的商品购物车用于计算价格
         checkGroup:[],
         checkall:false,

     },
     methods:{
         getprice(){
             var total = 0
             for (item of this.checkGroup){
                 total += item.price * item.number
             }
             return total
         },
         change(){
             // checkall如果为true说明勾选上了
             if(this.checkall){
                 // 选择的表 = 商品表 =全选
                 this.checkGroup = this.goodlist
             }else{
                 this.checkGroup = []
             }
         },
         handlecheckone(){
             // 判断长度是否不一致,如不一致说明并没有全选
             if (this.checkGroup.length==this.goodlist.length){
                 this.checkall = true
             }else {
                 this.checkall = false
             }
         },
         handleDown(item){
            if(item.number>1){
                item.number--
            }else{
                alert('不能低于1')
            }
         },
         handleup(item){
             if(item.number >= item.kc){
                 alert('超出库存了')
             }else{
                 item.number++
             }
         },
         clickdelete(item,index){
            this.goodlist.splice(index,1)
             var text = this.checkGroup.indexOf(item)

             if (text>=0){
                 this.checkGroup.splice(text,1)
             }




         }


     }
 })
</script>
</html>

标签:checkall,vue,item,price,number,购物车,案例,checkGroup,id
From: https://www.cnblogs.com/LiaJi/p/17128347.html

相关文章

  • Vue_侦听器watch
    方法格式监听器在Vue的watch节点下data:{ username:'Exungsh'}watch:{ username(newVal,oldVal){ console.log(newVal,oldVal) } //如果要侦听的是对象的子......
  • Vue急速入门-3
    内容概要购物车小练习<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title><scriptsrc="vue/vue.js"></script><......
  • Vue项目安装less和less-loader
    第一步:查看webpack和webpack-cli是否安装打开cmd,通过命令查看webpack-vwebpack-cli-v   如果没有安装,要先进行安装可以通过npmviewwebpackversion/npm......
  • vue3+vant中自定义隐藏DropdownMenu下拉菜单
    一、概述需求:当点击确定按钮时隐藏下拉菜单。主要使用的方法:ref标识当前组件van-dropdown-item,当点击确定按钮时通过getCurrentInstance来获取组件身上的属性方法。......
  • vue学习之-----组件递归调用
    1、关键点2、父组件<template><div><divclass="btn-title"><el-button@click="addRule(ruleDataList.id)">添加父节点</el-button>......
  • vue项目,使用query传参,页面显示重新刷新或回归后数据丢失
    1、将需要传输的数据使用  JSON.stringify()  转译成字符串形式进行传输  2、在需要接收的页面使用  JSON.parse()  将数据格式再转回来即可使用,且刷新回......
  • drf回顾,前端发展历史,vue介绍,第一个helloword,插值语法
    目录drf回顾,前端发展历史,vue介绍,第一个helloword,插值语法今日内容概要今日内容详细1drf回顾2前端发展历史3vue介绍4第一个helloworld5插值语法drf回顾,前端发展历史,vu......
  • vue3实现动态路由配置
    思路:1、登录成功后,返回动态的路由信息,存储在本地localStorage中。2、router.js中在路由守卫钩子函数中使用router.addRoute()进行动态路由添加。具体代码: login.vue......
  • vue js 预览word, Excel, TXT,PDF 图片等。
    npm安装插件npminstallxlsx--savenpminstalldocx-preview--save  npminstallaxios template部分//这里封装成一个组件<template><divclass=""......
  • Docker-应用部署案例
    1、Docker部署mysql拉取mysql镜像#查询mysql镜像dockersearchmysql#拉取镜像命令dockerpullcentos/mysql-57-centos7创建容器dockerrun-di--name=tes......