当前位置:首页 > VUE

vue实现select筛选功能

2026-01-21 18:13:31VUE

基础实现

使用Vue的v-model绑定select选中的值,结合v-for渲染选项列表。示例代码:

<template>
  <select v-model="selectedOption">
    <option 
      v-for="option in options" 
      :key="option.value" 
      :value="option.value"
    >
      {{ option.label }}
    </option>
  </select>
</template>

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

动态筛选功能

结合计算属性实现动态筛选。当选项数据来源于API或需要过滤时:

<template>
  <select v-model="selectedCategory">
    <option value="">全部</option>
    <option 
      v-for="cat in filteredCategories" 
      :key="cat.id" 
      :value="cat.id"
    >
      {{ cat.name }}
    </option>
  </select>
</template>

<script>
export default {
  data() {
    return {
      selectedCategory: '',
      allCategories: [
        { id: 1, name: '电子产品' },
        { id: 2, name: '家居用品' }
      ]
    }
  },
  computed: {
    filteredCategories() {
      return this.allCategories.filter(
        cat => cat.name.includes(this.searchText)
      )
    }
  }
}
</script>

多级联动筛选

实现省市区三级联动等复杂筛选:

<template>
  <select v-model="province" @change="loadCities">
    <option v-for="p in provinces" :value="p.id">{{ p.name }}</option>
  </select>

  <select v-model="city" :disabled="!province">
    <option v-for="c in cities" :value="c.id">{{ c.name }}</option>
  </select>
</template>

<script>
export default {
  data() {
    return {
      province: null,
      city: null,
      provinces: [],
      cities: []
    }
  },
  methods: {
    async loadCities() {
      this.cities = await fetchCities(this.province)
    }
  }
}
</script>

使用第三方组件

对于更复杂的需求,可以使用如Element UIel-select组件:

<template>
  <el-select 
    v-model="selectedTags" 
    multiple
    filterable
    placeholder="请选择标签"
  >
    <el-option
      v-for="tag in tags"
      :key="tag.value"
      :label="tag.label"
      :value="tag.value"
    />
  </el-select>
</template>

样式定制

通过CSS自定义select样式:

vue实现select筛选功能

select {
  padding: 8px 12px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  background-color: #fff;
}

select:focus {
  border-color: #409eff;
  outline: none;
}

注意事项

  • 移动端体验需考虑原生select的替代方案
  • 大量数据时建议添加虚拟滚动优化
  • 国际化场景需要处理多语言选项
  • 表单验证场景需配合vuelidate等验证库

标签: 功能vue
分享给朋友:

相关文章

vue实现https

vue实现https

Vue 中实现 HTTPS 的步骤 在 Vue 项目中实现 HTTPS 主要涉及开发环境配置和生产环境部署。以下是具体方法: 开发环境配置(Vue CLI 或 Vite) 使用 mkcert 生成…

vue实现bootstrap

vue实现bootstrap

Vue 中集成 Bootstrap 的方法 在 Vue 项目中集成 Bootstrap 可以通过以下方式实现,涵盖样式、组件和交互功能。 安装 Bootstrap 依赖 通过 npm 或 yarn…

vue实现同步

vue实现同步

Vue 实现同步的方法 在 Vue 中实现同步操作通常涉及数据绑定、状态管理和异步操作的处理。以下是几种常见的方法: 使用计算属性(Computed Properties) 计算属性基于响应式依赖进…

vue实现picker

vue实现picker

Vue 实现 Picker 组件的方法 在 Vue 中实现 Picker 组件可以通过多种方式完成,以下是几种常见的实现方法: 使用原生 HTML 和 CSS 实现 通过 Vue 的模板和样式绑定,…

vue 实现全屏

vue 实现全屏

Vue 实现全屏功能 在 Vue 中实现全屏功能可以通过浏览器提供的 Fullscreen API 完成。以下是一个完整的实现方案: 安装依赖(可选) 如果需要跨浏览器兼容性更好,可以安装…

vue底层实现

vue底层实现

Vue.js 的底层实现原理 Vue.js 是一个渐进式 JavaScript 框架,其核心实现主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是其关键实现细节: 响应式系统 Vue 2…