前端框架Vue----数组的过滤与排序

目录

  • 数组过滤
  • 数组的排序

数组过滤

通过输入关键字,实现一个搜索的功能。
原始页面:
前端框架Vue----数组的过滤与排序_第1张图片
 
搜索页面:
前端框架Vue----数组的过滤与排序_第2张图片

实现思路:计算属性

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>列表过滤title>
    <script src="/static/js/vue.js">script>
head>
<body>
    <div id="container">
        <input type="text" placeholder="输入搜索" v-model="keyword">
        <ul>
            
            <li v-for="p in search" :key="p.id">{{ p.name }}--{{ p.age }}li>
        ul>
    div>

    <script>
        Vue.config.productionTip = false

        new Vue({
            el: "#container",
            data: {
                keyword: "",
                persons: [
                    {id:"001", name:"jack", age:23},
                    {id:"002", name:"tom", age:18},
                    {id:"003", name:"lucy", age:19},
                ],
            },
            computed: {
                search(){//渲染页面,则第一次获取,执行
                    return this.persons.filter(p=>{
                        return p.name.indexOf(this.keyword) >= 0
                    })
                },
            },
        })
    script>
body>
html>

数组的排序

数组的排序

const arr = [4,3,2,1,5,0]
//sort arr self
arr.sort((a,b)=>{
	return a - b   //升序
})

对搜索的结果,按照年龄排序:
前端框架Vue----数组的过滤与排序_第3张图片
实现代码:

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>列表过滤title>
    <script src="/static/js/vue.js">script>
head>
<body>
    <div id="container">
        <input type="text" placeholder="输入搜索" v-model="keyword">
        <button @click="sortType=1">升序button>
        <button @click="sortType=2">降序button>
        
        <ul>
            
            <li v-for="p in search" :key="p.id">{{ p.name }}--{{ p.age }}li>
        ul>
    div>

    <script>
        Vue.config.productionTip = false

        new Vue({
            el: "#container",
            data: {
                keyword: "",
                sortType: 0,
                persons: [
                    {id:"001", name:"jack", age:23},
                    {id:"002", name:"tom", age:18},
                    {id:"003", name:"lucy", age:19},
                    {id:"004", name:"jom", age:25},
                    {id:"005", name:"jacy", age:14},
                ],
            },
            computed: {
                search(){//渲染页面,则第一次获取,执行
                    const arr = this.persons.filter(p=>{
                        return p.name.indexOf(this.keyword) >= 0
                    })
                    
                    //数组的排序 !!!
                    arr.sort((a,b)=>{
                        return this.sortType==1?a.age-b.age:b.age-a.age
                    })

                    return arr
                    
                },
            },
        })
    script>
body>
html>

你可能感兴趣的:(前端框架Vue,vue.js,前端框架,数组,排序,过滤)