当前位置:首页 > VUE

vue实现active多选

2026-03-10 08:50:00VUE

Vue实现多选active效果

在Vue中实现多选active效果,可以通过以下方法完成:

数据驱动方式

使用数组存储选中的项,通过v-for循环渲染列表项:

<template>
  <div>
    <div 
      v-for="(item, index) in items" 
      :key="index"
      @click="toggleSelect(index)"
      :class="{ active: selectedIndices.includes(index) }"
    >
      {{ item }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: ['选项1', '选项2', '选项3', '选项4'],
      selectedIndices: []
    }
  },
  methods: {
    toggleSelect(index) {
      const i = this.selectedIndices.indexOf(index)
      if (i > -1) {
        this.selectedIndices.splice(i, 1)
      } else {
        this.selectedIndices.push(index)
      }
    }
  }
}
</script>

<style>
.active {
  background-color: #42b983;
  color: white;
}
</style>

对象存储方式

对于更复杂的数据结构,可以使用对象存储选中状态:

<template>
  <div>
    <div 
      v-for="item in items" 
      :key="item.id"
      @click="toggleSelect(item.id)"
      :class="{ active: selectedItems[item.id] }"
    >
      {{ item.name }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: '选项1' },
        { id: 2, name: '选项2' },
        { id: 3, name: '选项3' }
      ],
      selectedItems: {}
    }
  },
  methods: {
    toggleSelect(id) {
      this.$set(this.selectedItems, id, !this.selectedItems[id])
    }
  }
}
</script>

使用计算属性

对于需要频繁判断选中状态的场景,可以使用计算属性:

<template>
  <div>
    <div 
      v-for="item in items" 
      :key="item.id"
      @click="toggleSelect(item)"
      :class="{ active: isSelected(item) }"
    >
      {{ item.name }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: '选项1' },
        { id: 2, name: '选项2' }
      ],
      selectedItems: []
    }
  },
  computed: {
    isSelected() {
      return (item) => this.selectedItems.some(selected => selected.id === item.id)
    }
  },
  methods: {
    toggleSelect(item) {
      const index = this.selectedItems.findIndex(selected => selected.id === item.id)
      if (index > -1) {
        this.selectedItems.splice(index, 1)
      } else {
        this.selectedItems.push(item)
      }
    }
  }
}
</script>

使用v-model和复选框

如果需要与表单提交配合,可以使用复选框实现多选:

vue实现active多选

<template>
  <div>
    <div v-for="item in items" :key="item.id">
      <input 
        type="checkbox" 
        :id="'item-' + item.id" 
        :value="item.id" 
        v-model="selectedItems"
      >
      <label :for="'item-' + item.id">{{ item.name }}</label>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: '选项1' },
        { id: 2, name: '选项2' }
      ],
      selectedItems: []
    }
  }
}
</script>

以上方法都可以实现多选active效果,根据具体需求选择最适合的方案。数据驱动的方式更符合Vue的设计理念,推荐优先考虑。

标签: 多选vue
分享给朋友:

相关文章

vue实现放大效果

vue实现放大效果

使用 CSS transform 实现放大效果 在 Vue 中实现放大效果可以通过 CSS 的 transform: scale() 属性。这种方法简单高效,适合大多数交互场景。 定义一个 CSS…

vue请求实现进度条

vue请求实现进度条

实现请求进度条的方法 在Vue中实现请求进度条可以通过多种方式完成,常见的有使用第三方库如axios结合nprogress或自定义进度条组件。以下是几种实现方法: 使用axios和nprogress…

vue实现setinterval

vue实现setinterval

在 Vue 中使用 setInterval Vue 中可以通过生命周期钩子和方法结合 setInterval 实现定时任务。以下是一个完整的实现示例: <template> <…

vue实现token

vue实现token

Vue 中实现 Token 认证的方法 在 Vue 中实现 Token 认证通常涉及前端与后端的交互,包括 Token 的获取、存储、发送和刷新等操作。以下是具体实现步骤: 获取 Token 用户登…

vue实现erp

vue实现erp

Vue实现ERP系统的关键步骤 技术选型与架构设计 采用Vue 3 + TypeScript构建前端,搭配Pinia状态管理,Element Plus或Ant Design Vue作为UI框架。后端可…

jquery实现vue

jquery实现vue

jQuery 和 Vue.js 是两种不同理念的前端工具,jQuery 以 DOM 操作和事件驱动为核心,而 Vue.js 是数据驱动的现代化框架。如果想用 jQuery 实现类似 Vue 的功能(如…