当前位置:首页 > 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 => {
      // 筛选逻辑
    })
  }
}

性能优化

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

vue实现分类筛选

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

标签: vue
分享给朋友:

相关文章

vue搜索过后实现分页

vue搜索过后实现分页

Vue 实现搜索后分页功能 数据绑定与搜索逻辑 在 Vue 组件中定义必要的数据属性: data() { return { searchQuery: '', currentPag…

vue实现无线滚动列表

vue实现无线滚动列表

无限滚动列表的实现方法 在Vue中实现无限滚动列表通常结合监听滚动事件和动态加载数据。以下是几种常见方法: 使用自定义指令 创建自定义指令v-infinite-scroll监听滚动事件: Vue.…

vue  select实现

vue select实现

Vue Select 实现方法 在 Vue 中实现下拉选择功能可以使用原生 <select> 标签或第三方库如 vue-select。以下是两种方法的详细说明: 原生 HTML Sele…

vue实现高亮

vue实现高亮

Vue 实现文本高亮的方法 在 Vue 中实现文本高亮可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 v-html 指令 通过 v-html 指令可以动态插入 HTML 内容,将需要高…

vue 实现登录

vue 实现登录

Vue 实现登录功能 创建登录表单组件 在 Vue 项目中创建一个登录表单组件(如 Login.vue),包含用户名和密码输入框以及提交按钮。使用 v-model 实现双向数据绑定。 <tem…

vue实现popper

vue实现popper

Vue 实现 Popper 的方法 使用 Tippy.js 库 Tippy.js 是一个轻量级的 Popper.js 封装库,提供丰富的工具提示功能。安装 Tippy.js 及其 Vue 封装: n…