首页 > 其他分享 >v-for指令--列表渲染、列表过滤(computed和watch)、列表排序

v-for指令--列表渲染、列表过滤(computed和watch)、列表排序

时间:2022-12-02 18:03:13浏览次数:38  
标签:computed -- age sex reactive let 列表 id name

v-for指令
1.用于展示列表数据
2.语法:v-for="(item,index) in xxx" :key="yyy"
3.可遍历:数组、对象、字符串、指定次数

<template>
<!-- 遍历数组 -->
<h2>列表渲染</h2>
<ul>
    <li v-for="p in persons" :key="p.id">{{p.name}}-----{{p.age}}</li>
</ul>
<!-- 遍历对象 -->
<h2>对象渲染</h2>
<ul>
    <li v-for="(value,keys) of cars" :key="keys">{{value}}-----{{keys}}</li>
</ul>
<!-- 遍历字符串 -->
<h2>遍历字符串</h2>
<ul>
    <li v-for="(value,index) of str" :key="index">{{value}}--{{index}}</li>
</ul>
<!-- 遍历指定次数 -->
<h2>遍历指定次数</h2>
<ul>
    <li v-for="(number,index) of 5" :key="index">{{number}}--{{index}}</li>
</ul>
</template>
<script setup>

import { watch,ref,reactive} from 'vue'
const persons=reactive([
    {id:1,name:'jhon',age:18},
    {id:2,name:'jack',age:19},
    {id:3,name:'simos',age:20}
])
const cars=reactive({
    name:'奥迪A8',
    price:'70w',
    color:'黑色'
})
const str=ref('hello')

</script>

列表过滤
1.通过watch监视完成过滤

<template>
<!-- 遍历数组 -->
<h2>列表渲染中key的原理</h2>
        <input type="text" placeholder="请输入需要搜索的名" 
        v-model="keyWords"
        />
<ul>
    <li v-for="p in filePersons" :key="p.id">
        {{p.name}}-----{{p.age}}-----{{p.sex}}

        </li>
    
</ul>


</template>
<script setup>

import { watch,ref,reactive} from 'vue'
let keyWords=ref('')
let persons=reactive([
    {id:1,name:'周杰伦',age:18,sex:'男'},
    {id:2,name:'马冬梅',age:19,sex:'女'},
    {id:3,name:'周冬雨',age:18,sex:'男'},
    {id:4,name:'夏雨',age:19,sex:'女'},
])
let filePersons=reactive([])
//使用watch方式进行模糊查询
watch(keyWords,(newValue,oldValue)=>{
   filePersons=persons.filter((item)=>item.name.indexOf(newValue)!==-1)
},{immediate:true})
</script>

2.通过computed计算属性完成过滤

<template>
<!-- 遍历数组 -->
<h2>列表渲染中key的原理</h2>
        <input type="text" placeholder="请输入需要搜索的名" 
        v-model="keyWords"
        />
<ul>
    <li v-for="p in filePersons" :key="p.id">
        {{p.name}}-----{{p.age}}-----{{p.sex}}

        </li>
    
</ul>


</template>
<script setup>

import { watch,ref,reactive, computed} from 'vue'
let keyWords=ref('')
let persons=reactive([
    {id:1,name:'周杰伦',age:18,sex:'男'},
    {id:2,name:'马冬梅',age:19,sex:'女'},
    {id:3,name:'周冬雨',age:18,sex:'男'},
    {id:4,name:'夏雨',age:19,sex:'女'},
])
let filePersons=computed(()=>{
    return persons.filter((item)=>item.name.indexOf(keyWords.value)!==-1)
})

</script>

列表排序

<template>
<!-- 遍历数组 -->
<h2>列表渲染中key的原理</h2>
        <input type="text" placeholder="请输入需要搜索的名" 
        v-model="keyWords"
        />
        <button @click="sortType=2">年龄升序</button>
        <button @click="sortType=1">年龄降序</button>
        <button @click="sortType=0">年龄原序</button>
