当前位置:首页 > VUE

vue实现多选列表

2026-02-19 06:11:24VUE

Vue 实现多选列表的方法

基础实现

使用 v-model 绑定数组实现多选功能,结合 v-for 渲染列表项:

vue实现多选列表

<template>
  <div>
    <h3>选择你喜欢的项目:</h3>
    <div v-for="item in items" :key="item.id">
      <input 
        type="checkbox" 
        :id="item.id" 
        :value="item.value" 
        v-model="selectedItems"
      >
      <label :for="item.id">{{ item.label }}</label>
    </div>
    <p>已选择:{{ selectedItems }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 'opt1', value: 'vue', label: 'Vue.js' },
        { id: 'opt2', value: 'react', label: 'React' },
        { id: 'opt3', value: 'angular', label: 'Angular' }
      ],
      selectedItems: []
    }
  }
}
</script>

使用组件库

Element UI 的多选组件示例:

vue实现多选列表

<template>
  <el-checkbox-group v-model="checkedCities">
    <el-checkbox 
      v-for="city in cities" 
      :label="city" 
      :key="city"
    >{{ city }}</el-checkbox>
  </el-checkbox-group>
</template>

<script>
export default {
  data() {
    return {
      cities: ['上海', '北京', '广州', '深圳'],
      checkedCities: []
    }
  }
}
</script>

自定义样式多选

通过 CSS 和事件处理实现美观的多选效果:

<template>
  <div class="checkbox-list">
    <div 
      v-for="item in options"
      :key="item.value"
      class="checkbox-item"
      :class="{ active: selected.includes(item.value) }"
      @click="toggleSelection(item.value)"
    >
      {{ item.label }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      options: [
        { value: 1, label: '选项1' },
        { value: 2, label: '选项2' },
        { value: 3, label: '选项3' }
      ],
      selected: []
    }
  },
  methods: {
    toggleSelection(value) {
      const index = this.selected.indexOf(value)
      index === -1 
        ? this.selected.push(value) 
        : this.selected.splice(index, 1)
    }
  }
}
</script>

<style>
.checkbox-item {
  padding: 8px 12px;
  border: 1px solid #ddd;
  margin: 5px;
  cursor: pointer;
}
.checkbox-item.active {
  background-color: #409eff;
  color: white;
}
</style>

全选/反选功能

添加全选控制逻辑:

<template>
  <div>
    <input 
      type="checkbox" 
      id="selectAll" 
      v-model="allSelected"
      @change="toggleAll"
    >
    <label for="selectAll">全选</label>

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

<script>
export default {
  data() {
    return {
      list: [
        { id: 1, name: '项目A' },
        { id: 2, name: '项目B' },
        { id: 3, name: '项目C' }
      ],
      selected: [],
      allSelected: false
    }
  },
  methods: {
    toggleAll() {
      this.selected = this.allSelected 
        ? this.list.map(item => item.id) 
        : []
    }
  },
  watch: {
    selected(newVal) {
      this.allSelected = newVal.length === this.list.length
    }
  }
}
</script>

注意事项

  • 确保 v-model 绑定的是数组类型变量
  • 多选组件需要设置不同的 key 属性
  • 大数据量列表考虑使用虚拟滚动优化性能
  • 移动端注意触摸反馈和选择区域大小

以上方法可根据实际需求组合使用,Vue 的响应式特性使得多选状态管理变得简单高效。

标签: 多选列表
分享给朋友:

相关文章

jquery 列表

jquery 列表

jQuery 列表操作 jQuery 提供了多种方法来操作 HTML 列表(如 <ul> 或 <ol>)。以下是一些常见的操作方式: 动态添加列表项 使用 append()…

vue实现竖向列表

vue实现竖向列表

Vue 实现竖向列表的方法 使用 v-for 指令 通过 Vue 的 v-for 指令可以轻松渲染一个竖向列表。假设有一个数组 items,可以通过以下方式渲染: <template>…

vue实现单选多选

vue实现单选多选

Vue 实现单选功能 使用 v-model 绑定单选按钮的值,确保同一组单选按钮的 name 属性相同。 <template> <div> <label&g…

vue实现目录列表

vue实现目录列表

实现目录列表的基本思路 在Vue中实现目录列表通常涉及动态渲染嵌套结构的数据,结合递归组件或v-for循环处理层级关系。以下是两种常见实现方式: 方法一:使用递归组件 递归组件适合处理未知深度的嵌套…

vue实现多选搜索

vue实现多选搜索

Vue 多选搜索实现方法 使用 Element UI 的 Select 组件 Element UI 提供了带有搜索功能的多选组件,适合快速实现需求。 安装 Element UI: npm inst…

vue实现列表连线

vue实现列表连线

实现列表连线的基本思路 在Vue中实现列表连线效果,可以通过动态渲染DOM元素并结合CSS样式来完成。关键在于获取列表项的位置信息,并通过计算连线路径。 使用CSS和伪元素实现简单连线 对于简单的垂…