You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

75 lines
2.2 KiB

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>列表过滤</title>
<script type="text/javascript" src="../vue.js"></script>
</head>
<body>
<div id="root">
<!-- 遍历列表-->
<h2>人员列表</h2>
<input type="text" placeholder="输入关键词" v-model="keyword">
<ul>
<li v-for="(data, index) in filterData" :key="data.id">
{{data.name}} - {{data.age}} - {{data.sex}}
</li>
</ul>
</div>
</body>
<script type="text/javascript">
Vue.config.productionTip = false
// 使用watch实现
// const vm = new Vue({
// el: "#root",
// data: {
// keyword: '',
// listData: [
// {id: 1, name: '马冬梅', age: 18, sex: '女'},
// {id: 2, name: '周冬雨', age: 19, sex: '女'},
// {id: 3, name: '周杰伦', age: 20, sex: '男'},
// {id: 4, name: '温兆伦', age: 21, sex: '男'}
// ],
// filterData: []
// },
// watch: {
// // keyword(val) {
// // this.listData = this.listData.filter((p) => {
// // return p.name.indexOf(val) !== -1
// // })
// // }
// keyword: {
// immediate: true,
// handler(val) {
// this.filterData = this.listData.filter((p) => {
// return p.name.indexOf(val) !== -1
// })
// }
// }
// }
// })
// 使用computed实现
const vm = new Vue({
el: "#root",
data: {
keyword: '',
listData: [
{id: 1, name: '马冬梅', age: 18, sex: '女'},
{id: 2, name: '周冬雨', age: 19, sex: '女'},
{id: 3, name: '周杰伦', age: 20, sex: '男'},
{id: 4, name: '温兆伦', age: 21, sex: '男'}
],
},
computed: {
filterData() {
return this.listData.filter((p) => {
return p.name.indexOf(this.keyword) !== -1
})
}
}
})
</script>
</html>