vue实现购物车的小练习


Posted in Vue.js onDecember 21, 2020

今天从网上找了一个购物车的小例子,照着敲了一下,收获不少。下面的用一个小动图展示一下成果:

vue实现购物车的小练习

接下来上代码:

<!DOCTYPE html>
<html>
 <head>
 <meta charset="UTF-8">
 <link href="css/shoppingcart.css" rel="stylesheet" type="text/css" />
 <title></title>
 </head>
 <body>
 <div id="app">
 <h2>购物清单</h2>
 <div class="nav">
 <div><span class="noselected" v-bind:class="{selected:ifAllselect}" @click="allSelect(ifAllselect)"></span>全选</div>
 <div>商品</div>
 <div>数量</div>
 <div>单价(元)</div>
 <div>金额(元)</div>
 <div>操作</div>
 </div>
 <table class="goods">
 <tbody>
 <tr v-for="(item,index) in goods">
 <td><span class="noselected" v-bind:class="{selected:item.isSelect}" @click="item.isSelect=!item.isSelect"></span></td>
 <td>
 <div class="good" >
  <img v-bind:src='item.gimg' />
   <div>
  <h3>{{item.gname}}</h3>
  <span>{{item.gbrand}}   {{item.gplace}}</span><br />
  <span>{{item.gpurity}}   {{item.gminnum}}</span><br />
  <span>{{item.gstore}}</span>
   </div>
  </div>
 </td>
 <td><input type="number" v-model="item.gnum" min="0"/></td>
 <td><span>¥{{item.gprice}}</span></td>
 <td><span>¥{{item.gprice*item.gnum}}</span></td>
 <td><button @click="deleteSingle(index)">删除</button></td>
 </tr>
 </tbody>
 </table>
 <div class="footer">
 <button @click="deleteSel">删除所选商品</button>
 <button>继续购物</button>
 <span><span>{{getTotal.num}}</span>件商品(不含运费)总计:<span>¥{{getTotal.allprice}}</span></span>
 <button>去结算</button>
 </div>
 </div>
 </div>
 </body>
 <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
 <script>
 var vm=new Vue({
 el:'#app',
 data:{ 
 goods:[
 {gname:'佳洁士美白牙膏',
 gbrand:'品牌:佳洁士',
 gplace:'产地:上海',
 gpurity:'规格:120g',
 gminnum:'起订量:10件',
 gstore:'仓库地:上海沧海仓储',
 gprice:'800',
 gimg:'img/good.jpg',
 gnum:'3',
 isSelect:false,
 },
 {gname:'佳洁士美白牙膏',
 gbrand:'品牌:佳洁士',
 gplace:'产地:上海',
 gpurity:'规格:120g',
 gminnum:'起订量:10件',
 gstore:'仓库地:上海沧海仓储',
 gimg:'img/good.jpg',
 gprice:'400',
 gnum:'5',
 isSelect:false, 
 }
 ] 
 },
 computed:{
 //实时判断是否全选
 ifAllselect:function(){
 var all;
 for(var i=0;i<this.goods.length;i++){
 if(this.goods[i].isSelect==false){
 all=false;
 break;
 }
 all=true;
 }
 return all;
 },
 //获取总件数和总金额数
 getTotal:function(){
 var num= 0 ;
 var allprice=0;
 for(var i=0;i<this.goods.length;i++){
 if(this.goods[i].isSelect==true){
 num=parseInt(num)+parseInt(this.goods[i].gnum);
 allprice=allprice+this.goods[i].gnum*this.goods[i].gprice;
 }
 }
 return{num:num,allprice:allprice}
 }
 },
 methods:{
 //全选或者取消全部选中
 allSelect:function(ifAllselect){
 for(var i=0;i<this.goods.length;i++){
 this.goods[i].isSelect=!ifAllselect;
 }
 },
 //删除单个商品
 deleteSingle:function(index){
 if(this.goods[index].isSelect==true)
 this.goods.splice(index,1);
 else alert('请选择您要删除的商品!');
 },
 //删除选中的商品
 deleteSel:function(){
 this.goods=this.goods.filter(function(item){return !item.isSelect})
 }
 }
 })
 </script>
