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.
		
		
		
	
	
		
		
			
	
	
		
			
				
					
						
							|  |  |  | <!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">
 | 
					
						
							|  |  |  |     <button @click="sortType=2">年龄升序</button>
 | 
					
						
							|  |  |  |     <button @click="sortType=1">年龄降序</button>
 | 
					
						
							|  |  |  |     <button @click="sortType=0">原顺序</button>
 | 
					
						
							|  |  |  |     <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
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 使用computed实现
 | 
					
						
							|  |  |  |     const vm = new Vue({
 | 
					
						
							|  |  |  |         el: "#root",
 | 
					
						
							|  |  |  |         data: {
 | 
					
						
							|  |  |  |             keyword: '',
 | 
					
						
							|  |  |  |             sortType: 0,  // 0-原顺序 1-年龄降序 2-年龄升序
 | 
					
						
							|  |  |  |             listData: [
 | 
					
						
							|  |  |  |                 {id: 1, name: '马冬梅', age: 25, sex: '女'},
 | 
					
						
							|  |  |  |                 {id: 2, name: '周冬雨', age: 19, sex: '女'},
 | 
					
						
							|  |  |  |                 {id: 3, name: '周杰伦', age: 20, sex: '男'},
 | 
					
						
							|  |  |  |                 {id: 4, name: '温兆伦', age: 17, sex: '男'}
 | 
					
						
							|  |  |  |             ],
 | 
					
						
							|  |  |  |         },
 | 
					
						
							|  |  |  |         computed: {
 | 
					
						
							|  |  |  |             filterData() {
 | 
					
						
							|  |  |  |                 const arr = this.listData.filter((p) => {
 | 
					
						
							|  |  |  |                     return p.name.indexOf(this.keyword) !== -1
 | 
					
						
							|  |  |  |                 })
 | 
					
						
							|  |  |  |                 if (this.sortType){
 | 
					
						
							|  |  |  |                     arr.sort((a,b) => {
 | 
					
						
							|  |  |  |                         return this.sortType === 1 ? b.age - a.age : a.age - b.age
 | 
					
						
							|  |  |  |                     })
 | 
					
						
							|  |  |  |                 }
 | 
					
						
							|  |  |  |                 return arr
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |     })
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | </script>
 | 
					
						
							|  |  |  | </html>
 |