当前位置:首页 > VUE

vue实现淘宝筛选

2026-01-19 06:53:14VUE

实现淘宝筛选功能

使用Vue实现类似淘宝的筛选功能需要结合组件化、状态管理和UI交互设计。以下是一个完整的实现方案:

数据准备

筛选功能需要定义数据结构,通常包括分类、品牌、价格区间等维度:

data() {
  return {
    filters: {
      categories: [
        { id: 1, name: '女装', sub: ['T恤', '连衣裙', '裤子'] },
        { id: 2, name: '男装', sub: ['衬衫', '牛仔裤', '外套'] }
      ],
      brands: [
        { id: 101, name: '耐克' },
        { id: 102, name: '阿迪达斯' }
      ],
      priceRanges: [
        { min: 0, max: 100 },
        { min: 100, max: 200 }
      ]
    },
    selectedFilters: {
      category: [],
      brand: [],
      price: []
    }
  }
}

筛选组件实现

创建可复用的筛选组件:

<template>
  <div class="filter-section">
    <h3>{{ title }}</h3>
    <div v-for="item in items" :key="item.id">
      <input 
        type="checkbox" 
        :id="`${type}-${item.id}`"
        :value="item"
        v-model="selectedItems"
      >
      <label :for="`${type}-${item.id}`">{{ item.name || formatPrice(item) }}</label>
    </div>
  </div>
</template>

<script>
export default {
  props: ['title', 'items', 'type'],
  computed: {
    selectedItems: {
      get() {
        return this.$store.state.selectedFilters[this.type]
      },
      set(value) {
        this.$store.commit('updateFilter', { type: this.type, value })
      }
    }
  },
  methods: {
    formatPrice(item) {
      return `${item.min}-${item.max}元`
    }
  }
}
</script>

状态管理

使用Vuex管理筛选状态:

const store = new Vuex.Store({
  state: {
    selectedFilters: {
      category: [],
      brand: [],
      price: []
    }
  },
  mutations: {
    updateFilter(state, payload) {
      state.selectedFilters[payload.type] = payload.value
    },
    resetFilters(state) {
      state.selectedFilters = {
        category: [],
        brand: [],
        price: []
      }
    }
  }
})

商品列表组件

根据筛选条件过滤商品:

<template>
  <div class="product-list">
    <div v-for="product in filteredProducts" :key="product.id">
      <img :src="product.image" :alt="product.name">
      <h4>{{ product.name }}</h4>
      <p>¥{{ product.price }}</p>
    </div>
  </div>
</template>

<script>
export default {
  computed: {
    filteredProducts() {
      return this.$store.getters.filteredProducts
    }
  }
}
</script>

筛选逻辑

在Vuex中实现商品过滤逻辑:

getters: {
  filteredProducts: (state) => {
    return products.filter(product => {
      const categoryMatch = state.selectedFilters.category.length === 0 || 
        state.selectedFilters.category.some(cat => product.category === cat.id)

      const brandMatch = state.selectedFilters.brand.length === 0 ||
        state.selectedFilters.brand.some(brand => product.brand === brand.id)

      const priceMatch = state.selectedFilters.price.length === 0 ||
        state.selectedFilters.price.some(range => 
          product.price >= range.min && product.price <= range.max)

      return categoryMatch && brandMatch && priceMatch
    })
  }
}

响应式布局

实现适应不同屏幕尺寸的布局:

.filter-container {
  display: flex;
  flex-wrap: wrap;
}

.filter-section {
  width: 200px;
  margin-right: 20px;
  padding: 15px;
  background: #f5f5f5;
}

.product-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 20px;
}

@media (max-width: 768px) {
  .filter-container {
    flex-direction: column;
  }
  .filter-section {
    width: 100%;
    margin-bottom: 10px;
  }
}

交互增强

添加折叠功能提升用户体验:

<template>
  <div class="filter-section">
    <div class="filter-header" @click="toggleCollapse">
      <h3>{{ title }}</h3>
      <span>{{ isCollapsed ? '+' : '-' }}</span>
    </div>
    <div v-if="!isCollapsed">
      <!-- 筛选内容 -->
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isCollapsed: false
    }
  },
  methods: {
    toggleCollapse() {
      this.isCollapsed = !this.isCollapsed
    }
  }
}
</script>

性能优化

对于大量数据时使用虚拟滚动:

<template>
  <RecycleScroller
    class="product-list"
    :items="filteredProducts"
    :item-size="200"
    key-field="id"
  >
    <template v-slot="{ item }">
      <div class="product-item">
        <img :src="item.image" :alt="item.name">
        <h4>{{ item.name }}</h4>
        <p>¥{{ item.price }}</p>
      </div>
    </template>
  </RecycleScroller>
</template>

<script>
import { RecycleScroller } from 'vue-virtual-scroller'
import 'vue-virtual-scroller/dist/vue-virtual-scroller.css'

export default {
  components: {
    RecycleScroller
  }
}
</script>

完整示例集成

将所有组件整合到主页面:

vue实现淘宝筛选

<template>
  <div class="container">
    <div class="filter-container">
      <filter-section 
        title="分类" 
        :items="filters.categories" 
        type="category"
      />
      <filter-section 
        title="品牌" 
        :items="filters.brands" 
        type="brand"
      />
      <filter-section 
        title="价格" 
        :items="filters.priceRanges" 
        type="price"
      />
      <button @click="resetFilters">重置筛选</button>
    </div>

    <product-list />
  </div>
</template>

<script>
import FilterSection from './FilterSection.vue'
import ProductList from './ProductList.vue'

export default {
  components: {
    FilterSection,
    ProductList
  },
  data() {
    return {
      filters: {
        // 筛选数据
      }
    }
  },
  methods: {
    resetFilters() {
      this.$store.commit('resetFilters')
    }
  }
}
</script>

这个实现方案包含了淘宝筛选功能的主要特点:多维度筛选、实时过滤、响应式布局和良好的用户体验。可以根据实际需求调整筛选维度和交互细节。

标签: 淘宝vue
分享给朋友:

相关文章

用vue实现搜索查询

用vue实现搜索查询

实现搜索查询的基本思路 在Vue中实现搜索查询功能通常涉及以下几个核心部分:数据绑定、事件监听、过滤逻辑和UI反馈。以下是一个完整的实现方案。 数据绑定与输入框 创建一个Vue组件,包含一个输入框用…

vue实现markdown

vue实现markdown

Vue 实现 Markdown 编辑器 在 Vue 中实现 Markdown 编辑器可以通过集成第三方库或自定义解析器完成。以下是两种常见方法: 使用第三方库(如 marked 或 markdown…

vue 实现href

vue 实现href

Vue 实现 href 的方法 在 Vue 中实现 href 功能可以通过多种方式,具体取决于需求场景。以下是几种常见的方法: 使用 <a> 标签 直接使用 HTML 的 <a&g…

vue实现抖动

vue实现抖动

Vue 实现抖动效果 在 Vue 中实现抖动效果可以通过 CSS 动画或 JavaScript 动画库来实现。以下是几种常见的方法: 使用 CSS 动画 通过定义 @keyframes 动画并在 V…

vue mvvm实现

vue mvvm实现

Vue MVVM 实现原理 Vue.js 是一个基于 MVVM(Model-View-ViewModel)模式的框架,其核心是通过数据绑定和响应式系统实现视图与数据的自动同步。以下是 Vue 实现 M…

vue实现链接

vue实现链接

Vue 实现链接的方法 在 Vue 中实现链接可以通过多种方式,包括使用 <router-link>、<a> 标签或编程式导航。以下是几种常见的方法: 使用 <rout…