vue2.0购物车

https://github.com/weibsgz/vue2.0_cart
VUE2.0购物车代码下载
====================================

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/css.css">
</head>

<body>
<div class="main" id="app">
<table class="table">
<tr>
<th></th>
<th>商品信息</th>
<th>商品金额</th>
<th>商品数量</th>
<th>总金额</th>
<th>编辑</th>
</tr>
<tr v-for="(item,index) in productListFilter">
<td>
<div class="uncheck" v-bind:class="{'checked':item.checked}" @click="selectedProduct(item)"></div>
</td>
<td>
<div class="item_img">
<img v-bind:src="item.productImage" alt="" width="150px" height="150px">
<p>{{item.productName + index}}</p>
<!-- 这里再次循环item中的数据 -->
<p>赠送:<span v-for="part in item.partsName">{{part.partsName}}</span></p>
</div>
</td>
<td>
<p>{{item.productPrice | formatMoney}}</p>
</td>
<td>
<em @click="reduce(item)"> - </em>
<!-- 双向数据绑定,绑定数量影响总价格 -->
<input type="text" style="width: 30px;" v-model="item.productQuentity">
<em @click="add(item)"> + </em>
</td>
<td>
<p class="totalCount">{{item.productPrice * item.productQuentity | money('元')}}</p>
</td>
<td>
<p class="del" @click="delProduct(index)">删除</p>
</td>
</tr>
</table>
<div class="menu">
<div class="menuLeft">
<div class="uncheck" style="display: inline-block;" :class="{'checked':checkAllFlag}" @click="checkAll()"></div>
<span style="margin-right: 15px;">全选</span><span>取消全选</span>
</div>
<div class="menuRight">
总金额<span>{{totalMoney}}</span>
<button>结算</button>
</div>
</div>


<div class="test">
<ul>
<li v-for="(item,index) in tab" :class="{'active':testIndex == index}" @click="tabChange(index)">
{{item.name}}
</li>
</ul>
<!-- 第一种方法: -->
<div class="content" >
{{tabContent[activeContentIndex].value}}
</div>

<!-- 第二种方法: -->
<!-- <div class="content" v-for="(item,index) in tabContent" v-show="stat == index">{{item.value}}</div> -->

</div>

</div>





<script type="text/javascript" src="js/vue.min.js"></script>
<script type="text/javascript" src="js/vue-resource.min.js"></script>
<script src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
var vm = new Vue({
el: "#app",
data: {
totalMoney: 0,
productList: [],
checkAllFlag:false,
limitNum:2,

//以下数据是为了TAB切换准备
tab:[{
"name":"1111"
},
{
"name":"2222"
},{
"name":"3333"
}],
testIndex:0 ,
activeContentIndex:0,
tabContent:[{
"value":"vvvvvvv1111111111"
},
{
"value":"vvvvvvv22222222"
},{
"value":"vvvvvvv3333333"
}],
stat:0
},
//可以内部注册也可以全局vue.filters注册
filters: {
formatMoney:function(value){
return "¥"+value.toFixed(2)
}
},
//代替1.0的ready方法,还是要配合nextTick使用
created: function() {
this.$nextTick(function () {
vm.cartView()
})
},
computed:{
totalMoney:function(){
var _this = this;
var totalMoney = 0;

this.productList.forEach(function(v,i){
if(v.checked){
totalMoney += v.productPrice * v.productQuentity;
}
})
return totalMoney
},
//VUE2.0 建议的给v-for增加条件限制的方法,现在只返回前两条数据
//注意slice和splice的区别 splice() 方法 用于插入、删除或替换数组的元素。会改变原数组
//对于数组对象来说,slice 方法提取 从 start下标起 以end下标 为结尾的
//一段元素(但不包括end下标的元素),然后返回新的数组,对原数组没有任何是影响,
productListFilter: function () {
return this.productList.slice(0,this.limitNum)
}
},


methods: {
cartView: function() {
//this指向VUE对象, 函数内的this变化了指向所以要先存起来
var _this = this
this.$http.get('data/data.json', {}).then(function(res) {
_this.productList = res.body.result.list;
// _this.totalMoney = res.body.result.totalMoney;

})

},
add:function(item){
item.productQuentity ++;
// this.calcTotalPrice()
},
reduce:function(item){
if(item.productQuentity === 0){
return
}
else
{
item.productQuentity --
}
// this.calcTotalPrice()
},
selectedProduct:function(item){
//判断item.checked是不是存在
if(typeof item.checked === 'undefined')
{
//全局注册一个checked属性
Vue.set(item,"checked",true)

//局部注册
//this.$set(item,"checked",true)
}
else
{
item.checked = !item.checked
//如果取消了其中一个的选择 那么也取消全选按钮
if(item.checked == false)
{
this.checkAllFlag = false
}
}

//this.calcTotalPrice()


},
checkAll:function(){
this.checkAllFlag = ! this.checkAllFlag;
var _this = this;
if(this.checkAllFlag){
this.productList.forEach(function(v,i){
Vue.set(v,"checked",true)
})
}
else{
this.productList.forEach(function(v,i){
Vue.set(v,"checked",false)
})
}

},

//前面用了computed属性 也可以单独使用一个方法
/* calcTotalPrice:function(){
var _this = this;
//每次先清0;
_this.totalMoney = 0;
this.productList.forEach(function(v,i){
if(v.checked){
_this.totalMoney += v.productPrice * v.productQuentity;
}
})
}*/
delProduct:function(index){
this.productList.splice(index,1)
},

tabChange:function(index){
this.testIndex=index;
//两种方法
this.activeContentIndex = index;
this.stat = index;
}
}

})
//全局过滤器
Vue.filter('money',function(value,type){
return "¥"+value.toFixed(2) + type
})
</script>
</body>

</html>