Vue button多选单选问题(点击选中可改变样式)

本文介绍如何在Vue中使用三元表达式实现按钮的多选和单选功能,点击按钮时能根据选中状态改变按钮的样式。

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

单选选中改变样式
做出如上效果

利用:class="item.ischeck == true ? 'btn_selected' : ''"三元表达式判断是否选中并改变样式

代码片段如下:


<div class="intent_btn">
  <van-button style="width: .90rem;" v-for="(item, index) in List" :key="index" :class="item.ischeck == true ? 'btn_selected' : ''"
  size="medium" @click="buttonSelectOne(index, item, List)">{
   {
   item.name}}</van-button>
</div>

​data:{
   
  List:[
    {
   val: 1, ischeck: false, name: "1"},
    {
   val: 2, ischeck
### 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值