<!DOCTYPE html>
<html lang="en">
<head>
<script src="js/vue.js"></script>
<style>
</style>
</head>
<body>
<div id="app">
<input type="text" v-model="shuru"/> <br/>
<ul>
<li v-for="(p,index) in filterpersons" :key="index">
{{p.namee}}----{{p.age}}
</li>
</ul>
</div>
</body>
<script type="text/javascript">
var vue = new Vue({
el: "#app",
data: {
persons: [{namee: "xuhaitao", age: 33}, {namee: "hunk", age: 45}, {namee: "xuhaihuan", age: 30}],
xuhao: ["A", "B", "C", "D"],
shuru: 'x'
},
computed: {
filterpersons() {
const{persons,shuru}=this;
return persons.filter(item=>{return item.namee.indexOf(shuru.trim())>=0})
//另一种写法 ,匿名函数
/*
return this.persons.filter(function (item) {
return item.namee.indexOf(shuru)>=0;
})
*/
}
}
});
</script>
<html>
实现效果如下:
image image image原文链接:https://blog.csdn.net/qq_15267341/article/details/100175897
总结:实现原理是计算属性,计算属性可以当作属性使用,所以上面循环是用计算属性定义的名字去循环。js的indexOf返回的是数组,原来的数组总数据不会发生改变,这点比较好。
其他例子
使用vue简单实现:模糊查找
https://www.cnblogs.com/luowenshuai/p/9362174.html
vue实现输入框搜索功能
<el-input v-model="searchVal" style="width:100%; margin-bottom:10px" placeholder="请输入内容"/>
data(){
searchVal: '',
items: [
{
'tenantId': 36045,
'orgId': 3604500002,
'orgName': '开发环境测试-1',
'type': null,
'activate': '1'
},
{
'tenantId': 36053,
'orgId': 3605300001,
'orgName': '52-测试账号(校区)',
'type': null,
'activate': '1'
},
{
'tenantId': 36053,
'orgId': 3605300008,
'orgName': '测试-大爷',
'type': null,
'activate': '1'
}
],
}
<div class="serchArea">
<ul>
<p>总部</p>
<el-radio-group v-model="radio1" >
<el-radio v-for="(item,index) in search " :key="'z'+index" :label="item.orgName" @change="online(item)"/>
</el-radio-group>
<p>校区</p>
<el-radio-group v-model="radio1" >
<el-radio v-for="(item,index) in search " :key="'x'+index" :label="item.orgId" @change="online(item)"/>
</el-radio-group>
</ul>
</div>
computed: {
search() {
var item = this.items.filter(ele => {
if (ele.orgName.match(this.searchVal)) {
return ele
}
})
return item
}
}
网友评论