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 相关文章推荐
在Vue中使用mockjs代码实例
Nov 25 Vue.js
vue的hash值原理也是table切换实例代码
Dec 14 Vue.js
vue自定义组件实现双向绑定
Jan 13 Vue.js
vue 递归组件的简单使用示例
Jan 14 Vue.js
如何在 Vue 表单中处理图片
Jan 26 Vue.js
Vue ​v-model相关知识总结
Jan 28 Vue.js
Vue 实现可视化拖拽页面编辑器
Feb 01 Vue.js
如何理解Vue前后端数据交互与显示
May 10 Vue.js
Vue如何实现组件间通信
May 15 Vue.js
Vue+Flask实现图片传输功能
Apr 01 Vue.js
vue+elementUI实现表格列的显示与隐藏
Apr 13 Vue.js
vue生命周期钩子函数以及触发时机
Apr 26 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
第五节--克隆
2006/11/16 PHP
php set_magic_quotes_runtime() 函数过时解决方法
2010/07/08 PHP
PHP中的cookie不用刷新就生效的方法
2012/02/04 PHP
php利用scws实现mysql全文搜索功能的方法
2014/12/25 PHP
全面解读PHP的人气开发框架Laravel
2015/10/15 PHP
ThinkPHP中session函数详解
2016/09/14 PHP
Zend Framework处理Json数据方法详解
2016/12/09 PHP
用jquery实现的模拟QQ邮箱里的收件人选取及其他效果(一)
2011/01/06 Javascript
Javascript限制网页只能在微信内置浏览器中访问
2014/11/09 Javascript
iscroll碰到Select无法选择下拉刷新的解决办法
2016/05/21 Javascript
微信小程序 使用canvas制作K线实例详解
2017/01/12 Javascript
Vue中的ref作用详解(实现DOM的联动操作)
2017/08/21 Javascript
集成vue到jquery/bootstrap项目的方法
2018/02/10 jQuery
如何利用node转发请求详解
2020/09/17 Javascript
详解React路由传参方法汇总记录
2020/11/29 Javascript
连接Python程序与MySQL的教程
2015/04/29 Python
详细解读Python的web.py框架下的application.py模块
2015/05/02 Python
Python中用于检查英文字母大写的isupper()方法
2015/05/19 Python
Python实现导出数据生成excel报表的方法示例
2017/07/12 Python
简单了解Django模板的使用
2017/12/20 Python
在windows下Python打印彩色字体的方法
2018/05/15 Python
python3实现单目标粒子群算法
2019/11/14 Python
探秘TensorFlow 和 NumPy 的 Broadcasting 机制
2020/03/13 Python
Python中操作各种多媒体,视频、音频到图片的代码详解
2020/06/04 Python
pycharm全局搜索的具体步骤
2020/07/28 Python
python向企业微信发送文字和图片消息的示例
2020/09/28 Python
python 使用tkinter+you-get实现视频下载器
2020/11/17 Python
英国知名奢侈品包包品牌:Milli Millu
2016/12/22 全球购物
大一学生的职业生涯规划书范文
2014/01/19 职场文书
幼儿园的门卫岗位职责
2014/04/10 职场文书
大学生村官工作总结2015
2015/04/09 职场文书
2015年企业团支部工作总结
2015/05/21 职场文书
高中家长意见怎么写
2015/06/03 职场文书
python析构函数用法及注意事项
2021/06/22 Python
redis中lua脚本使用教程
2021/11/01 Redis
超越Nginx的Web服务器caddy优雅用法
2022/06/21 Servers