vue实现列表查询
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: '',
items: [
{ id: 1, name: '苹果' },
{ id: 2, name: '香蕉' },
{ id: 3, name: '橙子' }
]
}
},
computed: {
filteredList() {
return this.items.filter(item =>
item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
)
}
}
}
</script>
使用 watch 和 methods 实现
对于更复杂的查询逻辑,可以结合 watch 和 methods 实现。
<template>
<div>
<input v-model="searchQuery" @input="filterItems">
<ul>
<li v-for="item in filteredItems" :key="item.id">
{{ item.name }}
</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
searchQuery: '',
items: [
{ id: 1, name: '苹果' },
{ id: 2, name: '香蕉' },
{ id: 3, name: '橙子' }
],
filteredItems: []
}
},
created() {
this.filteredItems = [...this.items]
},
methods: {
filterItems() {
this.filteredItems = this.items.filter(item =>
item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
)
}
}
}
</script>
使用第三方库实现高级搜索
对于更复杂的搜索需求,可以考虑使用 Fuse.js 等模糊搜索库。

import Fuse from 'fuse.js'
export default {
data() {
return {
searchQuery: '',
items: [
{ id: 1, name: '苹果', category: '水果' },
{ id: 2, name: '胡萝卜', category: '蔬菜' },
{ id: 3, name: '牛肉', category: '肉类' }
],
fuse: null,
searchResults: []
}
},
created() {
this.fuse = new Fuse(this.items, {
keys: ['name', 'category'],
threshold: 0.4
})
},
watch: {
searchQuery(newVal) {
this.searchResults = newVal ?
this.fuse.search(newVal).map(r => r.item) :
[...this.items]
}
}
}
结合分页实现查询
对于大数据量的列表,可以结合分页实现查询功能。
<template>
<div>
<input v-model="searchQuery" placeholder="搜索...">
<ul>
<li v-for="item in paginatedData" :key="item.id">
{{ item.name }}
</li>
</ul>
<button @click="prevPage" :disabled="currentPage === 1">上一页</button>
<span>第 {{ currentPage }} 页</span>
<button @click="nextPage" :disabled="currentPage >= pageCount">下一页</button>
</div>
</template>
<script>
export default {
data() {
return {
searchQuery: '',
currentPage: 1,
pageSize: 5,
items: [
// 大数组数据
]
}
},
computed: {
filteredList() {
return this.items.filter(item =>
item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
)
},
pageCount() {
return Math.ceil(this.filteredList.length / this.pageSize)
},
paginatedData() {
const start = (this.currentPage - 1) * this.pageSize
const end = start + this.pageSize
return this.filteredList.slice(start, end)
}
},
methods: {
nextPage() {
if (this.currentPage < this.pageCount) {
this.currentPage++
}
},
prevPage() {
if (this.currentPage > 1) {
this.currentPage--
}
}
}
}
</script>
使用 Vuex 管理搜索状态
在大型应用中,可以使用 Vuex 集中管理搜索状态。
// store.js
export default new Vuex.Store({
state: {
searchQuery: '',
items: [
{ id: 1, name: '苹果' },
{ id: 2, name: '香蕉' },
{ id: 3, name: '橙子' }
]
},
getters: {
filteredItems: state => {
return state.items.filter(item =>
item.name.toLowerCase().includes(state.searchQuery.toLowerCase())
)
}
},
mutations: {
setSearchQuery(state, query) {
state.searchQuery = query
}
}
})
// 组件中
<template>
<div>
<input
:value="searchQuery"
@input="updateSearchQuery($event.target.value)"
placeholder="搜索..."
>
<ul>
<li v-for="item in filteredItems" :key="item.id">
{{ item.name }}
</li>
</ul>
</div>
</template>
<script>
import { mapState, mapGetters, mapMutations } from 'vuex'
export default {
computed: {
...mapState(['searchQuery']),
...mapGetters(['filteredItems'])
},
methods: {
...mapMutations(['setSearchQuery']),
updateSearchQuery(query) {
this.setSearchQuery(query)
}
}
}
</script>
以上方法可以根据具体需求选择或组合使用,实现不同复杂度的列表查询功能。






