
computed 计算属性



正则

css部分
[v-cloak] {
display : none ;
}
table{
border : lpx solid #e9e9e9 ;
border- collapse: collapse ;
border-spacing : 0;
empty-cells : show ;
}
th , td{
padding : Spx 16px ;
border : lpx solid #e9e9e9 ;
text-align : left;
}
th{
background: #f7f7f7 ;
color : #5c6b77 ;
font-weight: 600 ;
wh te-space: nowrap ;
}
.checks {
font-size: 16px;
text-align: center;
}
Js部分
var app = new Vue({
el: '#app',
data:{
checkall: false,
list:[
{
title_name: '手机',
content: [
{ id: 1, name: '小米9', price: 3099, count: 9, check: false,
},
{ id: 2, name: '华为30', price: 4888, count: 1, check: false,
},
{ id: 3, name: 'Vivo20', price: 3199, count: 1, check: false,
},
]
},
{
title_name: '日常用品',
content: [
{ id: 1, name: '牙刷', price: 88, count: 1, check: false,
},
{ id: 2, name: '毛巾', price: 58, count: 1, check: false,
},
{ id: 3, name: '口杯', price: 28, count: 1, check: false,
},
]
},
{
title_name: '学习用品',
content: [
{ id: 1, name: '签字笔', price: 18, count: 1, check: false,
},
{ id: 2, name: '橡皮', price: 8, count: 1, check: false,
},
{ id: 3, name: '笔记本', price: 2, count: 1, check: false,
},
]
},
],
},
computed:{
totalPrice: function(){
var total = 0;
for( var i = 0; i < this.list.length; i++){
var item = this.list[i];
for(var j = 0; j<item.content.length; j++){
var items = item.content[j];
if(items.check === true){
total += items.price * items.count;
}
}
}
return total.toString().replace(/B(?=(d{3})+$)/g,','); //3位式显示金额
},
},
methods:{
handleReduce: function(index1, index2){
if(this.list[index1].content[index2].count === 1) return; //商品数量等于‘1’时,关闭按钮
this.list[index1].content[index2].count--; //商品数量减一
},
handleAdd: function(index1, index2){
this.list[index1].content[index2].count++; //商品数量加一
},
handleRemove: function(index1, index2){
this.list[index1].content.splice(index2, 1);
},
handleChecked:function(index1,index2){
this.list[index1].content[index2].check = !this.list[index1].content[index2].check
if(this.list[index1].content[index2].check === false){
this.checkall = false;
};
var checkallkey = true;
for(var i=0;i<this.list.length;i++){
for(var j=0;j<this.list[i].content.length;j++){
if(this.list[i].content[j].check === false) checkallkey = false;
}
}
this.checkall = checkallkey;
},
handleAllChecked: function(){ //全选 or 取消全选
if(this.checkall === false){
for(var i=0; i<this.list.length; i++){
for(var j=0; j<this.list[i].content.length; j++){
this.list[i].content[j].check = true;
}
}
this.checkall = true;
}
else if(this.checkall === true){
for(var i=0; i<this.list.length; i++){
for(var j=0; j<this.list[i].content.length; j++){
this.list[i].content[j].check = false;
}
}
this.checkall = false;
}
},
},
});
HTML 部分
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>购物车实战</title>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>
<body>
<div id="app" v-cloak>
<template v-if="list.length">
<!-- <template>里的代码分两部分, 一部分是商品列表信息,我们用表格 table 来展现:
另一部分就是带有千位分隔符的商品总价(每隔三位数加进一个逗号〉。这部分代码如下: -->
<table border="" cellspacing="" cellpadding="">
<thead>
<tr>
<th><input type="checkbox" id="checkbox" v-model="checked" @change="changeAllChecked()" /></th>
<th>商品编号</th>
<th>商品价格</th>
<th>商品单价</th>
<th>购买数量</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<!-- 循环list -->
<tr v-for="(item,index) in list">
<td><input type="checkbox" :value="item" v-model="checkedData" /></td>
<td>{{ index + 1 }}</td>
<td>{{ item.name }}</td>
<td>{{ item.price }}</td>
<td>
<button @click="handleReduce(index)"
:disabled="item.count === 1">-</button>
{{ item.count }}
<button @click="handleAdd(index)">+</button>
</td>
<td>
<button @click="handleRemove(index)">移除</button>
</td>
</tr>
</tbody>
</table>
<div class="checks">
<div>总价:¥ {{ totalPrice }}</div>
<br>
<span >
选择的值为:
<tr >
<td>{{ checkedData }}</td>
</tr>
</span>
</div>
</template>
<div v-else>购物车空了,添加一些商品吧</div>
</div>
<!-- 注意,这里将vue.min. js ,index.js
文件写在<body 的最底部,如果写在<head>里 Vue 实例将无法创建,
因为此时 DOM 还没有被解析完成,除非通过异步或在事件
DOMContentLoaded (IE 是onreadystatechange )
触发时再创建 Vue 实例,这有点像 jQuery 的$(document).ready()方法。 -->
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<!-- import JavaScript -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script src="./js/index.js"></script>
</body>
</html>