zoukankan      html  css  js  c++  java
  • Vue列表的过滤和排序

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Hello World</title>
        <script src="../js/vue.js"></script>
    </head>
    <body>
        <div id="app">
    		<input type="text" v-model="searchName" /><br />
            <ul>
    			<li v-for="(item, index) in filteredPersons" :key='index'>
    				{{item.name}}---{{item.age}}		
    			</li>
    			<button type="button" @click="orderType=1">按age升序</button>
    			<button type="button" @click="orderType=2">按age降序</button>
    			<button type="button" @click="orderType=0">原始顺序</button>
    		</ul>
        </div>
    
        <script>
            const vm = new Vue({
                el: '#app',
                data: {
    				searchName: '',
                    persons: [
    					{name:'tom', age:19},
    					{name:'jerry', age:15},
    					{name:'spike', age:20}
    				],
    				orderType: 0//0 不排序,1升序,2降序
                },
    			computed:{
    				filteredPersons(){
    					const {searchName, persons, orderType} = this
    					const newPersons = persons.filter(p=>p.name.indexOf(searchName)!==-1)
    					return orderType===0? newPersons:
    						orderType===1? newPersons.sort((a, b)=>a.age-b.age):
    						newPersons.sort((a, b)=>b.age-a.age)
    				}
    			}
            })
        </script>
    </body>
    </html>
    
  • 相关阅读:
    sqlserver获取当前id的前一条数据和后一条数据
    C#实现测量程序运行时间及cpu使用时间
    类库dll引用不成功问题
    合并相同字段
    Android之来历
    XML and JSON 验证
    特殊符号
    git 使用
    格式化字符串:金额
    grunt + sass 使用记录
  • 原文地址:https://www.cnblogs.com/pangqianjin/p/14893214.html
Copyright © 2011-2022 走看看