当前位置:首页 > VUE

vue实现点击选中

2026-01-19 09:16:54VUE

Vue 实现点击选中效果

使用 v-bind 和 v-on 实现基础选中

通过 v-bind:class 动态绑定样式类,结合 v-on:click 切换选中状态。在数据中定义 selected 变量控制选中状态:

<template>
  <div 
    :class="{ 'active': selected }" 
    @click="selected = !selected"
  >
    点击切换选中状态
  </div>
</template>

<script>
export default {
  data() {
    return {
      selected: false
    }
  }
}
</script>

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

列表项单选选中

对于列表场景,通过维护一个 selectedId 实现单选效果:

vue实现点击选中

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

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

列表项多选选中

使用数组存储多个选中项,通过 includes 方法判断是否选中:

vue实现点击选中

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

<script>
export default {
  data() {
    return {
      selectedIds: [],
      items: [
        { id: 1, name: '选项1' },
        { id: 2, name: '选项2' }
      ]
    }
  },
  methods: {
    toggleSelection(id) {
      const index = this.selectedIds.indexOf(id)
      index === -1 
        ? this.selectedIds.push(id) 
        : this.selectedIds.splice(index, 1)
    }
  }
}
</script>

使用计算属性优化

对于复杂选中逻辑,可以使用计算属性:

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

<script>
export default {
  computed: {
    getItemClass() {
      return (id) => ({
        'active': this.selectedIds.includes(id),
        'disabled': this.disabledIds.includes(id)
      })
    }
  }
}
</script>

结合 Vuex 管理状态

当选中状态需要跨组件共享时,使用 Vuex 存储选中状态:

// store.js
export default new Vuex.Store({
  state: {
    selectedItems: []
  },
  mutations: {
    TOGGLE_SELECTION(state, id) {
      const index = state.selectedItems.indexOf(id)
      index === -1 
        ? state.selectedItems.push(id) 
        : state.selectedItems.splice(index, 1)
    }
  }
})
<template>
  <div @click="$store.commit('TOGGLE_SELECTION', item.id)">
    {{ item.name }}
  </div>
</template>

这些方法覆盖了从基础到进阶的点击选中实现方案,可根据具体场景选择合适的方式。

标签: vue
分享给朋友:

相关文章

vue实现录音文件播放

vue实现录音文件播放

使用 Vue 实现录音文件播放 准备工作 确保项目中已安装必要的依赖,如 vue-audio-recorder 或原生 Web Audio API。录音文件播放通常涉及录音、存储和播放三个核心环节。…

vue 实现blog

vue 实现blog

Vue 实现博客的基本步骤 使用 Vue 实现博客可以分为前端和后端两部分,前端使用 Vue.js 框架,后端可以选择 Node.js、Python 或其他服务端语言。以下是一个基于 Vue 的博客实…

vue实现tablegrid

vue实现tablegrid

Vue 实现 TableGrid 的方法 使用 Element UI 的 Table 组件 Element UI 提供了强大的 Table 组件,可以快速实现表格布局。安装 Element UI 后,…

vue实现granfana

vue实现granfana

Vue 实现类似 Grafana 的仪表盘 要在 Vue 中实现类似 Grafana 的仪表盘功能,需要结合数据可视化库、状态管理和 UI 组件。以下是关键步骤和推荐工具: 数据可视化库 使用 E…

vue实现input

vue实现input

Vue 实现 Input 输入框的方法 基本双向绑定 使用 v-model 指令实现双向数据绑定,适用于大多数表单场景: <template> <input v-model="…

vue实现rtc

vue实现rtc

Vue 实现 RTC 的步骤 安装依赖 在 Vue 项目中安装 WebRTC 相关依赖,如 simple-peer 或 peerjs,用于简化 WebRTC 的实现。 运行以下命令安装 simple-…