vue+element ui纯前端模糊查询案例
时间: 2023-11-04 07:52:47 浏览: 49
下面是一个基于 Vue.js 和 Element UI 的前端模糊查询示例:
Template:
```html
<template>
<el-input v-model="searchText" placeholder="请输入搜索内容" @input="handleSearch"></el-input>
<el-table :data="tableData" style="width: 100%">
<el-table-column prop="name" label="姓名"></el-table-column>
<el-table-column prop="age" label="年龄"></el-table-column>
<el-table-column prop="address" label="地址"></el-table-column>
</el-table>
</template>
```
Script:
```js
<script>
export default {
data() {
return {
tableData: [
{ name: '张三', age: 22, address: '北京市海淀区' },
{ name: '李四', age: 25, address: '上海市浦东新区' },
{ name: '王五', age: 28, address: '广州市天河区' },
{ name: '赵六', age: 30, address: '深圳市南山区' },
{ name: '钱七', age: 33, address: '重庆市渝北区' },
],
searchText: '',
};
},
methods: {
handleSearch() {
if (this.searchText) {
this.tableData = this.tableData.filter(
(item) =>
item.name.includes(this.searchText) ||
item.address.includes(this.searchText)
);
} else {
this.tableData = [
{ name: '张三', age: 22, address: '北京市海淀区' },
{ name: '李四', age: 25, address: '上海市浦东新区' },
{ name: '王五', age: 28, address: '广州市天河区' },
{ name: '赵六', age: 30, address: '深圳市南山区' },
{ name: '钱七', age: 33, address: '重庆市渝北区' },
];
}
},
},
};
</script>
```
在上面的示例中,我们使用了 Element UI 的 el-input 和 el-table 组件来实现前端模糊查询。在 data 中定义了一个 tableData 数组,用来存储表格数据。在 methods 中定义了一个 handleSearch 方法,用来处理搜索事件。在 el-input 组件中绑定了 searchText 变量,用来存储用户输入的搜索内容。在 el-input 的 @input 事件中调用 handleSearch 方法,当 searchText 不为空时,使用 filter() 方法来过滤 tableData 数组,找到与 searchText 匹配的数据,然后更新 tableData 数组,这样就实现了前端模糊查询的功能。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)