当前位置:首页 > 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>

自定义多选逻辑

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

<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),可直接调用其多选组件。

以 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 多选),提升用户体验。

vue 实现列表多选

<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实现悬浮列表

Vue 实现悬浮列表的方法 使用 CSS 固定定位 通过 CSS 的 position: fixed 属性实现悬浮效果。在 Vue 模板中,为列表容器添加固定定位样式,并设置 top、left 等属…

vue实现文本列表

vue实现文本列表

Vue 实现文本列表的方法 Vue 可以通过多种方式实现文本列表的渲染和管理,以下是几种常见的方法: 使用 v-for 指令渲染列表 通过 v-for 指令可以动态渲染数组或对象中的文本列表。 &…

vue实现列表页面

vue实现列表页面

Vue 列表页面实现方案 基础列表渲染 使用 v-for 指令渲染数组数据,配合 :key 提升性能: <template> <ul> <li v-for=…

vue实现图片列表

vue实现图片列表

实现图片列表的基本方法 在Vue中实现图片列表可以通过v-for指令遍历图片数据数组,结合<img>标签动态绑定src属性。以下是基本实现步骤: <template> &…

vue实现点选多选

vue实现点选多选

Vue 实现点选多选功能 基础实现(基于数组存储选中项) 使用 v-model 绑定到数组实现多选,适用于复选框组场景: <template> <div> <…

vue实现题目列表

vue实现题目列表

Vue实现题目列表的方法 数据准备 在Vue中实现题目列表需要先准备题目数据。通常将题目数据存储在组件的data或通过API从后端获取。示例数据结构如下: data() { return {…