…">
当前位置:首页 > VUE

vue实现多选下拉

2026-02-17 14:10:39VUE

vue实现多选下拉

Vue 多选下拉实现方法

使用原生 HTML 和 Vue 指令

<template>
  <select v-model="selectedOptions" multiple>
    <option v-for="option in options" :value="option.value">
      {{ option.text }}
    </option>
  </select>
</template>

<script>
export default {
  data() {
    return {
      selectedOptions: [],
      options: [
        { value: '1', text: '选项1' },
        { value: '2', text: '选项2' },
        { value: '3', text: '选项3' }
      ]
    }
  }
}
</script>

使用 Element UI 组件库

<template>
  <el-select v-model="selectedOptions" multiple placeholder="请选择">
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
</template>

<script>
export default {
  data() {
    return {
      selectedOptions: [],
      options: [{
        value: '1',
        label: '选项1'
      }, {
        value: '2',
        label: '选项2'
      }, {
        value: '3',
        label: '选项3'
      }]
    }
  }
}
</script>

使用 Vue Multiselect 插件

安装插件:

vue实现多选下拉

npm install vue-multiselect

使用示例:

<template>
  <multiselect
    v-model="selectedOptions"
    :options="options"
    :multiple="true"
    :close-on-select="false"
    placeholder="选择项目"
    label="text"
    track-by="value">
  </multiselect>
</template>

<script>
import Multiselect from 'vue-multiselect'

export default {
  components: { Multiselect },
  data() {
    return {
      selectedOptions: [],
      options: [
        { value: '1', text: '选项1' },
        { value: '2', text: '选项2' },
        { value: '3', text: '选项3' }
      ]
    }
  }
}
</script>

自定义多选下拉组件

<template>
  <div class="custom-multiselect">
    <div class="selected-options" @click="toggleDropdown">
      <span v-for="(option, index) in selectedOptions" :key="index">
        {{ getOptionText(option) }}
        <span @click.stop="removeOption(index)">×</span>
      </span>
    </div>
    <div class="dropdown" v-show="isOpen">
      <div
        v-for="(option, index) in options"
        :key="index"
        @click="selectOption(option)"
        :class="{ selected: isSelected(option) }">
        {{ option.text }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    options: Array,
    value: Array
  },
  data() {
    return {
      isOpen: false,
      selectedOptions: this.value || []
    }
  },
  methods: {
    toggleDropdown() {
      this.isOpen = !this.isOpen
    },
    selectOption(option) {
      if (!this.isSelected(option)) {
        this.selectedOptions.push(option.value)
      } else {
        this.selectedOptions = this.selectedOptions.filter(
          val => val !== option.value
        )
      }
      this.$emit('input', this.selectedOptions)
    },
    removeOption(index) {
      this.selectedOptions.splice(index, 1)
      this.$emit('input', this.selectedOptions)
    },
    isSelected(option) {
      return this.selectedOptions.includes(option.value)
    },
    getOptionText(value) {
      const option = this.options.find(opt => opt.value === value)
      return option ? option.text : ''
    }
  }
}
</script>

注意事项

  • 原生 HTML select 多选样式有限,可能需要额外 CSS 美化
  • 组件库方案通常提供更丰富的功能和更好的用户体验
  • 自定义组件需要考虑键盘交互、无障碍访问等细节
  • 多选数据绑定使用数组类型 v-model
  • 大数据量时需考虑性能优化,如虚拟滚动

标签: 多选vue
分享给朋友:

相关文章

ssh vue 实现

ssh vue 实现

使用SSH连接Vue项目部署的服务器 在Vue项目开发或部署过程中,可能需要通过SSH连接到远程服务器进行配置或维护。以下是实现SSH连接并部署Vue项目的关键步骤: 生成SSH密钥对 在本地终端运…

通过vue实现

通过vue实现

Vue 实现方法 安装 Vue 确保已安装 Node.js 和 npm,通过以下命令安装 Vue CLI: npm install -g @vue/cli 创建 Vue 项目 使用 Vue CLI…

vue实现接口

vue实现接口

在 Vue 中实现接口请求 Vue 本身不直接提供 HTTP 请求功能,通常需要借助第三方库如 axios 或 fetch 来实现接口请求。以下是几种常见方法: 使用 axios 发送请求 安装 a…

vue实现答题

vue实现答题

Vue 实现答题功能 数据结构设计 使用 Vue 的响应式特性管理题目和答案数据。通常需要以下数据结构: data() { return { questions: [ {…

lua 实现vue

lua 实现vue

在 Lua 中实现类似 Vue 的响应式框架需要模拟 Vue 的核心功能,包括数据绑定、模板渲染和依赖追踪。以下是实现的关键思路和代码示例: 响应式数据绑定 使用 Lua 的 __index 和 _…

vue删除实现

vue删除实现

Vue 删除功能的实现方法 在 Vue 中实现删除功能通常涉及以下几个步骤: 方法一:使用数组的 splice 方法 通过索引删除数组中的元素,适用于列表渲染的场景。 methods: {…