<!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})
//另一種寫法 时呀,匿名函數(shù)
/*
return this.persons.filter(function (item) {
return item.namee.indexOf(shuru)>=0;
})
*/
}
}
});
</script>
<html>
實現(xiàn)效果如下:
image
image
image
原文鏈接:https://blog.csdn.net/qq_15267341/article/details/100175897
總結(jié):實現(xiàn)原理是計算屬性魏颓,計算屬性可以當(dāng)作屬性使用,所以上面循環(huán)是用計算屬性定義的名字去循環(huán)熄驼。js的indexOf返回的是數(shù)組,原來的數(shù)組總數(shù)據(jù)不會發(fā)生改變旧蛾,這點比較好瞬项。
其他例子
使用vue簡單實現(xiàn):模糊查找
https://www.cnblogs.com/luowenshuai/p/9362174.html
vue實現(xiàn)輸入框搜索功能
<el-input v-model="searchVal" style="width:100%; margin-bottom:10px" placeholder="請輸入內(nèi)容"/>
data(){
searchVal: '',
items: [
{
'tenantId': 36045,
'orgId': 3604500002,
'orgName': '開發(fā)環(huán)境測試-1',
'type': null,
'activate': '1'
},
{
'tenantId': 36053,
'orgId': 3605300001,
'orgName': '52-測試賬號(校區(qū))',
'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>校區(qū)</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
}
}