</html>
*{
 padding: 0;
 margin: 0;
}
html,body{
 width: 100%;
 overflow-x:hidden ;
}
#app{
 width: 90%;
 margin: 50px auto;
 border: 1px solid gainsboro;
 border-top: none;
}
h2{ 
 display: block;
 border-top: 4px solid dodgerblue;
 font-size: 17px;
 padding-left: 20px;
 line-height: 50px;
 color: dodgerblue;
}
.nav{
 width: 100%;
 height: 40px;
 border:1px solid gainsboro ;
 border-left:none ;
 border-right: none;
}
.nav>div{
 height: 100%;
 float: left;
 display: flex;
 justify-content: center;
 align-items: center;
}
.nav div:nth-child(1){
 width: 15%;
}
.nav div:nth-child(2){
 width:37%;
}
.nav div:nth-child(3){
 width: 12%;
}
.nav div:nth-child(4){
 width: 12%;
}
.nav div:nth-child(5){
 width: 12%;
}
.nav div:nth-child(6){
 width: 12%;
 }
.noselected{
 display: inline-block;
 width: 17px; 
 height:17px;
 margin-right: 5px;
 background: url(../img/nocheck.png) no-repeat;
 background-size: contain;
}
.goods{
 width: 100%;
 height: auto;
}
.goods tr{
 width: 100%;
}
.goods tr td{
 padding: 20px 0;
}
.goods tr>td:nth-child(1){
 width: 17%;
 text-align: center;
}
.goods tr>td:nth-child(2){
 width: 35%;
}
.goods tr>td:nth-child(3){
 width: 12%;
 text-align: center;
}
.goods tr>td:nth-child(4){
 width: 12%;
 text-align: center;
}
.goods tr>td:nth-child(5){
 width: 12%;
 text-align: center;
}
.goods tr>td:nth-child(6){
 width: 12%;
 text-align: center;
}
.good{
 width: 100%;
 display: flex;
 align-items: center;
}
.good img{
 width:120px;
 height: 120px;
 float: left;
 border: 2px solid gainsboro;
 margin-right: 30px;
}
.good>div{
 font-size: 13px;
 line-height: 20px;
}
.good>div h3{
 font-size: 11px;
 margin-bottom: 5px;
}
.goods input[type=number]{
 width: 50px;
}
.goods tr td:nth-child(4),.goods tr td:nth-child(5){
 color: red;
}
button{
 cursor: pointer;
 border: none;
 outline: none;
 background-color: white;
}
.footer{
 display: flex;
 align-items: center;
 width: 100%;
 height: 50px;
 background-color: #F7F7F7;
 position: relative;
}
.footer button{
 border: none;
 background-color: #F7F7F7; 
 font-size: 15px;
}
.footer button:nth-child(1){
 margin-left: 30px;
}
.footer button:nth-child(2){
 margin-left: 60px;
}
.footer button:nth-child(4){
 height: 100%;
 position: absolute;
 right: 0;
 padding:0 20px;
 background-color: orange;
}
.footer>span{
 position: absolute;
 right: 100px;
}
.footer>span span{
 color: red;
}
.selected{
 background: url(../img/check.png) no-repeat;
 background-size: contain;
}

以上为所有的html和css文件代码。

【总结】

1、computed:此处用computed主要有两个作用。一是判断是否全选。如果全选则添加selected这一class,如果没有则不添加;二是计算选择的总商品数和总金额。当用户更改商品数量时,总商品数和总金额也随之改变。

2、return返回两个值:第一次接触function里边return的值是两个这种情况,这种要通过对象的属性访问方法。例如:

function add(a,b){
 var sum;
 var sub
 return{
 sum:a+b,
 sub:a-b
 }
 }
var obj = add(5,2);
console.log(obj.sum);
console.log(obj.sub);

3、js的数组方法filter():

filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。

注意: filter() 不会对空数组进行检测。
注意: filter() 不会改变原始数组。

array.filter(function(currentValue,index,arr), thisValue)
  • currentValue: 必须。当前元素的值
  • index: 可选。当前元素的索引值
  • arr:可选。当前元素属于的数组对象
  • thisValue:可选。对象作为该执行回调时使用,传递给函数,用作 “this” 的值。如果省略了 thisValue ,“this” 的值为 “undefined”

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

