VUE基础知识08

列表渲染:
3.过滤遍历
4.排序遍历:两种方式,服务器和客户端。
新建组件:ListRenderTwo.vue

<div>
   <input type = "text" placeholder = "请输入姓名!" v-model = "searchName">
   <ul>
      <li v-for="(p,index) in filterPersons" :key="personsKeys[index]">
      {{index + 1}} )姓名:{{p.name}} 性别{{p.sex}}年龄{{p.age}}电话{{p.phone}}
      </li>
   </ul>
   <p></p>
   <div>
       <button @click="orderByAge(2)">年龄升序</button>
       <button @click="orderByAge(1)">年龄降序</button>
       <button @click="orderByAge(0)">默认</button>
   </div>
</div>
import shortId from 'shortid'
data(){
   return {
       searchName:'',
       persons:[
           {name:'张三',sex:'男',age:'19',phone:'18564885555'},
           {name:'李四',sex:'女',age:'20',phone:'57879955522'},
           {name:'王五',sex:'男',age:'23',phone:'85953356621'}
       ],
       personsKeys:[],
       orderType:0 //排序
    }
},
mounted(){
    this.personsKeys = this.persons.map(v=>shortId.generate())
},//取出shortId
methods:{
    orderByAge(orderType){
        this.orderType=orderType;
    }
}
computed:{
    filterPersons(){
        //获取数据
        let {searchName,persons,orderType} = this;
        //取出数组中的数据
        let arr = [...persons];
        //过滤数组
        if(searchName.trim()){
            arr =  persons.filter((p)=>p.name.indexOf(searchName) !== -1 );
        }
        //排序
        if(orderType){
            arr.sort((p1,p2)=> //es6的语法,我没学过,所以听课一脸懵,甚至还有点儿担心,前端要死了!
                if (orderType === 1){
                   return p2.age - p1.age
                   }else{
                   return p1.age-p2.age
                   }
        });
        return arr;
    }
}
//界面App.vue上的内容,从上到下依次顺序如下。
<ListRenderTwo/>
import ListRenderTwo from './components/ListRenderTwo'
components:ListRenderTwo
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值