<ul>
    <li v-for="p in filePersons" :key="p.id">
        {{p.name}}-----{{p.age}}-----{{p.sex}}

        </li>
    
</ul>


</template>
<script setup>

import { watch,ref,reactive, computed} from 'vue'
let keyWords=ref('')
let sortType=ref(0)//0代表原顺序,1代表降序,2代表升序
let persons=reactive([
    {id:1,name:'周杰伦',age:18,sex:'男'},
    {id:2,name:'马冬梅',age:129,sex:'女'},
    {id:3,name:'周冬雨',age:28,sex:'男'},
    {id:4,name:'夏雨',age:19,sex:'女'},
])
let filePersons=computed(()=>{
    
    let arr=persons.filter((item)=>item.name.indexOf(keyWords.value)!==-1)
    if(sortType.value){
        arr.sort((a,b)=>{
            return sortType.value===1?b.age-a.age:a.age-b.age
        })
    }
    return arr
})

</script>

 

标签:computed,--,age,sex,reactive,let,列表,id,name
From: https://www.cnblogs.com/shuchenhao/p/16945225.html

相关文章

  • MXD文件版本批量转换
    ArcGIS不同版本的mxd文件不兼容,例如我这里本地使用的是10.1的版本,无法打开10.5版本创建的mxd文件。如果是较少的mxd文件,可以通过在10.5版本的arcgis中打开,选择FILE—SAVEA......
  • Vue.js_Vue Router 4.x 动态路由解决刷新空白
    问题描述:基于对VueRouter3.x没有改变前,我们常规的实现一定,在store中根据获取的用户权限,对路由进行过滤并返回,然后到路由守卫的地方,使用addRoutes动态添加路由。但......
  • About this book (Code like pro in C#)
    本书以您现有的编程技能为基础,帮助您无缝提升您的编码实践或从Java或其他面向对象语言过渡到C#。您将学习编写对企业开发至关重要的惯用C#代码。这本书讨论了基本的后端技......
  • 解决跨域Access-Control-Allow-Origin 设置无效问题
    解决跨域Access-Control-Allow-Origin设置无效问题在开发或使用别人项目的过程中,经常会遇到跨域访问失败的问题,解决这种问题的方法也很简单,就是在nginx或web框架中加入跨......
  • Yii2.0 的多选框实现方法
    下面介绍一下Yii2.0的多选框实现方法第一种:ActiveForm::checkboxList(); 优点:可以将全部数据生成多选框,自带验证$form->field($model,'username')->checkboxList(Ar......
  • 网卡绑定bond0的实现
    1、虚拟机添加一张网卡  2、通过ipa可以看到多了一张eth1网卡  3、创建bond0[root@rocky8-1~]#nmcliconnectionaddtypebondcon-namemybond0ifnameb......
  • 分享国内常用的MD5在线解密网站,这5个网站很实用
    从事网络安全这一行业的人都知道,我们做渗tou测试时经常会遇到需要解密的Md5密文。为了能够提升效率,我们可以在网上找一些能够在线md5解密的网站。下面给大家分享国内常用的......
  • 启动脚本
    vim/etc/init.d/flink#!/bin/bash#chkconfig:23452090#description:kafka#processname:kafkaexportJAVA_HOME=//usr/local/jdkcase$1instart)/usr/......
  • 分享国内常用的MD5在线解密网站,这5个网站很实用
    从事网络安全这一行业的人都知道,我们做渗tou测试时经常会遇到需要解密的Md5密文。为了能够提升效率,我们可以在网上找一些能够在线md5解密的网站。下面给大家分享国内常用的......
  • P3049 [USACO12MAR]Landscaping S
    这篇博客我要单独写!思想:贪心的把到当前位置先补齐假设:i,j,k,p是位置i  j  k  p少 多 少  多-------------------------------------------------......