开源之夏第三届火热来袭,高校学生参与赢万元奖金!>>>
vue实现搜索功能
template 部分
返回
搜索
{{item.contact_name}}
{{item.address}}
{{item.phone}}
{{item.contact_name}}
{{item.address}}
{{item.phone}}
JS 部分
import { mapGetters } from "vuex";
import api from "../../api/axiosConfig.js";
export default {
data() {
return {
search: "",
// 原本展示数据
list: [],
// 搜索后的展示数据
searchData: []
};
},
components: {},
computed: {
...mapGetters(["getAddressList", "getLoginUser"])
},
created() {
// 获取的接口数据
this.getList();
},
mounted() {},
methods: {
goback() {
this.$router.go(-1);
},
// 获取接口中数据的方法
getList() {
let params = {
id: this.getLoginUser.id,
token_sc: this.getLoginUser.token_sc
};
api.AddressList(params).then(res => {
// list 就是原始数据
this.list = res.data;
});
},
Search() {
// search 是 v-model="search" 的 search
var search = this.search;
if (search) {
this.searchData = this.list.filter(function(product) {
// 每一项数据
// console.log(product)
return Object.keys(product).some(function(key) {
// 每一项数据的参数名
// console.log(key)
return (
String(product[key])
// toLowerCase() 方法用于把字符串转换为小写。
.toLowerCase()
// indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。
.indexOf(search) > -1
);
});
});
}
}
}
};
看看效果
这是初始样式
![](https://oscimg.oschina.net/oscnet/2df3fa8024ff634360b09bbe8a3b2bd1db1.png)
看看输入内容,搜索之后的
![](https://oscimg.oschina.net/oscnet/730640c903bc9197ff791eb0f05c58209c1.png)
但是,如果输入栏没了内容,下面啥也不显示里,那我来加工一下
![](https://oscimg.oschina.net/oscnet/2ef3c3176627783d0a66a22c7f894697a7c.png)
|