今天中午废了一会时间,总算把项目中的购物车的单选、全选、以及实现数据的动态显示做出来了,给小白分享一下我个人一个解决办法:
购物车的基本页面如下:
先说实现的总体思路
这是我个人的思路,具体代码实现如下:
html:
<div id="a" class="container">
<table class="table table-hover" v-if="list.length">
<thead>
<tr>
<th><input type="checkbox" id="box" @click="checkAll" v-model="checkall" /></th>
<th>图片</th>
<th>商品名</th>
<th>数量</th>
<th>单价</th>
<th>总金额</th>
<th>加入时间</th>
<th>删除</th>
</tr>
</thead>
<tbody>
<tr v-for="(dateil,index) in list" :key="index">
<td><input type="checkbox" class="checkbox" v-model="checked" @click="select(dateil.detailId,dateil.detailProductprice,dateil.detailProductnum)" :value="dateil.detailId" /></td>
<td><a @click="goShop(dateil.detailProductId)" ><img v-bind:src="web_server_static+dateil.product.productPhoto"></a></td>
<td><a @click="goShop(dateil.detailProductId)" >{{dateil.product.productName}}</a></td>
<td>{{dateil.detailProductnum}}</td>
<td>{{dateil.detailProductprice}}</td>
<td>{{dateil.detailProductprice*dateil.detailProductnum}}</td>
<td>{{dateil.detailDatetime}}</td>
<td>
<button type="button" id="but" @click="del(dateil.detailId)" class="btn btn-danger">删除</button>
</td>
</tr>
</tbody>
</table>
<div v-else >购物车空空如也,请先去购买商品~</div>
<div id="label_btn">
<span><label>已选商品<a>{{count}}</a>件,共</a>¥{{price}}</a>元 数组{{checked}}</label>
</span>
<span><button type="button" id="btn-close" @click="jiesuan()" class="btn btn-danger">结算</button></span>
</div>
<!--结算按钮-->
</div>
Vue中的数据应该这样写
var vue = new Vue({
el: '#a',
data: {
list: [],
checkall: false,
checked: [],
price:0,
count:0,
}
js:
checkAll: function() {
/**
*控制全选反选
*/
var _this = this
//true的时候是全选,false是不选
if(this.checkall) {
// 实现反选,按钮选中时 实现了反选,列表为空
this.checked = []
this.price=0;
this.count=0;
} else {
// 实现全选
this.price=0;
this.count=0;
this.checked = []
this.list.forEach(function(dateil) {
_this.price+=parseInt(dateil.detailProductprice);
_this.count+=parseInt(dateil.detailProductnum);
_this.checked.push(dateil.detailId)
})
}
if(this.checked.length === this.list.length) {
this.checkall = true
// console.log(this.checkall)
// console.log(this.checked)
}
}
/**
* 当单选框选中时
*/
checkAll: function() {
var _this = this
//true的时候是全选,false是不选
if(this.checkall) {
// 实现反选,按钮选中时 实现了反选,列表为空
this.checked = []
this.price=0;
this.count=0;
} else {
// 实现全选
this.price=0;
this.count=0;
this.checked = []
this.list.forEach(function(dateil) {
_this.price+=parseInt(dateil.detailProductprice);
_this.count+=parseInt(dateil.detailProductnum);
_this.checked.push(dateil.detailId)
})
}
if(this.checked.length === this.list.length) {
this.checkall = true
// console.log(this.checkall)
// console.log(this.checked)
}
}
这样一个购物车的全选、单选、与数据的显示就完成了。
以上所述是小编给大家介绍的Vue实现购物车的全选、单选、显示商品价格详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对亿速云网站的支持!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。