In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
这篇"vue怎么实现商品购物车全选反选"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇"vue怎么实现商品购物车全选反选"文章吧。
项目需求:
实现一个购物车
全选框实现对商家和商品的全选
商家全选框实现对当前商家所有商品的全选
取消其中一个商品则取消对应商家全选和全选框
选中一个商家下的所有商品则勾选对应商家的全选框,不勾选全选框
选中所有商品则勾选所有商家全选框和全选框
我的思路:
1、通过对数据的简单操作可实现更深层次的全选操作
2、vue.$set(object, key, value)给对象添加属性可以更新视图
3、通过es6的every判断数组中是否每个元素都满足条件,都满足返回true,有一个不满足则返回false
template:
全选 {{val.seller}} {{item.name}}
scritpt层
var app = new Vue({ el: "#app", data: { checkedAll: false, //控制是否全选 data: [{ seller: "商家1", goodsList: [{ name: '商品1' }] }, { seller: "商家2", goodsList: [{ name: '商品2' }, { name: '商品3' }] }] //数据 }, mounted() { this.data.forEach(item => { this.$set(item, "checked", false) //Vue 解决不能检测到对象属性的添加或删除 // item.checked = false; //如果为真实数据直接设置的对象改变值不会更新视图 if (item.goodsList) { item.goodsList.forEach((citem) => { this.$set(citem, "checked", false) // citem.checked = false; }) } }); }, methods: { //全选 checkAll() { this.data.forEach(item => { item.checked = this.checkedAll; if (item.goodsList) { item.goodsList.forEach(citem => { citem.checked = this.checkedAll; }) } }) }, //商家全选 _checkAll(val, k) { val.goodsList.forEach(item => { item.checked = val.checked; }); if (this.data.every(item => item.checked)) { this.checkedAll = true; } else { this.checkedAll = false; } }, //商品选择框 handleCheck(item, index) { var check = []; //保存中间层是否被选中的布尔值 this.data.forEach((items, index) => { if (items.goodsList) { var bool = items.goodsList.every(citem => citem.checked); if (bool) { items.checked = true; } else { items.checked = false; } check.push(bool); } }) if (check.indexOf(false) == -1) { this.checkedAll = true; } else { this.checkedAll = false; } }, }})
效果如下
以上就是关于"vue怎么实现商品购物车全选反选"这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注行业资讯频道。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.