当前位置:首页 > VUE

vue 实现列表多选

2026-03-29 02:06:38VUE

实现列表多选的方法

使用 v-model 绑定数组

通过 v-model 绑定一个数组,当复选框被选中时,对应的值会自动添加到数组中。

<template>
  <div>
    <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>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: '1', value: 'apple', label: 'Apple' },
        { id: '2', value: 'banana', label: 'Banana' },
        { id: '3', value: 'orange', label: 'Orange' }
      ],
      selectedItems: []
    };
  }
};
</script>

使用 v-for 动态渲染列表

结合 v-for 动态渲染复选框列表,确保每个复选框的值正确绑定到数组中。

<template>
  <div>
    <div v-for="(item, index) in items" :key="index">
      <input type="checkbox" :value="item" v-model="selectedItems">
      <label>{{ item }}</label>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: ['Apple', 'Banana', 'Orange'],
      selectedItems: []
    };
  }
};
</script>

自定义多选组件

封装一个可复用的多选组件,支持更复杂的需求,如全选、反选等。

<template>
  <div>
    <div>
      <input type="checkbox" v-model="selectAll" @change="toggleSelectAll">
      <label>Select All</label>
    </div>
    <div v-for="item in items" :key="item.id">
      <input type="checkbox" :value="item" v-model="selectedItems">
      <label>{{ item.label }}</label>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: '1', label: 'Apple' },
        { id: '2', label: 'Banana' },
        { id: '3', label: 'Orange' }
      ],
      selectedItems: []
    };
  },
  computed: {
    selectAll: {
      get() {
        return this.selectedItems.length === this.items.length;
      },
      set(value) {
        this.selectedItems = value ? [...this.items] : [];
      }
    }
  },
  methods: {
    toggleSelectAll() {
      this.selectAll = !this.selectAll;
    }
  }
};
</script>

使用第三方库

对于更复杂的需求,可以使用第三方库如 vue-multiselect,提供更丰富的功能。

vue 实现列表多选

<template>
  <div>
    <multiselect
      v-model="selectedItems"
      :options="items"
      :multiple="true"
      :close-on-select="false"
      placeholder="Select items"
      label="label"
      track-by="id"
    ></multiselect>
  </div>
</template>

<script>
import Multiselect from 'vue-multiselect';

export default {
  components: { Multiselect },
  data() {
    return {
      items: [
        { id: '1', label: 'Apple' },
        { id: '2', label: 'Banana' },
        { id: '3', label: 'Orange' }
      ],
      selectedItems: []
    };
  }
};
</script>

注意事项

  • 确保每个复选框的 value 唯一,避免绑定冲突。
  • 使用 v-model 绑定时,初始值应为空数组。
  • 动态渲染列表时,为每个项添加唯一的 key 属性以提升性能。

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

相关文章

vue实现传输列表

vue实现传输列表

Vue 实现传输列表 在 Vue 中实现传输列表功能,通常涉及两个列表之间的数据传递,用户可以通过按钮或拖拽操作将项目从一个列表移动到另一个列表。以下是实现传输列表的几种方法。 使用组件间数据传递…

vue实现多选下拉

vue实现多选下拉

Vue 实现多选下拉 使用 Vue 实现多选下拉可以通过原生 HTML <select> 元素结合 Vue 的双向绑定,或者使用第三方组件库如 Element UI、Vuetify 等。以…

vue列表展开实现

vue列表展开实现

Vue 列表展开实现方法 使用 v-for 和 v-show 控制展开状态 通过 v-for 渲染列表项,配合 v-show 控制子内容的显示隐藏。需要为每个列表项维护一个展开状态。 <tem…

vue实现虚拟列表

vue实现虚拟列表

虚拟列表的概念 虚拟列表(Virtual List)是一种优化长列表渲染性能的技术,通过仅渲染可视区域内的元素,减少DOM节点数量,从而提升页面性能。适用于数据量大的场景(如表格、下拉选择器等)。…

vue实现伸缩列表

vue实现伸缩列表

Vue 实现伸缩列表的方法 使用 Vue 实现伸缩列表可以通过动态绑定样式或类名,结合过渡效果实现平滑的展开和收起动画。以下是几种常见实现方式: 动态绑定高度 通过 v-if 或 v-show 控制…

vue实现目录列表

vue实现目录列表

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