当前位置:首页 > VUE

vue 实现多选功能

2026-01-15 23:14:22VUE

Vue 实现多选功能的方法

使用 v-model 绑定数组

在 Vue 中,可以通过 v-model 绑定一个数组来实现多选功能。适用于复选框组(checkbox)或下拉多选(select multiple)。

<template>
  <div>
    <h3>选择你喜欢的编程语言:</h3>
    <label v-for="lang in languages" :key="lang">
      <input type="checkbox" :value="lang" v-model="selectedLanguages">
      {{ lang }}
    </label>
    <p>已选择:{{ selectedLanguages }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      languages: ['JavaScript', 'Python', 'Java', 'C++'],
      selectedLanguages: []
    };
  }
};
</script>

使用第三方组件库

对于更复杂的需求(如表格多选、标签式多选),可以使用第三方 UI 库:

vue 实现多选功能

  • Element UIel-checkbox-groupel-select 多选模式
  • Vuetifyv-checkboxv-select 多选
  • Ant Design Vuea-checkbox-groupa-select 多选

以 Element UI 为例:

vue 实现多选功能

<template>
  <el-checkbox-group v-model="selectedOptions">
    <el-checkbox v-for="option in options" :key="option" :label="option">
      {{ option }}
    </el-checkbox>
  </el-checkbox-group>
</template>

自定义多选逻辑

如果需要完全自定义逻辑(如点击 div 多选),可以通过事件和数组操作实现:

<template>
  <div>
    <div 
      v-for="item in items" 
      :key="item.id"
      @click="toggleSelection(item)"
      :class="{ 'selected': isSelected(item) }"
    >
      {{ item.name }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: '选项1' },
        { id: 2, name: '选项2' }
      ],
      selectedItems: []
    };
  },
  methods: {
    toggleSelection(item) {
      const index = this.selectedItems.findIndex(i => i.id === item.id);
      if (index >= 0) {
        this.selectedItems.splice(index, 1);
      } else {
        this.selectedItems.push(item);
      }
    },
    isSelected(item) {
      return this.selectedItems.some(i => i.id === item.id);
    }
  }
};
</script>

多选表格实现

结合表格组件实现行多选功能(以 Element UI 为例):

<template>
  <el-table
    ref="multipleTable"
    :data="tableData"
    @selection-change="handleSelectionChange"
  >
    <el-table-column type="selection" width="55"></el-table-column>
    <el-table-column prop="name" label="名称"></el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: '数据1' },
        { name: '数据2' }
      ],
      multipleSelection: []
    };
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }
  }
};
</script>

注意事项

  1. 使用 v-model 绑定时,初始值需设为空数组 [],否则可能导致非数组类型错误
  2. 对于动态渲染的选项,确保 :key 唯一性
  3. 多选下拉(<select multiple>)需按住 Ctrl/Command 键选择

标签: 多选功能
分享给朋友:

相关文章

vue的艾特功能实现

vue的艾特功能实现

Vue 的 @ 功能实现 在 Vue 中实现类似社交平台的 @ 功能,通常涉及输入框的监听、用户匹配和选择插入。以下是具体实现方法: 监听输入框内容 使用 v-model 绑定输入框内容,并通过 @…

vue实现功能

vue实现功能

Vue 功能实现方法 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现常见功能的几种方法: 数据绑定与响应式更新 在 Vue 中,数据绑定通过 v-mode…

vue实现按钮实现点击功能

vue实现按钮实现点击功能

Vue 按钮点击功能实现 在 Vue 中实现按钮点击功能可以通过多种方式完成,以下是常见的实现方法: 方法一:使用 v-on 指令 通过 v-on:click 或简写 @click 绑定事件:…

vue实现拨号功能

vue实现拨号功能

Vue实现拨号功能的方法 使用HTML5的tel协议 在Vue模板中直接使用<a>标签的tel:协议实现拨号功能。这种方法适用于移动端浏览器,点击后会调起系统的拨号界面。 <te…

vue 实现收藏功能

vue 实现收藏功能

实现收藏功能的基本思路 在Vue中实现收藏功能通常涉及前端交互与后端数据存储的结合。核心逻辑包括:用户点击收藏按钮时切换状态,并通过API将状态同步到后端数据库。 前端组件实现 创建收藏按钮组件,…

vue实现ping功能

vue实现ping功能

实现Ping功能的思路 在Vue中实现Ping功能通常需要借助浏览器API或后端服务。由于浏览器环境限制,无法直接发送ICMP请求(传统Ping协议),但可通过以下两种方式模拟: HTTP请求模拟…