vue.js实战——购物车练习(包含全选功能)
vue.js实战第5章 54页的练习1
直接放代码好了,全选的部分搞了好久,代码好像有点啰嗦,好在实现功能了(*^▽^*)
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>购物车示例</title>
<link rel="stylesheet" href="index.css">
</head>
<body>
<div id="app" v-cloak>
<template v-if="list.length">
<table>
<thead>
<tr>
<th>
<input type="checkbox" v-model="checkAll">全选
</th>
<th>商品名称</th>
<th>商品单价</th>
<th>购买数量</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="(item,index) in list">
<!-- <td>{{index+1}}</td> -->
<td><input type="checkbox" :checked="item.check" @click="isAll(index)"> {{item.check}}</td>
<td>{{item.name}}</td>
<td>{{item.price}}</td>
<td>
<button @click="handleReduce(index)" :display="item.count===1">-</button>
{{item.count}}
<button @click="handleAdd(index)">+</button>
</td>
<td>
<button @click="handleRemove(index)">移除</button>
</td>
</tr>
</tbody>
</table>
<div>总价:¥{{totalPrice}}</div>
</template>
<div v-else>
购物车为空
</div>
</div>
<script src="../vue.js"></script>
<script src="index.js"></script>
</body>
</html>
JS:
var app=new Vue({
el:'#app',
data:{
list:[
{
id:1,
name:'iPhone 7',
price:6288,
count:1,
check:false
},{
id:2,
name:'iPad Pro',
price:5888,
count:1,
check:false
},{
id:3,
name:'MacBook Pro',
price:21488,
count:1,
check:false
}
],
checkAll:false,
smallHand:false
},
computed:{
totalPrice:function(){
this.newList=this.list.filter(function(item){
if(item.check){
return item;
}
});
var total=0;
for(var i=0;i<this.newList.length;i++){
var item=this.newList[i];
total+=item.price*item.count;
}
return total.toString().replace(/\B(?=(\d{3})+$)/g,',');//匹配后面已3个数字结尾的非单词边界,换成“,”
/* replace:
用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串
\B :匹配非单词边界
(red|blue|green):查找任何指定的选项
?=n :匹配任何其后紧接指定字符串n的字符串(n量词) 提供后面的n找?
\d :查找数字
n{X}:匹配包含X个n的序列字符串
\d{3}:匹配含有3个数字的字符串
n$ :匹配任何结尾为n的字符串
n+ :匹配任何包含至少一个n的字符串
(\d{3})+$ :匹配至少一个已含有3个数字字符串结尾的字符
*/
}
},
methods:{
handleReduce:function(index){
if(this.list[index].count===1) return;
this.list[index].count--;
},
handleAdd:function(index){
this.list[index].count++;
},
handleRemove:function(index){
this.list.splice(index,1);
},
isAll:function(index){
console.log(this.list[index].check);
var indexItem=this.list[index]; indexItem.check=!indexItem.check;
var num=0;
for(var i=0;i<this.list.length;i++){
var item=this.list[i];
if(item.check){
num++;
}else{
num--;
}
}
console.log(num);////(选中了最后一个)3-全部勾选-勾选全选 (之前全部勾选,取消了任意一个勾选) 1-取消全选的勾选
if(num==3||(num==1&&indexItem.check==false)){ //考虑不周全
this.checkAll=indexItem.check;
this.smallHand=true;
}
}
},
watch:{
checkAll:function(){
if(this.smallHand){ }else{
for(var i=0;i<this.list.length;i++){
this.list[i]['check']=this.checkAll;
}
}
this.smallHand=false;
}
}
})
JS部分补充说明:上面的代码在删除时没办法继续“全选”的同步,而且这里数字被写死是错误的( if(num==3||(num==1&&indexItem.check==false)){ )。
更新改进后的代码如下:
var app=new Vue({
el:'#app',
data:{
list:[
{
id:1,
name:'iPhone 7',
price:6288,
count:1,
check:false
},{
id:2,
name:'iPad Pro',
price:5888,
count:1,
check:false
},{
id:3,
name:'MacBook Pro',
price:21488,
count:1,
check:false
}
],
checkAll:false,
smallHand:false
},
computed:{
totalPrice:function(){
this.newList=this.list.filter(function(item){
if(item.check){
return item;
}
});
var total=0;
for(var i=0;i<this.newList.length;i++){
var item=this.newList[i];
total+=item.price*item.count;
}
return total.toString().replace(/\B(?=(\d{3})+$)/g,',');//匹配后面已3个数字结尾的非单词边界,换成“,”
/* replace:
用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串
\B :匹配非单词边界
(red|blue|green):查找任何指定的选项
?=n :匹配任何其后紧接指定字符串n的字符串(n量词) 提供后面的n找?
\d :查找数字
n{X}:匹配包含X个n的序列字符串
\d{3}:匹配含有3个数字的字符串
n$ :匹配任何结尾为n的字符串
n+ :匹配任何包含至少一个n的字符串
(\d{3})+$ :匹配至少一个已含有3个数字字符串结尾的字符
*/
}
},
methods:{
handleReduce:function(index){
if(this.list[index].count===1) return;
this.list[index].count--;
},
handleAdd:function(index){
this.list[index].count++;
},
handleRemove:function(index){
this.list.splice(index,1);
var num=0;
var allNum=0;
for(var i=0;i<this.list.length;i++){
var item=this.list[i];
allNum++;
if(item.check){
num++;
}else{
num--;
}
}
if(num==allNum){
this.checkAll=true;
}else{
this.checkAll=false;
}
},
isAll:function(index){
console.log(this.list[index].check);
var indexItem=this.list[index]; indexItem.check=!indexItem.check;
var num=0;
var allNum=0;
for(var i=0;i<this.list.length;i++){
var item=this.list[i];
allNum++;
if(item.check){
num++;
}else{
num--;
}
}
console.log(num);////(选中了最后一个)3-全部勾选-勾选全选 (之前全部勾选,取消了任意一个勾选) 1-取消全选的勾选
// if(num==3||(num==1&&indexItem.check==false)){ 这里不能写死的傻瓜
if(num==allNum||(num==(allNum-2)&&indexItem.check==false)){
this.checkAll=indexItem.check;
this.smallHand=true;
}
}
},
watch:{
checkAll:function(){
if(this.smallHand){ }else{
for(var i=0;i<this.list.length;i++){
this.list[i]['check']=this.checkAll;
}
}
this.smallHand=false;
}
}
})
css:
[v-cloak] {
display: none;
}
table {
border: 1px solid #e9e9e9;
border-collapse: collapse;
border-spacing:;
empty-cells: show;
}
table th,
table td {
padding: 8px 16px;
border: 1px solid #e9e9e9;
text-align: left;
}
table th {
background: #f7f7f7;
color: #5c6b77;
font-weight:;
white-space: nowrap;
}
vue.js实战——购物车练习(包含全选功能)的更多相关文章
- 第八十二篇:Vue购物车(三) 实现全选功能
好家伙, 继续完善购物车相应功能 1.如何实现全选和反全选 1.1.全选框的状态显示(父传子) 来一波合理分析: 在页面中,有三个商品中 三个商品中的第二个未选择, 我么使用一个计算属性(fullSt ...
- Js 实战3(实现全选)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs ...
- js实现简单的菜谱全选功能
思路:全选按钮和子按钮分开考虑,当全选按钮选中的时候,也就是其checked为true的时候,所有的子按钮也全都为true,反之,则为false.子按钮的想法是,当点击某一个子按钮的时候,会看一下是否 ...
- 分享Node.js + Koa2 + MySQL + Vue.js 实战开发一套完整个人博客项目网站
这是个什么的项目? 使用 Node.js + Koa2 + MySQL + Vue.js 实战开发一套完整个人博客项目网站. 博客线上地址:www.boblog.com Github地址:https: ...
- vue.js 实战 todo list
vue.js 起源 vue.js 的作者是尤雨溪,是一名中国人,之前在谷歌工作,现在在全职维护 vue 项目. vue.js 是 2014 年推出来的.现在已经更新到 2.x 版本,3.0 版本会在 ...
- JS全选功能代码优化
原文:JS全选功能代码优化 JS全选功能代码优化 最近在看javascript MVC那本书,也感觉到自己写的代码也并不优雅,所以一直在想 用另一种模式来编写JS代码,所以针对之前的简单的JS全选功能 ...
- jquery与js实现全选功能的区别---2017-05-12
一.jquery常用的事件 click(),dbclick() focus(),blur() change() keydown(),keypress(),keyup() mousedown(),mou ...
- JS实现全选功能
000. 开始 学习JS有一段时间了,最近看了一些JS练手的小demo实例,自己也尝试着用JS进行实现. 全选功能是在很多注册页面.获取用户兴趣爱好.让用户勾选一些选项等页面中常见的一种效果,主要有全 ...
- vue.js实现购物车功能
购物车是电商必备的功能,可以让用户一次性购买多个商品,常见的购物车实现方式有如下几种: 1. 用户更新购物车里的商品后,页面自动刷新. 2. 使用局部刷新功能,服务器端返回整个购物车的页面html 3 ...
随机推荐
- 【学习笔记】tensorflow实现一个简单的线性回归
目录 准备知识 Tensorflow运算API 梯度下降API 简单的线性回归的实现 建立事件文件 变量作用域 增加变量显示 模型的保存与加载 自定义命令行参数 准备知识 Tensorflow运算AP ...
- BN算法
批量归一化(BN: Batch Normalization) 1 BN训练 1)随机梯度下降法(SGD)对于训练深度网络简单高效,但是它有个毛病,就是需要我们人为的去选择参数,比如学习率.参数初始化. ...
- Android 的 so 文件加载机制
本篇文章已授权微信公众号 guolin_blog (郭霖)独家发布 最近碰到一些 so 文件问题,顺便将相关知识点梳理一下. 提问 本文的结论是跟着 System.loadlibrary() 一层层源 ...
- JSON WEB TOKEN(JWT)的分析
JSON WEB TOKEN(JWT)的分析 一般情况下,客户的会话数据会存在文件中,或者引入redis来存储,实现session的管理,但是这样操作会存在一些问题,使用文件来存储的时候,在多台机器上 ...
- Vue开篇之Vue-cli搭建项目
介绍 Vue.js是一套构建用户界面的渐进式框架.Vue 只关注视图层,采用自底向上增量开发的设计.Vue 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件. 第一步:安装node ...
- Microsoft Dynamics CRM 2015 and Microsoft Dynamics CRM 2016 Performance and Scalability Documentation
摘要: 本人微信公众号:微软动态CRM专家罗勇 ,回复285或者20181126可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me ...
- Android 使用Picasso加载网络图片等比例缩放
在做android图片加载的时候,由于手机屏幕受限,很多大图加载过来的时候,我们要求等比例缩放,比如按照固定的宽度,等比例缩放高度,使得图片的尺寸比例得到相应的缩放,但图片没有变形.显然按照andro ...
- Linux操作系统--定时任务
最近在学习Linux操作系统.学到了关于定时任务的章节,作为一个总结写下这篇文章.在Linux中,我们可以将耗时大的任务如复制大文件,压缩.解压缩大文件等放进定时任务中(深夜执行,因为工作时间访问量大 ...
- 如何用git上传代码到github详细步骤
注册账户 这个小菜鸟带着心跳写的第一篇博客! 还请大家多多提点! 想使用github,第一步肯定是要注册github账号,有了账号就是直接登录啦 可以直接打开http://github.com页面注册 ...
- iOS 限制TextField输入长度(支持删除)
if (textField == _phoneTF) { //支持删除 && ) { return YES; } ) { _phoneTF.text = [textField.text ...