…">
当前位置:首页 > 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
分享给朋友:

相关文章

vue实现过程

vue实现过程

Vue 的实现过程 Vue 的实现过程可以分为多个核心模块,包括响应式系统、虚拟 DOM、模板编译等。以下是 Vue 的主要实现过程分析。 响应式系统 Vue 的响应式系统基于 Object.def…

vue实现签章

vue实现签章

Vue 实现签章功能 签章功能通常包括手写签名、电子印章等。以下是基于 Vue 的实现方法: 使用 canvas 实现手写签名 安装依赖(如需要): npm install signatu…

vue实现driver

vue实现driver

Vue 实现 Driver.js 引导功能 Driver.js 是一个轻量级的 JavaScript 库,用于在网页上创建引导式导览。以下是在 Vue 项目中集成 Driver.js 的详细方法:…

vue 实现图片

vue 实现图片

Vue 实现图片加载与展示的方法 在 Vue 中实现图片加载和展示可以通过多种方式完成,以下是一些常见的方法: 使用 v-bind 绑定图片路径 通过 v-bind(或简写为 :)动态绑定图片的…

vue  实现分页

vue 实现分页

实现分页的基本思路 在Vue中实现分页通常需要结合后端API返回的数据和前端的分页组件。分页的核心逻辑包括计算总页数、处理当前页码变化以及触发数据请求。 分页组件示例 使用Element UI的分页…

vue日历实现

vue日历实现

实现基础日历布局 使用Vue的模板语法构建日历的基本HTML结构。一个典型的日历包含星期标题和日期格子,可以通过v-for循环生成。月份切换功能通过计算属性动态更新日期数组。 <templat…