Vue_列表的搜索和排序

4 篇文章 0 订阅

学习笔记

#_Vue_列表的搜索和排序

_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>Document</title>
    <style>
        #test{
            width: 400px;
            height: 400px;
            background-color: pink;
        }
    </style>
</head>
<body>
    <div id="test">
        <h1>列表过滤</h1>
        <input type="text" v-model="searchName1">
        <ul>
            <li v-for="(p,index) in filterPersons" :key='index'>
                {{index}} -----{{p.name}} --{{p.age}}
            </li>  
        </ul>
       <button @click="setoderType(1)">年龄升序</button>   <!-- 给排序的内容传参 -->
       <button @click="setoderType(2)">年龄降序</button>
       <button @click="setoderType(0)">原本序列</button>
    </div>
</body>
<script type="text/javascript" src="./js/vue.js"></script>
<script type="text/javascript">
 new Vue({
    el:'#test',
    data:{
        searchName1: "",
        oderType: 0,  // 0 原本序列   
        persons:[  //vue 
        {name:'tom0',age:18},
        {name:'tom1',age:19},
        {name:'tom2',age:16},
        {name:'tom3',age:15}
        ]
    },
    computed:{
        filterPersons () {
            //定义相关的数据
            const{searchName1,persons,oderType} = this
            // alert(searchName1)
            let fPersons ;
            //对person过滤
            fPersons = persons.filter( p => p.name.indexOf(searchName1) !==-1)
            if(oderType!==0){
                fPersons.sort(function(p1,p2){
                    if(oderType===2){
                        return p2.age-p1.age
                    }else{
                        return p1.age-p2.age
                    }
                })
            }
            return fPersons
        }
    },
    methods:{   //定义一个方法
        setoderType(oderType){
        this.oderType = oderType
        }
    }
    
 })
</script>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值