当前位置:首页 > 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实现列表添加

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

jquery列表

jquery列表

jQuery 列表操作 jQuery 提供了多种方法来操作 HTML 列表(如 <ul> 或 <ol>),包括动态添加、删除、修改列表项,以及事件绑定等。 创建列表 使用 j…

vue图片实现多选

vue图片实现多选

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

vue循环实现多选

vue循环实现多选

Vue循环实现多选的方法 在Vue中实现多选功能,可以通过v-for循环渲染选项,结合v-model绑定数组来记录选中的值。以下是具体实现方式: 基础实现方案 数据准备 在组件的data中定义选项…

vue实现列表分页

vue实现列表分页

Vue 列表分页实现方法 基础分页实现 安装依赖(如使用第三方库) npm install vue-paginate 模板部分示例 <template> <div>…

vue实现列表过渡

vue实现列表过渡

vue实现列表过渡的方法 在Vue中,可以使用内置的<transition-group>组件来实现列表项的过渡效果。该组件专门为动态列表设计,能够处理元素的添加、删除和顺序变化。…