当前位置:首页 > VUE

vue实现多选搜索

2026-01-18 17:49:53VUE

Vue 多选搜索实现方法

使用 Element UI 的 Select 组件

Element UI 提供了带有搜索功能的多选组件,适合快速实现需求。

安装 Element UI:

npm install element-ui

代码示例:

<template>
  <el-select
    v-model="selectedItems"
    multiple
    filterable
    placeholder="请搜索并选择"
    @change="handleChange">
    <el-option
      v-for="item in filteredOptions"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
</template>

<script>
export default {
  data() {
    return {
      selectedItems: [],
      options: [
        { value: '1', label: '选项1' },
        { value: '2', label: '选项2' }
      ],
      searchQuery: ''
    }
  },
  computed: {
    filteredOptions() {
      return this.options.filter(option => 
        option.label.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  },
  methods: {
    handleChange(value) {
      console.log(value)
    }
  }
}
</script>

自定义实现多选搜索

对于需要完全自定义的场景,可以结合 input 和 checkbox 实现。

<template>
  <div class="multi-select">
    <input 
      v-model="searchQuery"
      placeholder="搜索..."
      @input="filterOptions">

    <div class="options">
      <div 
        v-for="option in filteredOptions"
        :key="option.value"
        class="option">
        <input 
          type="checkbox"
          :id="option.value"
          :value="option.value"
          v-model="selectedItems">
        <label :for="option.value">{{ option.label }}</label>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      selectedItems: [],
      options: [
        { value: '1', label: '选项1' },
        { value: '2', label: '选项2' }
      ],
      filteredOptions: []
    }
  },
  created() {
    this.filteredOptions = [...this.options]
  },
  methods: {
    filterOptions() {
      this.filteredOptions = this.options.filter(option =>
        option.label.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  }
}
</script>

<style>
.multi-select {
  border: 1px solid #dcdfe6;
  padding: 10px;
  border-radius: 4px;
}
.options {
  max-height: 200px;
  overflow-y: auto;
}
.option {
  padding: 5px 0;
}
</style>

使用 Vue Multiselect 插件

vue-multiselect 是专门为 Vue 设计的强大多选组件。

安装:

npm install vue-multiselect

使用示例:

<template>
  <multiselect
    v-model="selected"
    :options="options"
    :multiple="true"
    :searchable="true"
    :close-on-select="false"
    placeholder="搜索并选择"
    label="label"
    track-by="value">
  </multiselect>
</template>

<script>
import Multiselect from 'vue-multiselect'

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

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

实现远程搜索

当需要从服务器获取选项时,可以使用防抖技术优化性能。

vue实现多选搜索

<script>
export default {
  methods: {
    fetchOptions: _.debounce(function(query) {
      axios.get('/api/options', { params: { q: query } })
        .then(response => {
          this.options = response.data
        })
    }, 500)
  },
  watch: {
    searchQuery(newVal) {
      this.fetchOptions(newVal)
    }
  }
}
</script>

注意事项

  • 大数据量情况下考虑虚拟滚动优化性能
  • 移动端需要特别注意触摸交互体验
  • 选中的项目需要有明确的视觉反馈
  • 考虑添加全选/清除全部等快捷操作

以上方法可根据实际项目需求选择使用,Element UI 方案适合快速开发,自定义方案灵活性最高,vue-multiselect 则提供了丰富的开箱即用功能。

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

相关文章

vue实现自动翻译

vue实现自动翻译

Vue 实现自动翻译的方法 在 Vue 项目中实现自动翻译功能可以通过多种方式完成,以下是几种常见的方法: 使用第三方翻译 API 注册并获取翻译 API 的密钥(如 Google Translat…

vue实现横向导航

vue实现横向导航

vue实现横向导航的方法 使用Flex布局实现横向导航 在Vue中可以通过Flex布局快速实现横向导航。创建一个组件,使用display: flex和flex-direction: row属性使导航项…

vue实现帖子

vue实现帖子

Vue 实现帖子功能 使用 Vue 实现帖子功能需要结合前端框架和后端数据交互,以下是实现的关键步骤和代码示例。 帖子列表展示 创建帖子列表组件,使用 v-for 渲染帖子数据,通过 axios 获…

vue实现groupbox

vue实现groupbox

Vue 实现 GroupBox 组件 在 Vue 中实现类似 GroupBox 的效果可以通过自定义组件完成。GroupBox 通常是一个带有标题的边框容器,用于将相关控件分组显示。 基本实现方法…

vue实现tap

vue实现tap

Vue 实现 Tap 事件 在移动端开发中,Tap 事件比 Click 事件更常用,因为 Click 事件有 300ms 延迟。以下是几种在 Vue 中实现 Tap 事件的方法。 使用第三方库 安装…

vue实现双向

vue实现双向

Vue 实现双向绑定的方法 Vue 的双向绑定主要通过 v-model 指令实现,它结合了数据绑定和事件监听,适用于表单元素(如 input、select、textarea 等)。以下是几种常见的实现…