1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
| <body>
<div id="root">
<input type="text" v-model="keyWord">
<ul>
<li v-for="(p,index) of filPersons" :key="index">
{{p.name}}-{{p.age}}-{{p.gender}}
</li>
</ul>
</div>
</body>
<script type="text/javascript">
Vue.config.productionTip = false
new Vue({
el: '#root',
data: {
keyWord: '',
persons: [{
id: '001',
name: '马冬梅',
age: 19,
gender: '女'
}, {
id: '002',
name: '周冬雨',
age: 20,
gender: '女'
}, {
id: '003',
name: '周杰伦',
age: 21,
gender: '女'
}, {
id: '004',
name: '温兆伦',
age: 22,
gender: '男'
}, ],
filPersons: []
},
watch: {
keyWord: {
immediate: true,
handler(val) {
this.filPersons = this.persons.filter((p) => {
return p.name.indexOf(val) !== -1 // 'abc'.indexOf(xxx) abc不包含xxx就显示-1, 包含则返回它在第几位
})
}
}
}
})
</script>
|