<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>列表过滤</title> <script type="text/javascript" src="js/vue.js" ></script> </head> <body>
<div id="root"> <h2>人员列表</h2> <ul> <input type="text" placeholder="请输入搜索名字" v-model="keyWord"> <li v-for="(p,index) in filterPersons" :key="index"> {{p.name}}-{{p.age}}-{{p.sex}} </li> </ul> </div> </body> <script> Vue.config.productionTip = false /*new Vue({ el:'#root', data:{ keyWord:'', persons:[ {id:'001',name:'马冬梅',age:18,sex:'女'}, {id:'002',name:'周冬雨',age:28,sex:'女'}, {id:'003',name:'周杰伦',age:38,sex:'男'}, {id:'003',name:'邓伦',age:28,sex:'男'}, ], filterPersons:[] }, watch:{ keyWord:{ immediate:true, handler(value){ this.filterPersons = this.persons.filter((p)=>{ return p.name.indexOf(value) !== -1 }) } } } })*/ new Vue({ el:'#root', data:{ keyWord:'', persons:[ {id:'001',name:'马冬梅',age:18,sex:'女'}, {id:'002',name:'周冬雨',age:28,sex:'女'}, {id:'003',name:'周杰伦',age:38,sex:'男'}, {id:'003',name:'邓伦',age:28,sex:'男'}, ] }, computed:{ filterPersons(){ return this.persons.filter((p)=>{ return p.name.indexOf(this.keyWord) !== -1 }) } } }) </script> </html>
|