https://github.com/weibsgz/vue2.0_cart VUE2.0购物车代码下载 ====================================
<!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></p>
<!-- 这里再次循环item中的数据 -->
<p>赠送:<span v-for="part in item.partsName"></span></p>
</div>
</td>
<td>
<p></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"></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></span>
<button>结算</button>
</div>
</div>
<div class="test">
<ul>
<li v-for="(item,index) in tab" :class="{'active':testIndex == index}" @click="tabChange(index)">
</li>
</ul>
<!-- 第一种方法: -->
<div class="content" >
</div>
<!-- 第二种方法: -->
<!-- <div class="content" v-for="(item,index) in tabContent" v-show="stat == index"></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>