• 【尚硅谷 Vue学习笔记】p31列表过滤 p32列表排序


    p31列表过滤

    最终效果:

    image-20221117085725270

    拿监视(侦听)属性写:

    版本一:

    <body>
      <!--准备好一个容器-->
      <div id="root">
        <input type="text" placeholder="请输入名字" v-model="keyWord">
        <ul>
          <li v-for="p in persons" :key="p.id">
            {{p.name}}-{{p.age}}--{{p.sex}}
          </li>
        </ul>
      </div>
    
      <script type="text/javascript">
        Vue.config.productionTip = false;
    
        new Vue({
          el: '#root',
          data: {
            keyWord:'',
            persons: [
              { id: '001', name: '马冬梅', age: 19,sex:'女'},
              { id: '002', name: '周冬雨', age: 20,sex:'女' },
              { id: '003', name: '周杰伦', age: 21,sex:'男' },
              { id: '003', name: '温兆伦', age: 22,sex:'男' }
            ]
          },
          watch:{
            keyWord(val){
              this.persons=this.persons.filter((p)=>{
                return p.name.indexOf(val) !==-1
              })
            }
          }
        })
      </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    image-20221117091947287

    版本二:

    <body>
      <!--准备好一个容器-->
      <div id="root">
        <input type="text" placeholder="请输入名字" v-model="keyWord">
        <ul>
          <li v-for="p in filPersons" :key="p.id">
            {{p.name}}-{{p.age}}--{{p.sex}}
          </li>
        </ul>
      </div>
    
      <script type="text/javascript">
        Vue.config.productionTip = false;
    
        new Vue({
          el: '#root',
          data: {
            keyWord:'',
            persons: [
              { id: '001', name: '马冬梅', age: 19,sex:'女'},
              { id: '002', name: '周冬雨', age: 20,sex:'女' },
              { id: '003', name: '周杰伦', age: 21,sex:'男' },
              { id: '003', name: '温兆伦', age: 22,sex:'男' }
            ],
            filPersons:[]
          },
          watch:{
            keyWord(val){
              this.filPersons=this.persons.filter((p)=>{
                return p.name.indexOf(val) !==-1
              })
            }
          }
        })
      </script>
    </body>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    image-20221117092301217

    有个小问题就是刚开始的时候什么也不显示。

    解决方案:

    在watch里面加:
    immediate:true
    功能:立即实现
    
    • 1
    • 2
    • 3

    image-20221117093225705

    用watch实现的代码:

    <!DOCTYPE html>
    <html>
    
    <head>
      <meta charset="UTF-8">
      <!--引入Vue-->
      <script type="text/javascript" src="../js/vue.js"></script>
      <title></title>
    </head>
    
    <body>
      <!--准备好一个容器-->
      <div id="root">
        <input type="text" placeholder="请输入名字" v-model="keyWord">
        <ul>
          <li v-for="p in filPersons" :key="p.id">
            {{p.name}}-{{p.age}}--{{p.sex}}
          </li>
        </ul>
      </div>
    
      <script type="text/javascript">
        Vue.config.productionTip = false;
    
        new Vue({
          el: '#root',
          data: {
            keyWord: '',
            persons: [
              { id: '001', name: '马冬梅', age: 19, sex: '女' },
              { id: '002', name: '周冬雨', age: 20, sex: '女' },
              { id: '003', name: '周杰伦', age: 21, sex: '男' },
              { id: '003', name: '温兆伦', age: 22, sex: '男' }
            ],
            filPersons: []
          },
          watch: {
            keyWord: {
              immediate: true,
              handler(val) {
                this.filPersons = this.persons.filter((p) => {
                  return p.name.indexOf(val) !== -1
                })
              }
            }
          }
            
        })
      </script>
    </body>
    
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    插播:随便折叠代码

    #region

    #endregion

    计算属性实现的代码:

    computed:{
            filPersons(){
              return this.persons.filter((p)=>{
                return p.name.indexOf(this.keyWord)!==-1
              })
            }
          }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    image-20221117142013909

    p32列表排序

    image-20221117142724398

    sort:

    image-20221117143304587

    computed:{
            filPersons(){
              const arr= this.persons.filter((p)=>{
                return p.name.indexOf(this.keyWord)!==-1
              })
              //判断一下是否需要排序
              if(this.sortType){
                arr.sort((a,b)=>{
                  return this.sortType===1?b.age-a.age:a.age-b.age
                })  
              }
              return arr
            }
          }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    入门Echarts数据可视化:从基础到实践
    微信小程序获取公众号的文章
    第一次接触web前端开发
    Golang goroutine 进程、线程、并发、并行
    2023.11.16 hivesql高阶函数之开窗函数
    1.6 CAN通信 F28335-Simulink仿真自动代码生成
    基于DNN深度学习网络的OFDM信号检测算法的matlab仿真,对比LS和MMSE两个算法
    【事务代码】MF60-拉料清单
    [MindSpore]ValueError: loss_fn can not be None
    嵌入式系统开发笔记95:安装STM32CubeIDE
  • 原文地址:https://blog.csdn.net/m0_54381284/article/details/127903924