vue实现购物车加减

 更新时间:2020年05月30日 16:30:15   作者:GaHoOo  
这篇文章主要为大家详细介绍了vue实现购物车加减,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了vue实现购物车加减的具体代码,供大家参考,具体内容如下

通常我们会在模板中绑定表达式,模板是用来描述视图结构的。如果模板中的表达式存在过多的逻辑,模板会变得臃肿不堪,维护变得非常困难。因此,为了简化逻辑,当某个属性的值依赖于其他属性的值时,我们可以使用计算属性。

那么什么是计算属性呢?

计算属性就是当其依赖属性的值发生变化是,这个属性的值会自动更新,与之相关的DOM部份也会同步自动更新。
实现的效果图如下:

我是使用了bootstrap跟Vue去完成这个效果的。

首先导入包:

<link rel="stylesheet" href="css/bootstrap.css" />
<script type="text/javascript" src="js/vue.js" ></script>

接着是布局样式:

<div id="app">
 <div class="container">
 <table class="table table-bordered table-hover"> 
 <tr>
 <td>
  <input type="checkbox" v-model="checkAll" @click="selectAll" />
 </td>
 <td>
 商品名称
 </td>
 <td>
 商品价格
 </td>
 <td>
 商品数量
 </td>
 <td>
 商品总额
 </td>
 <td>
 操作
 </td>
 </tr>
  <tr v-for="(item,index) in listInfo">
  <td>
  <input type="checkbox" :value="item.id" v-model="checkItem" @change="selectOne" />
  </td>
 <td>{{item.shopName}}</td>
 <td>{{item.shopPrice}}</td>
 <td>
 <button class="btn btn-default" @click="reduce(index)">-</button>
 <input type="text" v-model="item.shopCount" />
 <button class="btn btn-default" @click="add(index)">+</button>
 </td>
 <td>
 {{item.shopPrice*item.shopCount}}
 </td>
 <td>
 <button class="btn btn-warning" @click="del(index)">删除</button>
 </td>
 </tr> 
 </table>
 <p class="text-right">
 金额总计:{{sum}}
 </p>
 <p class="text-right">
 商品数量:{{count}}
 </p>
 <hr />
 <form>
 <div class="form-group">
 <input class="form-control" placeholder="商品名称" v-model="shopName" />
 </div>
 <div class="form-group">
 <input class="form-control" placeholder="商品价格" v-model = "shopPrice"/>
 </div>
 <div class="form-group">
 <button class="btn btn-primary" type="button" @click="addInfo">增加</button>
 </div>
 </form>
 </div>
</div>

最后是方法:

<script>
 new Vue({
 el:"#app",
 data:{
 listInfo:[
 {id:1,shopName:"男装1",shopPrice:1000,shopCount:0},
 {id:2,shopName:"男装2",shopPrice:2000,shopCount:0},
 {id:3,shopName:"男装3",shopPrice:3000,shopCount:0},
 {id:4,shopName:"男装4",shopPrice:4000,shopCount:0},
 {id:5,shopName:"男装5",shopPrice:5000,shopCount:0},
 ],
 shopName:"",
 shopPrice:"",
 checkItem:[],
 checkAll:false
 },
 methods:{
 add:function(index){
 this.listInfo[index].shopCount++
 
 },
 reduce:function(index){
 if(this.listInfo[index].shopCount<=0){
 this.listInfo[index].shopCount = 0
 }else {
 this.listInfo[index].shopCount--
 }
 
 },
 del:function(index){
  this.listInfo.splice(index,1)
 },
 addInfo:function(){
// alert(1)
 
 var obj = {
 id:this.listInfo.length+1,
 shopName:this.shopName,
 shopPrice:this.shopPrice,
 shopCount:0
 }
 console.log(obj)
 this.listInfo.push(obj) 
 },
 
 selectAll:function(){
 this.checkItem = []
 if(!this.checkAll){
 for (var i=0;i<this.listInfo.length;i++) {
 this.checkItem.push(this.listInfo[i].id)
 }
 }else {
 this.checkItem = []
 this.checkAll = false
 }
 },
 selectOne(){
 console.log(this.checkItem)
 if(this.checkItem.length == this.listInfo.length){
 this.checkAll = true
 }else {
 this.checkAll = false
 }
 }
 },
 computed:{
 sum(){
 var total = 0
 for (var i=0;i<this.listInfo.length;i++) {
 total+=parseFloat(this.listInfo[i].shopPrice)*parseFloat(this.listInfo[i].shopCount)
 }
 return total 
 },
 count:function(){
 var total = 0
 for (var i=0;i<this.listInfo.length;i++) {
 total+=parseInt(this.listInfo[i].shopCount)
 }
 return total
 }
 }
 })
</script>

通过以上的代码即可实现简单的购物车加减与增加删除!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 示例解析Ant Design Vue组件slots作用

    示例解析Ant Design Vue组件slots作用

    这篇文章主要为大家通过示例解析Ant Design Vue组件slots作用,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05
  • vue3中$attrs的变化与inheritAttrs的使用详解

    vue3中$attrs的变化与inheritAttrs的使用详解

    $attrs现在包括class和style属性。 也就是说在vue3中$listeners不存在了,vue2中$listeners是单独存在的,在vue3 $attrs包括class和style属性, vue2中 $attrs 不包含class和style属性,这篇文章主要介绍了vue3中$attrs的变化与inheritAttrs的使用 ,需要的朋友可以参考下
    2022-10-10
  • vue获取时间戳转换为日期格式代码实例

    vue获取时间戳转换为日期格式代码实例

    这篇文章主要介绍了vue获取时间戳转换为日期格式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04
  • Vue3 去除 vue warn 及生产环境去除console.log的方法

    Vue3 去除 vue warn 及生产环境去除console.log的方法

    这篇文章主要介绍了Vue3 去除 vue warn 及生产环境去除console.log的方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-06-06
  • vue的toast弹窗组件实例详解

    vue的toast弹窗组件实例详解

    本文通过实例给大家讲解了vue的toast弹窗组件功能,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友参考下吧
    2018-05-05
  • 使用 Element UI Table 的 slot-scope方法

    使用 Element UI Table 的 slot-scope方法

    这篇文章主要介绍了使用 Element UI Table 的 slot-scope方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10
  • Vue路由传参页面刷新后参数丢失原因和解决办法

    Vue路由传参页面刷新后参数丢失原因和解决办法

    这几天在开发中遇见的一个关于路由传参后,页面刷新数据丢失的问题,下面这篇文章主要给大家介绍了关于Vue路由传参页面刷新后参数丢失原因和解决办法,需要的朋友可以参考下
    2022-12-12
  • Vue精简版风格概述

    Vue精简版风格概述

    本篇文章给大家讲解了一下Vue精简版风格的相关知识点内容以及分享了实例代码,有兴趣的朋友参考下。
    2018-01-01
  • vue移动端弹起蒙层滑动禁止底部滑动操作

    vue移动端弹起蒙层滑动禁止底部滑动操作

    这篇文章主要介绍了vue移动端弹起蒙层滑动禁止底部滑动操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • vuex 中辅助函数mapGetters的基本用法详解

    vuex 中辅助函数mapGetters的基本用法详解

    mapGetters辅助函数仅仅是将 store 中的 getter 映射到局部计算属性,在组件或界面中不使用mapGetter调用映射vuex中的getter,在组件或界面中使用mapGetter调用映射vuex中的getter,具体内容跟随小编一起通过本文学习吧 
    2021-07-07

最新评论