当前位置:首页 > VUE

vue实现筛选列表

2026-03-09 04:46:09VUE

实现筛选列表的基本思路

在Vue中实现筛选列表通常需要结合数据绑定、计算属性和方法。通过监听用户输入或选择的变化,动态过滤原始数据列表,展示符合条件的项目。

准备数据源

定义一个数组作为原始数据源,包含需要筛选的列表项。每个列表项可以是对象,包含多个属性用于不同维度的筛选。

data() {
  return {
    items: [
      { id: 1, name: 'Apple', category: 'Fruit', price: 2.5 },
      { id: 2, name: 'Banana', category: 'Fruit', price: 1.2 },
      { id: 3, name: 'Carrot', category: 'Vegetable', price: 0.8 },
      { id: 4, name: 'Milk', category: 'Dairy', price: 3.5 }
    ],
    searchTerm: '',
    selectedCategory: ''
  }
}

创建筛选逻辑

使用计算属性实现筛选逻辑,根据用户输入的条件返回过滤后的列表。计算属性会自动缓存结果,提高性能。

computed: {
  filteredItems() {
    return this.items.filter(item => {
      const matchesSearch = item.name.toLowerCase().includes(this.searchTerm.toLowerCase())
      const matchesCategory = !this.selectedCategory || item.category === this.selectedCategory
      return matchesSearch && matchesCategory
    })
  }
}

添加用户输入界面

在模板中添加输入控件,让用户可以输入搜索词和选择分类。使用v-model实现双向数据绑定。

<div>
  <input type="text" v-model="searchTerm" placeholder="Search items...">

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

显示筛选结果

使用v-for指令循环渲染过滤后的列表项。可以添加条件判断,当没有匹配结果时显示提示信息。

<ul>
  <li v-for="item in filteredItems" :key="item.id">
    {{ item.name }} - {{ item.category }} - ${{ item.price }}
  </li>
  <li v-if="filteredItems.length === 0">No items match your criteria</li>
</ul>

添加分类选项

创建一个计算属性获取所有唯一的分类选项,用于下拉选择框。

computed: {
  uniqueCategories() {
    return [...new Set(this.items.map(item => item.category))]
  }
}

多条件筛选扩展

对于更复杂的筛选需求,可以添加更多筛选条件和逻辑。例如增加价格范围筛选。

data() {
  return {
    minPrice: 0,
    maxPrice: 100
  }
}

computed: {
  filteredItems() {
    return this.items.filter(item => {
      const matchesSearch = item.name.toLowerCase().includes(this.searchTerm.toLowerCase())
      const matchesCategory = !this.selectedCategory || item.category === this.selectedCategory
      const matchesPrice = item.price >= this.minPrice && item.price <= this.maxPrice
      return matchesSearch && matchesCategory && matchesPrice
    })
  }
}

性能优化建议

对于大型列表,可以考虑以下优化措施:

vue实现筛选列表

  • 使用debounce处理搜索输入
  • 实现分页加载
  • 使用虚拟滚动技术
  • 将复杂计算移至Web Worker

标签: 列表vue
分享给朋友:

相关文章

vue里实现增删改查

vue里实现增删改查

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

vue实现截图

vue实现截图

Vue 实现截图的方法 使用 html2canvas 库 安装 html2canvas 库: npm install html2canvas 在 Vue 组件中使用: import html2ca…

vue 实现关闭

vue 实现关闭

Vue 实现关闭功能的方法 在Vue中实现关闭功能通常涉及组件销毁、模态框隐藏或路由跳转等场景。以下是几种常见实现方式: 销毁当前组件 使用v-if或v-show控制组件显示/隐藏,适用于局部组件:…

vue实现slidetoggle

vue实现slidetoggle

Vue 实现 SlideToggle 效果 SlideToggle 是一种常见的交互效果,元素以滑动方式展开或收起。以下是几种实现方法: 使用 CSS Transition 和 v-show 通过…

vue 实现分屏

vue 实现分屏

Vue 实现分屏的方法 使用 CSS Flexbox 或 Grid 布局 通过 CSS Flexbox 或 Grid 布局可以轻松实现分屏效果。在 Vue 组件中定义两个或多个区域,并使用 CSS 控…

vue实现tip

vue实现tip

Vue实现Tooltip的方法 使用Vue实现Tooltip可以通过多种方式完成,以下是几种常见的方法: 使用原生HTML的title属性 在Vue模板中直接使用HTML的title属性是最简单的实…