当前位置:首页 > VUE

vue实现筛选效果

2026-01-17 15:04:16VUE

Vue 实现筛选效果的方法

基于计算属性的筛选

在 Vue 中可以通过计算属性实现数据的动态筛选。定义一个计算属性,根据用户输入的筛选条件返回过滤后的数据列表。

<template>
  <div>
    <input v-model="searchText" placeholder="输入筛选条件">
    <ul>
      <li v-for="item in filteredItems" :key="item.id">{{ item.name }}</li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchText: '',
      items: [
        { id: 1, name: 'Apple' },
        { id: 2, name: 'Banana' },
        { id: 3, name: 'Orange' }
      ]
    }
  },
  computed: {
    filteredItems() {
      return this.items.filter(item => 
        item.name.toLowerCase().includes(this.searchText.toLowerCase())
      )
    }
  }
}
</script>

使用 watch 监听筛选条件变化

当筛选逻辑较复杂或需要异步操作时,可以使用 watch 监听筛选条件的变化。

<script>
export default {
  data() {
    return {
      searchText: '',
      items: [],
      filteredItems: []
    }
  },
  watch: {
    searchText(newVal) {
      this.filterItems(newVal)
    }
  },
  methods: {
    filterItems(searchText) {
      this.filteredItems = this.items.filter(item =>
        item.name.toLowerCase().includes(searchText.toLowerCase())
      )
    }
  },
  created() {
    // 模拟异步获取数据
    setTimeout(() => {
      this.items = [
        { id: 1, name: 'Apple' },
        { id: 2, name: 'Banana' },
        { id: 3, name: 'Orange' }
      ]
      this.filteredItems = [...this.items]
    }, 500)
  }
}
</script>

多条件筛选实现

对于需要多个筛选条件的场景,可以扩展计算属性或方法来实现。

<template>
  <div>
    <input v-model="filters.name" placeholder="名称">
    <input v-model="filters.category" placeholder="类别">
    <ul>
      <li v-for="item in filteredItems" :key="item.id">
        {{ item.name }} - {{ item.category }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      filters: {
        name: '',
        category: ''
      },
      items: [
        { id: 1, name: 'Apple', category: 'Fruit' },
        { id: 2, name: 'Carrot', category: 'Vegetable' },
        { id: 3, name: 'Orange', category: 'Fruit' }
      ]
    }
  },
  computed: {
    filteredItems() {
      return this.items.filter(item => {
        const nameMatch = item.name.toLowerCase().includes(
          this.filters.name.toLowerCase()
        )
        const categoryMatch = item.category.toLowerCase().includes(
          this.filters.category.toLowerCase()
        )
        return nameMatch && categoryMatch
      })
    }
  }
}
</script>

使用第三方库实现高级筛选

对于更复杂的筛选需求,可以考虑使用 lodash 等工具库的筛选函数。

import _ from 'lodash'

export default {
  computed: {
    filteredItems() {
      return _.filter(this.items, item => {
        return _.includes(item.name.toLowerCase(), this.searchText.toLowerCase())
      })
    }
  }
}

服务器端筛选实现

当数据量很大时,建议将筛选逻辑放在服务器端,通过 API 请求获取筛选结果。

methods: {
  async fetchFilteredData() {
    const response = await axios.get('/api/items', {
      params: {
        search: this.searchText
      }
    })
    this.filteredItems = response.data
  }
},
watch: {
  searchText() {
    this.fetchFilteredData()
  }
}

以上方法可以根据具体需求选择使用,计算属性适合简单客户端筛选,watch 适合需要额外处理的筛选,服务器端筛选适合大数据量场景。

vue实现筛选效果

标签: 效果vue
分享给朋友:

相关文章

vue实现多级弹窗

vue实现多级弹窗

Vue 多级弹窗实现方法 方法一:使用动态组件与递归组件 在 Vue 中可以通过动态组件配合递归组件实现多级弹窗。定义一个基础弹窗组件,通过递归调用自身实现层级嵌套。 基础弹窗组件模板示例:…

vue表单实现搜索

vue表单实现搜索

实现Vue表单搜索功能 在Vue中实现表单搜索功能通常涉及表单绑定、事件处理和搜索逻辑。以下是具体实现方法: 数据绑定与表单结构 使用v-model实现表单与Vue数据的双向绑定: <t…

在线脑图 vue 实现

在线脑图 vue 实现

在线脑图 Vue 实现方案 使用开源库实现 推荐使用 mind-elixir 或 jsmind 这类开源库,它们专为脑图设计且支持 Vue 集成。 安装依赖: npm install mind…

vue中如何实现循环

vue中如何实现循环

在 Vue 中实现循环 Vue 提供了 v-for 指令用于实现循环渲染列表数据。v-for 可以遍历数组、对象或数字范围,生成动态的 DOM 元素。 遍历数组 使用 v-for 遍历数组时,语法…

vue实现录音文件播放

vue实现录音文件播放

使用 Vue 实现录音文件播放 准备工作 确保项目中已安装必要的依赖,如 vue-audio-recorder 或原生 Web Audio API。录音文件播放通常涉及录音、存储和播放三个核心环节。…

vue实现微博发布动态

vue实现微博发布动态

使用Vue实现微博发布动态功能 创建Vue组件结构 新建一个WeiboPost.vue组件,包含文本框、图片上传和发布按钮: <template> <div class="we…