zoukankan      html  css  js  c++  java
  • vue里数组排序示例

    vue的默认排序方式
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
      </head>
      <body>
        <div id="app">
          <template v-for="user in userInfo">
            <li>姓名:{{user}}</li>
          </template>
        </div>
        <script>
            var v=new Vue({
                el:"#app",
                data:{
                    userInfo:['zhangsan','aisi','wangwu']
                }
            });
           v.userInfo.sort();
        </script>
      </body>
    </html>
     
    改写排序方式的示例
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
      </head>
      <body>
        <div id="app">
          <template v-for="user in userInfoSorted">
            <li>姓名:{{user.name}}</li>
          </template>
        </div>
        <script>
          var v = new Vue({
            el: "#app",
            data: {
              userInfo: [
                { name: "zhangsan" },
                { name: "aisi" },
                { name: "wangwu" },
              ],
            },
            computed: {
              userInfoSortedfunction () {
    // a,b参数代表的是user 按照 长度降序排序
                return this.userInfo.sort(function (a, b) {
                  return -(a.name.length - b.name.length);
                });
              },
            },
          });
        </script>
      </body>
    </html>
  • 相关阅读:
    常用 SQL Server 规范集锦
    让Git忽略所有obj和bin目录的同步
    Sql server 存储过程基础语法
    nginx 站点代理,负载均衡
    CentOS7.0安装Nginx-1.12.0
    CentOS7安装GNOME可视化界面和如何配置IP地址
    开发工具资料集合
    NOIP2018总结反思
    NOIP2018考试报告
    STL基础用法
  • 原文地址:https://www.cnblogs.com/kukai/p/12857743.html
Copyright © 2011-2022 走看看