购物车页面的设计图
商品的列表
代码:
<ul v-if="shoppingListData.rows.length"> <li v-for="(item,index) in shoppingListData.rows" :key="index" > <van-checkbox :value="item.goods_id" v-model="item.isChecked" checked-color="#07c160" @click="chooseChange(item.goods_id)" ></van-checkbox> <div> <div><img :src="item.goods_image+'?w=150&h=150&crop=1'" alt="" ></div> <div> <div><p>{{item.goods_name}}</p></div> <!-- <p>{{item.color}}</p> --> <div> <span>{{item.now_price | formatMoney}}</span> <span> <button @click="handleReduce(index)" :disabled="item.goods_qty===1" >-</button> <i>{{item.goods_qty}}</i> <button @click="handleAdd(index)">+</button> </span> </div> </div> </div> </li> </ul>
全选的复选框
全选的代码:
<div> <van-checkbox v-model="AllChecked" @click="checkAll" >全选</van-checkbox> <span v-if="managementShow" @click="management()" >管理</span> <span v-if="finishShow" @click="management()" >完成</span> <van-button type="default" @click="sureDel()" v-if="finishShow">删除</van-button> <div v-if="managementShow"> <span>合计</span> <i>{{ totalPrice }}</i> <van-button type="default" @click="closeAnAccount()">结算</van-button> </div> </div>
单选的change事件
代码:
// 单选的change事件 chooseChange(id) { if (this.selectedData.indexOf(id) > -1) { this.remove(this.selectedData, id); } else { this.selectedData.push(id); } if (this.selectedData.length < this.shoppingListData.total) { this.AllChecked = false; } else { this.AllChecked = true; } console.log(this.selectedData); }
全选的JS
全选的代码:
// 全选和反选 checkAll() { let list = this.shoppingListData.rows; if (this.AllChecked) { list.forEach(element => { element.isChecked = false; }); this.selectedData = []; } else { list.forEach(element => { element.isChecked = true; if (this.selectedData.indexOf(element.goods_id) < 0) { this.selectedData.push(element.goods_id); } }); console.log(this.selectedData); } },
数组删除
代码
//数组删除 remove(arr, val) { var index = arr.indexOf(val); if (index > -1) { arr.splice(index, 1); } }
总结
以上所述是小编给大家介绍的vue+vant-UI框架实现购物车的复选框全选和反选功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
您可能感兴趣的文章: