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

vue实现多选下拉

2026-02-17 14:10:39VUE

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 插件

安装插件:

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实现报表的方法 使用Vue实现报表通常需要结合图表库或表格组件。以下是几种常见方法: 使用ECharts 安装ECharts库: npm install echarts vue-echart…

vue实现类别

vue实现类别

Vue 实现类别的几种方法 在 Vue 中实现类别功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-bind:class 动态绑定类名 通过对象语法或数组语法动态绑定类名,根据条件添加或…

vue实现setinterval

vue实现setinterval

在 Vue 中使用 setInterval Vue 中可以通过生命周期钩子和方法结合 setInterval 实现定时任务。以下是一个完整的实现示例: <template> <…

vue实现route

vue实现route

Vue 路由实现方法 Vue 中实现路由通常使用 vue-router 库,这是 Vue 官方推荐的路由管理器。以下是具体实现步骤: 安装 vue-router 通过 npm 或 yarn 安…

vue 实现树状

vue 实现树状

Vue 实现树状结构的方法 递归组件实现树状结构 递归组件是 Vue 中实现树状结构的常见方法。通过组件自身调用自身,可以轻松构建多层级树状结构。 <template> <u…

vue实现treeselect

vue实现treeselect

Vue TreeSelect 实现方法 使用 Vue 实现 TreeSelect 组件可以通过以下几种方式完成,包括使用现成的第三方库或自行封装组件。 使用第三方库 推荐使用成熟的第三方 Tree…