vue_小项目_模糊搜索(列表过滤)_结果排序

html

  •     <div id="test">
            <label>
                <input type="text" v-model="searchWord" placeholder="搜索一下"/>
            </label>
            <button type="button" @click="orderType=1">原来排序</button>
            <button type="button" @click="orderType=2">升序排序</button>
            <button type="button" @click="orderType=3">降序排序</button>
            
            <ul>
                <li v-for="(p, index) in searchResult">
                    {{p.name}}----{{p.age}}
                </li>
            </ul>
            
        </div>

js

  • <script src="./js/vue.js"></script>
        <script>
            new Vue({
                el:"#test",
                data(){
                    return {
                        persons:[
                            {name:"Tom", age:18},
                            {name:"Jack", age:16},
                            {name:"Jerry", age:15},
                            {name:"Kate", age:17},
                            {name:"Lee", age:14}
                        ],
                        searchWord: "",
                        orderType: 1
                    }
                },
                computed:{
                    searchResult(){
                        const {orderType, searchWord, persons} = this;
                        
                        let result = persons.filter(person=>person.name.indexOf(searchWord)!==-1);
                        
                        if(orderType !== 1){
                            result.sort((one, two)=>{
                                if(orderType === 2){
                                    return one.age - two.age
                                }else if(orderType === 3){
                                    return two.age - one.age
                                }
                            })
                        }
                        
                        return result;
                    }
                }
            })
        </script>

 

转载于:https://www.cnblogs.com/tianxiaxuange/p/10383855.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值