当前位置:首页 > VUE

vue 实现列表多选

2026-01-16 01:49:56VUE

Vue 实现列表多选的方法

使用 v-model 绑定数组

通过 v-model 绑定一个数组,可以自动处理多选逻辑。适用于复选框(checkbox)场景。

<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, label: '选项1', value: 'value1' },
        { id: 2, label: '选项2', value: 'value2' },
        { id: 3, label: '选项3', value: 'value3' },
      ],
      selectedItems: []
    };
  }
};
</script>

自定义多选逻辑

如果需要更复杂的交互(如点击行选中),可以通过手动管理选中状态实现。

vue 实现列表多选

<template>
  <div>
    <div 
      v-for="item in items" 
      :key="item.id" 
      @click="toggleSelect(item)"
      :class="{ 'selected': selectedItems.includes(item.id) }"
    >
      {{ item.label }}
    </div>
  </div>
</template>

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

<style>
.selected {
  background-color: #f0f0f0;
}
</style>

使用第三方组件库

若项目中使用 UI 库(如 Element UI、Ant Design Vue),可直接调用其多选组件。

vue 实现列表多选

以 Element UI 为例:

<template>
  <el-checkbox-group v-model="selectedItems">
    <el-checkbox 
      v-for="item in items" 
      :key="item.id" 
      :label="item.value"
    >
      {{ item.label }}
    </el-checkbox>
  </el-checkbox-group>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, label: '选项1', value: 'value1' },
        { id: 2, label: '选项2', value: 'value2' },
        { id: 3, label: '选项3', value: 'value3' },
      ],
      selectedItems: []
    };
  }
};
</script>

键盘交互增强

通过监听键盘事件(如 Shift 多选),提升用户体验。

<template>
  <div>
    <div 
      v-for="(item, index) in items" 
      :key="item.id" 
      @click="handleClick(index, $event)"
      :class="{ 'selected': selectedItems.includes(item.id) }"
    >
      {{ item.label }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, label: '选项1' },
        { id: 2, label: '选项2' },
        { id: 3, label: '选项3' },
      ],
      selectedItems: [],
      lastSelectedIndex: null
    };
  },
  methods: {
    handleClick(index, event) {
      if (event.shiftKey && this.lastSelectedIndex !== null) {
        const start = Math.min(index, this.lastSelectedIndex);
        const end = Math.max(index, this.lastSelectedIndex);
        this.selectedItems = this.items
          .slice(start, end + 1)
          .map(item => item.id);
      } else {
        this.toggleSelect(this.items[index]);
        this.lastSelectedIndex = index;
      }
    },
    toggleSelect(item) {
      const index = this.selectedItems.indexOf(item.id);
      if (index === -1) {
        this.selectedItems.push(item.id);
      } else {
        this.selectedItems.splice(index, 1);
      }
    }
  }
};
</script>

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

相关文章

vue实现考试多选功能

vue实现考试多选功能

实现考试多选功能的方法 数据绑定与选项渲染 使用v-model绑定用户选择的答案数组,配合v-for渲染选项列表: <template> <div v-for="(option…

vue实现列表循环

vue实现列表循环

Vue 列表循环的实现方法 在 Vue 中,可以通过 v-for 指令实现列表循环渲染。以下是几种常见的实现方式: 基础列表渲染 <template> <ul>…

vue实现多选答题

vue实现多选答题

Vue 实现多选答题功能 实现多选答题功能需要结合 Vue 的数据绑定和事件处理机制,以下是一个完整的实现方案: 数据结构设计 data() { return { questions:…

vue 实现列表

vue 实现列表

Vue 实现列表的方法 在 Vue 中实现列表通常使用 v-for 指令,结合数组或对象进行渲染。以下是几种常见的实现方式: 基础列表渲染 通过 v-for 指令遍历数组,动态生成列表项。 &…

vue实现列表添加

vue实现列表添加

使用 Vue 实现列表添加功能 数据绑定与列表渲染 在 Vue 中,通过 v-for 指令可以轻松实现列表渲染。首先需要定义一个数组来存储列表数据,并在模板中使用 v-for 循环渲染。 <t…

vue图片实现多选

vue图片实现多选

Vue 图片多选实现方法 在 Vue 中实现图片多选功能可以通过以下几种方式完成,具体取决于项目需求和复杂度。 使用 v-model 和数组存储选中状态 通过 v-model 绑定一个数组来记录选中…