Vue实现单选多选反选全选全不选

该博客介绍了如何在Vue.js应用中实现单选、多选、反选以及全选全不选的功能。通过使用特定的数据结构和事件处理方法,结合HTML和JavaScript,实现了点击选中和取消选中。文章特别强调了在使用mint-ui的mt-cell-swiper组件进行左滑删除功能时,需使用v-show而非v-if以避免问题。此外,还提供了相关的HTML代码段、数据结构定义、计算属性和方法,以及二级菜单的单选实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

功能:实现点击选中,再点击取消选中,全选,取消全选,左滑删除。

页面效果如下:

左滑删除功能需要用到mint-ui的mt-cell-swiper。

注意使用这个插件的时候不能用v-if,要用v-show,否则无法实现预期效果。

(我在这上面踩了很久的坑,才突然顿悟的) 

需要引入mint相关的js和css

<link rel="stylesheet" type="text/css" href="../css/mint-ui.css" />


                
### Vue 实现列表单选全选功能 #### 列表单选功能实现 对于列表中的单选操作,在Vue应用中可以通过绑定`v-model`指令来追踪当前被中的项。下面是一个简单的例子,展示了如何创建一个可以单选的列表: ```html <div id="app"> <ul> <li v-for="(item, index) in items" :key="index" @click="selectItem(index)" :class="{ 'selected': selectedIndex === index }">{{ item }}</li> </ul> Selected Item: {{ selected }} </div> ``` ```javascript new Vue({ el: '#app', data() { return { items: ['Option A', 'Option B', 'Option C'], selectedIndex: null, }; }, computed: { selected() { if (this.selectedIndex !== null && this.items[this.selectedIndex]) { return this.items[this.selectedIndex]; } return ''; } }, methods: { selectItem(index) { this.selectedIndex = index; } } }); ``` 此代码片段实现了当用户点击某个列表项时会更新`selectedIndex`变量从而改变样式并显示所项[^1]。 #### 列表功能实现 为了支持模式,则需要维护一个数组用于存储已中的索引或对象ID,并通过事件处理函数管理这个集合的状态变化。这里给出一个的例子: ```html <div id="multi-app"> <ul> <li v-for="(item, idx) in multiItems" :key="idx" @click="toggleSelection(idx)" :class="{ active: isSelected(idx) }">{{ item.name }}</li> </ul> Selected Items: {{ selectedMultiItems.join(', ') || "None"}} </div> ``` ```javascript new Vue({ el: '#multi-app', data() { return { multiItems: [ { name: 'Apple' }, { name: 'Banana' }, { name: 'Orange'} ], selectedIndexes: [] }; }, methods: { toggleSelection(idx) { const i = this.selectedIndexes.indexOf(idx); if(i > -1){ this.selectedIndexes.splice(i, 1); // Remove from selection. }else{ this.selectedIndexes.push(idx); // Add to selection. } }, isSelected(idx) { return this.selectedIndexes.includes(idx); } }, computed: { selectedMultiItems(){ return this.multiItems.filter((_, idx) => this.isSelected(idx)).map(item=>item.name); } } }); ``` 这段脚本允许用户点击同的条目来进行择,并且能够动态映用户的交互行为。 #### 全选功能实现 要增加全选/取消全选以及择的功能,可以在界面上加入额外按钮控制整个列表的择状态。以下是增强版的组件加上了这些特性后的样子: ```html <div id="all-select-app"> <!-- 添加两个按钮 --> <button @click="selectAll()">Select All</button> <button @click="invertSelection()">Invert Selection</button> <ul> <li v-for="(item, idx) in allSelectItems" :key="idx" @click="toggleAllSelection(idx)" :class="{ active: isAllSelected(idx) }">{{ item.label }}</li> </ul> Selected Count: {{ selectedCount }} / Total: {{ allSelectItems.length }} </div> ``` ```javascript new Vue({ el: '#all-select-app', data() { return { allSelectItems: [{ label: 'Red'}, {label:'Green'}, {label:'Blue'}], allSelectedIndexes: [], }; }, methods: { toggleAllSelection(idx) { let i = this.allSelectedIndexes.indexOf(idx); if (i >= 0) { this.allSelectedIndexes.splice(i, 1); } else { this.allSelectedIndexes.push(idx); } }, selectAll() { this.allSelectedIndexes = Array.from({ length: this.allSelectItems.length }, (_, i) => i); }, invertSelection() { this.allSelectedIndexes = this.allSelectItems.map((_, idx) => !this.isAllSelected(idx) ? idx : undefined).filter(Boolean); }, isAllSelected(idx) { return this.allSelectedIndexes.includes(idx); } }, computed: { selectedCount() { return this.allSelectedIndexes.length; } } }); ``` 上述代码仅提供了基本的能力,还增加了方便的操作方式让用户更轻松地完成批量择的任务[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吴小花的博客

1分也是爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值