Vue (5) — 列表过滤(分别用watch和computed实现)、列表排序

目录

一、列表过滤

  1.用watch实现

  2.用computed实现

二、列表排序


一、列表过滤

        实现功能:在输入框内输入关键词,过滤列表中与关键词相关的列。可以用watch或computed来实现。

        列表样式:

  1.用watch实现

<body>
    <div id="root">
        <h2>人员列表</h2>
        <input type="text" placeholder="请输入姓名" v-model="keyWord">
        <ul>
            <li v-for="(p,index) in filPersons" :key="index">
                {
   
   {p.name}}-{
   
   {p.age}}-{
   
   {p.sex}}
            </li>
        </ul>
    </div>
</body>
<script>
    Vue.config.productionTip = false//阻止Vue在启动时生成生产提示
   new Vue({
        el:"#root",
        data:{
            keyWord:'',
            persons:[
                {id:'001',name:"马冬梅",age:18,sex:"女"},
                {id:'002',name:"周冬雨",age:19,sex:"女"},
                {id:'003',name:"周杰伦",age:20,sex:"男"},
                {id:'004',name:"温兆伦",age:21,sex:"男"},
            ],
            filPersons:[]
        },
        watch:{
            keyWord:{
                immediate:true,
                handler(val){
                this.filPersons = this.persons.filter((p)=>{
                    return p.name.indexOf(val) !== -1
                })
            }
            }
        }
    })
</script>

Q:为什么使用 immediate:true ?

A:使用 immediate:true 将函数先调用一次,否则在第一次打开页面时列表将不会展示出来,只有在输入框内输入关键词检索后,清除输入框内的内容,使输入框内变成空字符串,从而检索到列表的所有内容,将列表内容展示出来 。

 

  2.用computed实现

<body>
    <div id="root">
        <h2>人员列表</h2>
        <input type="text" placeholder="请输入姓名" v-model="keyWord">
        <ul>
            <li v-for="(p,index) in filPersons" :key="index">
                {
   
   {p.name}}-{
   
   {p.age}}-{
   
   {p.sex}}
            </li>
        </ul>
    </div>
</body>
<script>
    Vue.config.productionTip = false//阻止Vue在启动时生成生产提示
    new Vue({
        el:"#root",
        data:{
            keyWord:'',
            persons:[
                {id:'001',name:"马冬梅",age:18,sex:"女"},
                {id:'002',name:"周冬雨",age:19,sex:"女"},
                {id:'003',name:"周杰伦",age:20,sex:"男"},
                {id:'004',name:"温兆伦",age:21,sex:"男"},
            ]
        },
        computed:{
            filPersons(){
                return this.persons.filter((p)=>{
                    return p.name.indexOf(this.keyWord)!==-1
                })
            }
        }
    })
</script>

  

二、列表排序

        实现功能:按下按键将列表中的项排序,如果在输入框中输入关键词后,将列表中显示出来的项排序列表如下:

<body>
    <div id="root"> 
        <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="(p,index) of filPersons" :key='index'>  
                {
   
   {p.name}}-{
   
   {p.age}}-{
   
   {p.sex}}
            </li>
        </ul>
    </div>
</body>
<script>
    Vue.config.productionTip = false
    new Vue({
        el:'#root',
        data:{
            keyWord:'',
            sortType:0,//原顺序0 ,降序1,升序2
            persons:[
                {id:'001',name:"马冬梅",age:18,sex:"女"},
                {id:'002',name:"周冬雨",age:89,sex:"女"},
                {id:'003',name:"周杰伦",age:29,sex:"男"},
                {id:'004',name:"温兆伦",age:21,sex:"男"},
            ]
        },
        computed:{
            filPersons(){
                const arr = this.persons.filter((p)=>{
                return p.name.indexOf(this.keyWord) !== -1
                })
                if(this.sortType){
                    arr.sort((p1,p2)=>{
                        return this.sortType === 1 ? p2.age-p1.age : p1.age-p2.age
                    })
                }
                return arr
            } 
        }
    })

 

猜你喜欢

转载自blog.csdn.net/m0_59897687/article/details/121643338