当前位置:首页 > VUE

vue 实现列表多选

2026-03-29 02:06:38VUE

实现列表多选的方法

使用 v-model 绑定数组

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

vue 实现列表多选

<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 动态渲染复选框列表,确保每个复选框的值正确绑定到数组中。

vue 实现列表多选

<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,提供更丰富的功能。

<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中实现多选答题功能,可以通过以下方法完成: 数据准备 定义一个questions数组,每个问题包含题目和选项,选项是一个对象数组,每个选项有文本和是否被选中的状态。 da…

vue 实现列表多选

vue 实现列表多选

Vue 实现列表多选的方法 使用 v-model 绑定数组 通过 v-model 绑定一个数组,可以自动处理多选逻辑。适用于复选框(checkbox)场景。 <template> &…

vue实现列表全选

vue实现列表全选

实现全选功能的基本思路 在Vue中实现列表全选功能,通常需要结合v-model和计算属性来管理选中状态。全选按钮的状态与列表中所有项的选中状态相互关联。 基础实现方法 准备一个数据列表和对应的选中状…

vue实现竖向列表

vue实现竖向列表

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

vue实现多选删除

vue实现多选删除

实现多选删除功能 在Vue中实现多选删除功能通常涉及以下几个关键步骤:数据绑定、选择状态管理、删除逻辑处理。以下是具体实现方法: 数据准备与渲染 创建包含选择状态的数据结构,通常是一个对象数组,每个…

vue实现列表点击

vue实现列表点击

Vue 实现列表点击的方法 在 Vue 中实现列表点击功能通常涉及 v-for 渲染列表和 v-on(或 @)绑定点击事件。以下是几种常见实现方式: 基础列表点击 使用 v-for 渲染列表,并通过…