当前位置:首页 > VUE

vue实现multiselect效果

2026-02-19 21:14:24VUE

Vue 实现多选(Multiselect)效果

使用第三方库 vue-multiselect

安装依赖包:

npm install vue-multiselect

基础实现代码:

<template>
  <div>
    <multiselect
      v-model="selected"
      :options="options"
      :multiple="true"
      :close-on-select="false"
      placeholder="选择项"
      label="name"
      track-by="name"
    ></multiselect>
  </div>
</template>

<script>
import Multiselect from 'vue-multiselect'
export default {
  components: { Multiselect },
  data() {
    return {
      selected: [],
      options: [
        { name: '选项1' },
        { name: '选项2' },
        { name: '选项3' }
      ]
    }
  }
}
</script>

<style src="vue-multiselect/dist/vue-multiselect.min.css"></style>

自定义实现多选组件

基础HTML结构:

<template>
  <div class="custom-multiselect">
    <div class="selected-items">
      <span v-for="(item, index) in selected" :key="index">
        {{ item }}
        <button @click="removeItem(index)">×</button>
      </span>
    </div>
    <input 
      type="text" 
      v-model="search"
      @focus="showOptions = true"
      @blur="handleBlur"
      @keydown.enter="addItem"
    />
    <ul v-show="showOptions">
      <li 
        v-for="(option, index) in filteredOptions" 
        :key="index"
        @mousedown="selectItem(option)"
      >
        {{ option }}
      </li>
    </ul>
  </div>
</template>

JavaScript逻辑:

<script>
export default {
  data() {
    return {
      selected: [],
      options: ['选项1', '选项2', '选项3', '选项4'],
      search: '',
      showOptions: false
    }
  },
  computed: {
    filteredOptions() {
      return this.options.filter(option => 
        option.toLowerCase().includes(this.search.toLowerCase()) &&
        !this.selected.includes(option)
      )
    }
  },
  methods: {
    selectItem(item) {
      if (!this.selected.includes(item)) {
        this.selected.push(item)
      }
      this.search = ''
      this.showOptions = false
    },
    removeItem(index) {
      this.selected.splice(index, 1)
    },
    addItem() {
      if (this.search && !this.selected.includes(this.search)) {
        this.selected.push(this.search)
        this.search = ''
      }
    },
    handleBlur() {
      setTimeout(() => {
        this.showOptions = false
      }, 200)
    }
  }
}
</script>

基础样式:

.custom-multiselect {
  position: relative;
  width: 300px;
}
.selected-items {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin-bottom: 5px;
}
.selected-items span {
  background: #eee;
  padding: 2px 5px;
  border-radius: 3px;
  display: flex;
  align-items: center;
}
.selected-items button {
  margin-left: 5px;
  background: none;
  border: none;
  cursor: pointer;
}
input {
  width: 100%;
  padding: 8px;
  box-sizing: border-box;
}
ul {
  position: absolute;
  width: 100%;
  margin: 0;
  padding: 0;
  list-style: none;
  background: white;
  border: 1px solid #ddd;
  max-height: 200px;
  overflow-y: auto;
  z-index: 100;
}
li {
  padding: 8px;
  cursor: pointer;
}
li:hover {
  background: #f5f5f5;
}

功能增强建议

添加搜索高亮功能:

// 在methods中添加
highlightText(text) {
  if (!this.search) return text
  const regex = new RegExp(this.search, 'gi')
  return text.replace(regex, match => `<span class="highlight">${match}</span>`)
}

添加分组支持:

options: [
  {
    group: '水果',
    items: ['苹果', '香蕉', '橙子']
  },
  {
    group: '蔬菜',
    items: ['胡萝卜', '番茄', '黄瓜']
  }
]

添加远程搜索功能:

async searchItems(query) {
  if (query.length < 2) return
  const response = await axios.get('/api/search', { params: { q: query } })
  this.options = response.data
}

vue实现multiselect效果

标签: 效果vue
分享给朋友:

相关文章

antd vue 实现

antd vue 实现

使用 Ant Design Vue 实现功能 Ant Design Vue 是基于 Vue.js 的 UI 组件库,提供了丰富的组件和设计规范。以下是一些常见功能的实现方法。 安装 Ant Desi…

vue实现发帖

vue实现发帖

使用 Vue 实现发帖功能 前端实现 创建发帖表单组件,包含标题和内容输入框,以及提交按钮。使用 v-model 绑定表单数据。 <template> <div>…

vue实现追加

vue实现追加

追加数据到数组或列表 在Vue中追加数据到数组或列表,可以通过push方法或concat方法实现。以下是几种常见的实现方式: 方法一:使用push方法 this.items.push(newI…

vue实现treetable

vue实现treetable

Vue实现TreeTable的方法 使用第三方组件库(如Element UI) Element UI的el-table组件支持树形表格展示,通过设置row-key和tree-props属性即可实现。…

vue rooter 实现原理

vue rooter 实现原理

Vue Router 实现原理 Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。其核心实现原理包括路由匹配、组件渲染和导航守卫等机制。 路由匹配与动态路由 V…

vue  怎么实现双击

vue 怎么实现双击

实现双击事件的方法 在 Vue 中实现双击事件可以通过 @dblclick 指令或自定义逻辑实现。以下是两种常见方法: 使用 @dblclick 指令 Vue 提供了原生 @dblclick 指令…