当前位置:首页 > VUE

vue实现多选搜索

2026-01-18 17:49:53VUE

Vue 多选搜索实现方法

使用 Element UI 的 Select 组件

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

安装 Element UI:

npm install element-ui

代码示例:

vue实现多选搜索

<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 设计的强大多选组件。

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>

实现远程搜索

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

<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方案 使用WebRTC技术结合Vue框架可以构建多人视频通话应用。以下是核心实现方法: 技术栈选择 Vue 2/3作为前端框架 WebRTC用于实时通信 Socket.io…

vue公共列表的实现

vue公共列表的实现

实现 Vue 公共列表组件 公共列表组件通常用于展示重复结构的数据,例如商品列表、用户列表等。以下是一个典型的实现方式: 基础实现 创建可复用的列表组件 List.vue: <tem…

vue异步组建实现原理

vue异步组建实现原理

Vue 异步组件实现原理 Vue 的异步组件允许将组件按需加载,通常用于代码分割或延迟加载非关键组件。其核心原理基于 Promise 和动态导入。 基本实现方式 通过 defineAsyncComp…

vue实现双折线图

vue实现双折线图

实现双折线图的步骤 安装必要的依赖库(如 ECharts 或 Chart.js),这里以 ECharts 为例: npm install echarts --save 在 Vue 组件中引入 EC…

vue里实现增删改查

vue里实现增删改查

在Vue中实现增删改查 数据初始化 在Vue组件的data中定义初始数据和表单绑定的变量: data() { return { items: [ { id: 1, name…

vue实现方法

vue实现方法

Vue 实现方法 Vue 是一种流行的前端框架,用于构建用户界面和单页应用。以下是几种常见的 Vue 实现方法: 创建 Vue 实例 通过 new Vue() 创建一个 Vue 实例,传入配置对象…