当前位置:首页 > 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 多选),提升用户体验。

<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 实现多选功能 在 Vue 中实现多选功能可以通过多种方式完成,以下是常见的几种方法: 使用 v-model 绑定数组 通过 v-model 绑定到一个数组,可以实现多选功能。适用于复选框组(…

vue实现搜索列表

vue实现搜索列表

Vue 实现搜索列表 在 Vue 中实现搜索列表功能,通常需要结合数据绑定、计算属性和事件监听。以下是一个完整的实现方案: 数据准备与模板结构 <template> <div…

vue实现WiFi列表

vue实现WiFi列表

获取WiFi列表的Vue实现 使用Vue获取WiFi列表需要结合浏览器API或后端服务,因为浏览器出于安全限制无法直接访问系统WiFi信息。以下是两种常见实现方式: 方法一:通过Web API获取…

vue实现多选答题

vue实现多选答题

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

网页制作css 列表

网页制作css 列表

创建CSS列表样式 使用CSS可以自定义HTML列表(有序列表<ol>和无序列表<ul>)的外观,包括项目符号、缩进、颜色等。以下是一些常见的列表样式方法: 基础列表样式…

vue实现无线滚动列表

vue实现无线滚动列表

无限滚动列表的实现方法 无限滚动列表通常用于展示大量数据,通过动态加载数据减少初始渲染压力。以下是基于Vue的实现方法: 使用Intersection Observer API监听滚动 Inters…