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="allListNum">
<table>
<thead>
<tr>
<th>
<input type="checkbox" v-model="checkAll">全选
</th>
<th>商品名称</th>
<th>商品单价</th>
<th>购买数量</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<template v-for="(item,index) in list">
<tr v-if="item.content.length">
<td colspan="5">{{item.name}}</td>
</tr>
<tr v-for="(small,smallIndex) in item.content">
<!-- <td>{{index+1}}</td> -->
<td><input type="checkbox" :checked="small.check" @click="isAll(index,smallIndex)"> {{small.check}}</td>
<td>{{small.name}}</td>
<td>{{small.price}}</td>
<td>
<button @click="handleReduce(index,smallIndex)" :display="small.count===1">-</button>
{{small.count}}
<button @click="handleAdd(index,smallIndex)">+</button>
</td>
<td>
<button @click="handleRemove(index,smallIndex)">移除</button>
</td>
</tr>
</template> </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:[
{
name:'电子产品',
content:[
{
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
}
]
},{
name:'图书',
content:[
{
id:1,
name:'《小王子》',
price:10000000000,
count:1,
check:false
},{
id:2,
name:'《失控》',
price:100,
count:1,
check:false
},{
id:3,
name:"《目送》",
price:40,
count:1,
check:false
},{
id:4,
name:'《爱与孤独》',
price:10,
count:1,
check:false
}
]
} ],
checkAll:false,
smallHand:false
},
computed:{
totalPrice:function(){
var arr=[];
for(var i=0;i<this.list.length;i++){
arr=arr.concat(this.list[i]['content']);
}
this.newList=arr.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个数字字符串结尾的字符
*/
},
allListNum:function(){
var allNum=0;
for(var i=0;i<this.list.length;i++){
var item=this.list[i]['content'];
for(var j=0;j<item.length;j++){
allNum++;
}
}
return allNum;
}
},
methods:{
handleReduce:function(index,smallIndex){
console.log(index);
console.log(smallIndex);
if(this.list[index]['content'][smallIndex].count===1) return;
this.list[index]['content'][smallIndex].count--;
},
handleAdd:function(index,smallIndex){
this.list[index]['content'][smallIndex].count++;
},
handleRemove:function(index,smallIndex){
this.list[index]['content'].splice(smallIndex,1);
var num=0;
for(var i=0;i<this.list.length;i++){
var item=this.list[i]['content'];
for(var j=0;j<item.length;j++){
if(item[j].check){
num++;
}else{
num--;
}
}
}
if(num==this.allListNum){
this.checkAll=true;
}else{
this.checkAll=false;
}
},
isAll:function(index,smallIndex){
console.log(this.list[index]['content'][smallIndex].check);
var indexItem=this.list[index]['content'][smallIndex]; indexItem.check=!indexItem.check;
var num=0;
for(var i=0;i<this.list.length;i++){
var item=this.list[i]['content'];
for(var j=0;j<item.length;j++){
if(item[j].check){
num++;
}else{
num--;
}
}
}
console.log(num);////(选中了最后一个)3-全部勾选-勾选全选 (之前全部勾选,取消了任意一个勾选) 1-取消全选的勾选
// if(num==7||(num==5&&indexItem.check==false)){ 这里的值不能写死,太笨了 if(num==this.allListNum||(num==(this.allListNum-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++){
var list=this.list;
for(var j=0;j<list[i]['content'].length;j++){
this.list[i]['content'][j]['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实战——升级版购物车的更多相关文章

  1. vue.js实战——购物车练习(包含全选功能)

    vue.js实战第5章 54页的练习1 直接放代码好了,全选的部分搞了好久,代码好像有点啰嗦,好在实现功能了(*^▽^*) HTML: <!DOCTYPE html> <html l ...

  2. 【Vue.js实战案例】- Vue.js递归组件实现组织架构树和选人功能

    大家好!先上图看看本次案例的整体效果. 浪奔,浪流,万里涛涛江水永不休.如果在jq时代来实这个功能简直有些噩梦了,但是自从前端思想发展到现在的以MVVM为主流的大背景下,来实现一个这样繁杂的功能简直不 ...

  3. 分享Node.js + Koa2 + MySQL + Vue.js 实战开发一套完整个人博客项目网站

    这是个什么的项目? 使用 Node.js + Koa2 + MySQL + Vue.js 实战开发一套完整个人博客项目网站. 博客线上地址:www.boblog.com Github地址:https: ...

  4. vue.js 实战 todo list

    vue.js 起源 vue.js 的作者是尤雨溪,是一名中国人,之前在谷歌工作,现在在全职维护 vue 项目. vue.js 是 2014 年推出来的.现在已经更新到 2.x 版本,3.0 版本会在 ...

  5. vue.js实战(文摘)

    ---------------第1篇 基础篇 第1章 初始vue.js 第2章 数据绑定和第一个vue应用 第3章 计算属性 第4章 v-bind及class与style绑定 第5章 内置命令 第6章 ...

  6. Vue.js实战

    指令 什么是指令 指令,directives,是vue非常常用的功能,在template里. 都是以v-开头 不是自己所为html元素,比如假设指令叫v-abc,没有这种写法,这是组件(compone ...

  7. Vue.js 实战教程(附demo)

    在实战之前,你需要对vuejs的基础语法有一定的了解,可以通过以下几个途径进行学习: vue.js官方文档:https://cn.vuejs.org/v2/guide/index.html vue.j ...

  8. Vue.js 实战总结

    最近在某个项目中用到了Vue.js,从上手做开发到项目发布,一步步踩了不少坑.本文试图总结过去一个多月使用Vue.js中的一些经验,也算是一点心得体会吧,拿出来与大家分享,欢迎多多交流. Vue.js ...

  9. vue.js实战——props单向数据流

    Vue2.x通过props传递数据是单向的了,也就是父组件数据变化时会传递给子组件,但是反过来不行. 业务中会经常遇到两种需要改变prop的情况, 一种是父组件传递初始值进来,子组件将它作为初始值保存 ...

随机推荐

  1. zepto和jQuery on事件委托在苹果手机上的”坑“

    移动端用zepto做的页面,突然发现on绑定的click事件并没有触发,代码如下: <ul> <li class="a">1111111aaaaaaasss ...

  2. 编译内核时出现drivers/mfd/mxc-hdmi-core.c:36:24: fatal error: mach/clock.h: No such file or directory

    在学习恩智浦IMX6D开发板时,编译内核出现 drivers/mfd/mxc-hdmi-core.c::: fatal error: mach/clock.h: No such file or dir ...

  3. CSS实现单行、多行文本超出部分显示省略号

     单行文本超出,代码如下: css代码: <style> .one{ width:200px; overflow: hidden; text-overflow:ellipsis; whit ...

  4. MySQL基础、主从复制、优化

    MySQL apache公司 开源共享 免费 mysql [-hlocalhost] -uroot -p 以超级管理员的身份登录 use demo; 查看 DCL(数据库控制语言): DDL(数据库定 ...

  5. 为你揭秘知乎是如何搞AI的——窥大厂 | 数智方法论第1期

    文章发布于公号[数智物语] (ID:decision_engine),关注公号不错过每一篇干货. 数智物语(公众号ID:decision_engine)出品 策划.编写:卷毛雅各布 「我们相信,在垃圾 ...

  6. 轨迹系列6——车载GPS对接方案汇总小结(809、自定义协议、前置库、WS)

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 最近在不同项目中对接了多个车载GPS厂商服务终端,绝大多数厂商 ...

  7. 解决jest处理es模块

    解决jest处理es模块 问题场景 项目使用jest进行测试时, 当引入外部库是es模块时, jest无法处理导致报错. Test suite failed to run Jest encounter ...

  8. Django 如何获取真实远程客户端IP

    问题简述 我们知道HttpRequest.META字典包含所有HTTP头部信息(可用的头部信息取决于客户端和服务器).一般情况下,HttpRequest.META.get('REMOTE_ADDR') ...

  9. ES6使用的一些方法

    查找数组中符合条件的所有记录 var list=[ {id:1,name:"张三"}, {id:2,name:"李四"}, {id:3,name:"王 ...

  10. 离线搭建Android Studio开发环境

    https://blog.csdn.net/lilang_9920/article/details/81005345 自媒体可放心发,到时候再删就行了,to thi tha