当前位置:首页 > VUE

vue实现分类筛选

2026-01-16 18:43:52VUE

Vue 实现分类筛选的方法

数据准备

在 Vue 组件中定义分类数据和筛选条件。通常使用 datacomputed 属性管理数据。

data() {
  return {
    products: [
      { id: 1, name: 'Product A', category: 'Electronics', price: 200 },
      { id: 2, name: 'Product B', category: 'Clothing', price: 50 },
      { id: 3, name: 'Product C', category: 'Electronics', price: 150 }
    ],
    selectedCategory: '',
    priceRange: [0, 1000]
  }
}

筛选逻辑实现

使用 computed 属性动态计算筛选结果,避免直接修改原始数据。

computed: {
  filteredProducts() {
    return this.products.filter(product => {
      const matchesCategory = 
        !this.selectedCategory || 
        product.category === this.selectedCategory
      const matchesPrice = 
        product.price >= this.priceRange[0] && 
        product.price <= this.priceRange[1]
      return matchesCategory && matchesPrice
    })
  }
}

模板绑定

在模板中使用 v-model 绑定筛选条件,展示筛选结果。

<div>
  <select v-model="selectedCategory">
    <option value="">All Categories</option>
    <option v-for="cat in categories" :value="cat">{{ cat }}</option>
  </select>

  <input type="range" v-model="priceRange[0]" min="0" max="1000">
  <input type="range" v-model="priceRange[1]" min="0" max="1000">

  <ul>
    <li v-for="product in filteredProducts" :key="product.id">
      {{ product.name }} - {{ product.category }} (${{ product.price }})
    </li>
  </ul>
</div>

动态分类列表

提取唯一分类列表作为计算属性,避免硬编码。

computed: {
  categories() {
    return [...new Set(this.products.map(p => p.category))]
  }
}

多条件筛选扩展

对于更复杂的筛选需求,可以扩展筛选逻辑。

computed: {
  filteredProducts() {
    return this.products.filter(product => {
      // 分类筛选
      const categoryMatch = 
        !this.filters.category || 
        product.category === this.filters.category

      // 价格筛选
      const priceMatch = 
        product.price >= this.filters.minPrice && 
        product.price <= this.filters.maxPrice

      // 搜索关键词筛选
      const searchMatch = 
        !this.filters.search || 
        product.name.toLowerCase().includes(this.filters.search.toLowerCase())

      return categoryMatch && priceMatch && searchMatch
    })
  }
}

使用 Vuex 管理状态

对于大型应用,可以使用 Vuex 集中管理筛选状态。

// store.js
state: {
  products: [...],
  filters: {
    category: '',
    minPrice: 0,
    maxPrice: 1000,
    search: ''
  }
},
getters: {
  filteredProducts: state => {
    return state.products.filter(product => {
      // 筛选逻辑
    })
  }
}

性能优化

对于大数据量场景,可以考虑以下优化:

  • 使用虚拟滚动只渲染可见项
  • 添加防抖处理频繁的筛选操作
  • 考虑分页加载数据
// 添加防抖
methods: {
  applyFilters: _.debounce(function() {
    this.filterChanged = true
  }, 300)
}

vue实现分类筛选

标签: vue
分享给朋友:

相关文章

vue前端实现搜索

vue前端实现搜索

实现搜索功能的基本方法 在Vue中实现搜索功能通常涉及以下几个关键步骤,结合数据绑定、计算属性和方法调用来动态过滤和显示结果。 数据绑定与输入处理 使用v-model双向绑定搜索输入框的值,监听用户…

vue实现简单的弹窗

vue实现简单的弹窗

使用 Vue 实现简单弹窗 组件基础结构 创建一个名为 Modal.vue 的组件文件,包含模板、脚本和样式部分: <template> <div class="modal-…

vue实现多选题

vue实现多选题

Vue实现多选题的方法 使用Vue实现多选题功能,可以通过v-model绑定数组、动态渲染选项、以及处理选中状态来实现。以下是一个完整的实现示例: 基础实现代码 <template>…

vue中实现显示和隐藏

vue中实现显示和隐藏

控制显示与隐藏的方法 在Vue中实现元素的显示和隐藏可以通过多种方式完成,以下是常见的几种方法: 1. 使用v-if指令v-if根据条件动态添加或移除DOM元素,适合需要频繁切换的场景。当条件为fa…

vue调用接口实现退出

vue调用接口实现退出

实现Vue退出功能的步骤 在Vue中调用接口实现退出功能通常需要与后端API交互,以下是实现方法: 前端实现 创建退出方法,调用后端API接口: methods: { logout() {…

vue实现密码框的包

vue实现密码框的包

Vue 密码框实现方案 在 Vue 中实现密码框功能,可以通过原生 HTML 结合 Vue 指令或使用第三方组件库完成。以下是几种常见方案: 原生 HTML + Vue 双向绑定 <temp…