Vue.js 相关文章推荐
ESLint 是如何检查 .vue 文件的
Nov 30 Vue.js
vue 通过base64实现图片下载功能
Dec 19 Vue.js
详解Vue的异步更新实现原理
Dec 22 Vue.js
Vue+scss白天和夜间模式切换功能的实现方法
Jan 05 Vue.js
vue keep-alive的简单总结
Jan 25 Vue.js
vue监听键盘事件的相关总结
Jan 29 Vue.js
vue点击弹窗自动触发点击事件的解决办法(模拟场景)
May 25 Vue.js
Vue实现tab导航栏并支持左右滑动功能
Jun 28 Vue.js
Vue实现导入Excel功能步骤详解
Jul 03 Vue.js
vue中 this.$set的使用详解
Nov 17 Vue.js
vue实力踩坑之push当前页无效
Apr 10 Vue.js
三种方式清除vue路由跳转router-link的历史记录
Apr 10 Vue.js
Vue实现小购物车功能
Dec 21 #Vue.js
vue监听滚动事件的方法
Dec 21 #Vue.js
vue el-upload上传文件的示例代码
Dec 21 #Vue.js
vue 在单页面应用里使用二级套嵌路由
Dec 19 #Vue.js
vue中如何添加百度统计代码
Dec 19 #Vue.js
vue 导航守卫和axios拦截器有哪些区别
Dec 19 #Vue.js
Vue——解决报错 Computed property &quot;****&quot; was assigned to but it has no setter.
Dec 19 #Vue.js
You might like
整合了前面的PHP数据库连接类~~做成一个分页类!
2006/11/25 PHP
在VS2008中编译MYSQL5.1.48的方法
2010/07/03 PHP
php简单socket服务器客户端代码实例
2015/05/18 PHP
PHP类的封装与继承详解
2015/09/29 PHP
PHP时间戳格式全部汇总 (获取时间、时间戳)
2016/06/13 PHP
PHP5.6读写excel表格文件操作示例
2019/02/26 PHP
yii框架结合charjs实现统计30天数据的方法
2020/04/04 PHP
论坛里点击别人帖子下面的回复,回复标题变成“回复 24# 的帖子”
2009/06/14 Javascript
javascript 面向对象全新理练之继承与多态
2009/12/03 Javascript
Javascript模拟加速运动与减速运动代码分享
2014/12/11 Javascript
window.onload使用指南
2015/09/13 Javascript
javascript实现input file上传图片预览效果
2015/12/31 Javascript
ES6概念 Symbol.keyFor()方法
2016/12/25 Javascript
JQuery获取鼠标进入和离开容器的方向
2016/12/29 Javascript
微信小程序实现图片上传、删除和预览功能的方法
2017/12/18 Javascript
小程序实现单选多选功能
2018/11/04 Javascript
微信小程序bindtap事件与冒泡阻止详解
2019/08/08 Javascript
Vue 3.0双向绑定原理的实现方法
2019/10/23 Javascript
Node.JS发送http请求批量检查文件中的网页地址、服务是否有效可用
2019/11/20 Javascript
基于vuex实现购物车功能
2021/01/10 Vue.js
Python实现正则表达式匹配任意的邮箱方法
2018/12/20 Python
PyTorch搭建一维线性回归模型(二)
2019/05/22 Python
Python获取一个用户名的组ID过程解析
2019/09/03 Python
分享PyCharm的几个使用技巧
2019/11/10 Python
解决Tensorflow占用GPU显存问题
2020/02/03 Python
在keras下实现多个模型的融合方式
2020/05/23 Python
Python tkinter实现日期选择器
2021/02/22 Python
Vector, ArrayList, HashTable, HashMap哪些是线程安全的,哪些不是
2015/10/12 面试题
网络管理员岗位职责
2014/03/17 职场文书
《生命的药方》教学反思
2014/04/08 职场文书
带刀到教室的检讨书
2014/10/04 职场文书
工程部经理岗位职责
2015/02/02 职场文书
高中数学教学反思范文
2016/02/18 职场文书
Nest.js参数校验和自定义返回数据格式详解
2021/03/29 Javascript
Pytorch 如何实现常用正则化
2021/05/27 Python
Go遍历struct,map,slice的实现
2021/06/13 Golang