zoukankan      html  css  js  c++  java
  • 列表的搜索和排序

    1、案例1

    <!DOCTYPE html>
    <html>
    
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>列表的搜索和排序</title>
    </head>
    <!--
        1、根据输入框中输入的条件对数组进行过滤
        2、对过滤后的结果按照指定顺序排序
    -->
    
    <body>
        <div id="app">
            <input type="text" v-model="searchName" />
            <ul>
                <li v-for="(item,index) in filterPersons">
                    {{index}} --- {{item.name}} --- {{item.age}}
                </li>
            </ul>
            <button @click="clickFunc(1)">年龄升序</button>
            <button @click="clickFunc(2)">年龄降序</button>
            <button @click="clickFunc(0)">原本顺序</button>
            <button @click="clickFunc2">改变persons</button>
        </div>
        <script src="../js/vue.js" type="text/javascript"></script>
        <script>
            const vm = new Vue({
                el: "#app",
                data: {
                    orderType: 0, //默认是原本顺序
                    searchName: "",
                    persons: [
                        { name: "liuyang", age: 28 },
                        { name: "yangjing", age: 19 },
                        { name: "zhangsan", age: 25 },
                        { name: "xiaoming", age: 30 },
                    ]
                },
                methods: {
                    clickFunc(param) {
                        this.orderType = param;
                    },
                    clickFunc2() {
                        //同样会触发filterPersons()方法重新计算filterPersons的值
                        this.persons.splice(0, 1);
                    }
                },
                computed: {
                    /*
                    filterPersons是根据orderType、searchName、persons
                    三个相关数据计算而来的,只要这三个有改变就会重新计算filterPersons
                    的值
                    */
                    filterPersons() {
                        /*
                        等效于:
                        const orderType = this.orderType;
                        const searchName = this.searchName;
                        const persons = this.persons;
                        */
                        const { orderType, searchName, persons } = this;
                        //p是persons的每个元素
                        //返回true就是过滤后我们需要的数据
                        let results = persons.filter(p => p.name.indexOf(searchName) !== -1);
                        results.sort(function (a, b) {
                            if (orderType === 1) {
                                //升序
                                return a.age - b.age
                            } else if (orderType === 2) {
                                //降序
                                return b.age - a.age
                            }
                        });
                        return results;
                    }
                }
            });
        </script>
    </body>
    
    </html>
  • 相关阅读:
    前端常见跨域解决方案
    VS单元测试--初级篇
    高等数学思路
    二元函数求极值判别式AC-B^2
    向量积详解
    伯努利分布均值和方差
    两个标准正态随机变量相乘的方差
    a分位数与双侧a分位数
    中心极限定理概念理解与记忆
    样本方差概念解析
  • 原文地址:https://www.cnblogs.com/liuyang-520/p/12458466.html
Copyright © 2011-2022 走看看