前端Vue学习笔记(二)

重点:splice() filter() sort() indexOf()

<template>
  <div>
    <input type="text" v-model="searchP">
  <ul>
    <li v-for="(person,index) in filterPersons" :key="index">
      {{index}}---{{person.name}}--{{person.age}}
      <button @click="deleteP(index)">删除</button>
      <button @click="updateP(index,{'name':'dage',age:12})">更新</button>
    </li>
  </ul>
    <button @click="setOrderType(1)">升序</button>
    <button @click="setOrderType(2)">降序</button>
    <button @click="setOrderType(0)">原来排序</button>
  </div>
</template>

<script>
    export default {
        name: "testTwo",
      data(){
          return{
            searchP:'',
            orderType:0,
          persons:[{
            name:'kai',
            age:8
          },{
            name:'kenan',
            age:44
          },{
            name:'fanzhang',
            age:90
          },{
            name:'libai',
            age:34
          }]
      }
      },
      methods:{
          deleteP(index){
            this.persons.splice(index,1)
          },
        updateP(index,person){
            this.persons.splice(index,1,person)
        },
        setOrderType(value){
            this.orderType = value;
        }
      },
      computed:{
        filterPersons(){
          const {searchP,persons,orderType} = this;
          let filterPP =persons.filter(p=>p.name.indexOf(searchP)>=0)
          if (orderType!==0){
            filterPP.sort(function (p1,p2) {
              //1是升序  2是降序
              if (orderType===1){
                return p1.age-p2.age;
              }else {
                return p2.age-p1.age;
              }
            })
          }
          return filterPP;
        }
      }
    }
</script>

<style scoped>

</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值