vue列表查询实现
Vue 列表查询实现
在 Vue 中实现列表查询功能通常涉及数据绑定、事件处理和过滤逻辑。以下是几种常见实现方式:
基于计算属性的过滤
<template>
<div>
<input v-model="searchQuery" placeholder="搜索...">
<ul>
<li v-for="item in filteredList" :key="item.id">
{{ item.name }}
</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
searchQuery: '',
originalList: [
{ id: 1, name: '苹果' },
{ id: 2, name: '香蕉' },
{ id: 3, name: '橙子' }
]
}
},
computed: {
filteredList() {
return this.originalList.filter(item =>
item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
)
}
}
}
</script>
使用第三方库实现高级搜索
对于更复杂的搜索需求,可以使用 lodash 的 debounce 函数实现防抖:
import { debounce } from 'lodash'
export default {
data() {
return {
searchQuery: '',
filteredList: []
}
},
created() {
this.debouncedFilter = debounce(this.filterList, 300)
},
watch: {
searchQuery(newVal) {
this.debouncedFilter(newVal)
}
},
methods: {
filterList(query) {
this.filteredList = this.originalList.filter(item =>
item.name.toLowerCase().includes(query.toLowerCase())
)
}
}
}
结合后端API的分页查询
当数据量较大时,通常需要后端配合实现分页查询:
methods: {
async fetchData(params = {}) {
const response = await axios.get('/api/items', {
params: {
page: this.currentPage,
size: this.pageSize,
keyword: this.searchQuery,
...params
}
})
this.listData = response.data.items
this.total = response.data.total
}
}
多条件复合查询实现
对于需要多个筛选条件的场景:
computed: {
filteredList() {
return this.originalList.filter(item => {
const matchesSearch = item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
const matchesCategory = this.selectedCategory ? item.category === this.selectedCategory : true
return matchesSearch && matchesCategory
})
}
}
性能优化建议
对于大型数据集,考虑以下优化方案:

- 虚拟滚动技术(如使用
vue-virtual-scroller) - 分页加载
- Web Worker 处理过滤逻辑
- 对列表数据进行预处理建立索引
每种实现方式适用于不同场景,开发者应根据项目具体需求选择合适方案。






