vue列表查询实现
实现Vue列表查询功能
基本数据绑定与渲染
在Vue中实现列表查询,首先需要定义数据源和查询条件。通过v-model绑定搜索输入框,使用计算属性过滤列表。
<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>
多条件复合查询
对于更复杂的查询需求,可以扩展查询条件和过滤逻辑。
<template>
<div>
<input v-model="queryParams.name" placeholder="名称">
<input v-model="queryParams.category" placeholder="类别">
<select v-model="queryParams.status">
<option value="">全部</option>
<option value="1">启用</option>
<option value="0">禁用</option>
</select>
<table>
<tr v-for="item in filteredItems" :key="item.id">
<td>{{ item.name }}</td>
<td>{{ item.category }}</td>
<td>{{ item.status ? '启用' : '禁用' }}</td>
</tr>
</table>
</div>
</template>
<script>
export default {
data() {
return {
queryParams: {
name: '',
category: '',
status: ''
},
items: [
{ id: 1, name: '产品A', category: '电子', status: 1 },
{ id: 2, name: '产品B', category: '家居', status: 0 }
]
}
},
computed: {
filteredItems() {
return this.items.filter(item => {
const nameMatch = item.name.includes(this.queryParams.name)
const categoryMatch = this.queryParams.category ?
item.category === this.queryParams.category : true
const statusMatch = this.queryParams.status ?
item.status.toString() === this.queryParams.status : true
return nameMatch && categoryMatch && statusMatch
})
}
}
}
</script>
分页处理
对于大数据量列表,需要结合分页功能实现高效查询。
<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 >= totalPages">下一页</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())
)
},
totalPages() {
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.totalPages) {
this.currentPage++
}
},
prevPage() {
if (this.currentPage > 1) {
this.currentPage--
}
}
}
}
</script>
使用第三方库优化查询
对于更复杂的查询需求,可以考虑使用lodash等工具库的debounce函数优化性能。
import { debounce } from 'lodash'
export default {
data() {
return {
searchQuery: '',
items: [],
filteredItems: []
}
},
created() {
this.debouncedFilter = debounce(this.filterItems, 300)
},
watch: {
searchQuery() {
this.debouncedFilter()
}
},
methods: {
filterItems() {
this.filteredItems = this.items.filter(item =>
item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
)
}
}
}
服务端查询
当数据量很大时,应该将查询逻辑放到服务端,前端只负责发送请求和显示结果。
export default {
data() {
return {
searchQuery: '',
items: [],
loading: false
}
},
watch: {
searchQuery() {
this.fetchItems()
}
},
methods: {
async fetchItems() {
this.loading = true
try {
const response = await axios.get('/api/items', {
params: {
q: this.searchQuery
}
})
this.items = response.data
} catch (error) {
console.error(error)
} finally {
this.loading = false
}
}
}
}
这些方法涵盖了从基础到高级的Vue列表查询实现方案,可以根据实际需求选择合适的实现方式。







