zoukankan      html  css  js  c++  java
  • AngularJS orderBy 使用要点

    AngularJS orderBy 使用要点总结:

      1,书写格式

    基本应用格式为:

    ng-repeat="item in itemList | orderBy:p1:p2"

    参数p1可以是字段名或自定义函数,p2指是否逆序,默认是false

    举例:

    假设$scope中有

    $scope.itemList=[{id:201,name:'abc',amount:100},{id:100,name:'zdb',amount:100},
    {id:10,name:'xxx',amount:200},{id:80,name:'231',amount:1020},
    {id:50,name:'ppp',amount:20},{id:1,name:'hhh',amount:1100}];

    按照id,倒排序

    ng-repeat="item in itemList | orderBy:'id':true"
    

        2,自定义排序:

    controller中设置自定义函数,函数接受参数为当前的item,需要返回一个数值代表该item的顺序

    $scope.orderIt=function(item){
        if(item.name.indexOf('h')===0)return 0;
        return 1;
    };

    使用方法:

    ng-repeat="item in itemList | orderBy:orderIt"

      

      3,多字段排序:

    如果单个字段排序不能满足需求,那就要祭出绝活了!orderBy还支持多字段排序,方法如下

    ng-repeat="item in itemList | orderBy:[orderIt,'name','-amount']

    没错,第一个参数传递数组,可以是自定义函数或字段名,字段名前面加“-”,代表倒排序。

  • 相关阅读:
    移动布局---1. 移动端布局基础
    1. CSS新特性之选择器
    1. H5新增语义化标签
    POJ 3281
    poj 1986
    POJ 3728
    poj 2763
    poj 2749
    uva 11294
    LA 3713
  • 原文地址:https://www.cnblogs.com/dajianshi/p/4484237.html
Copyright © 2011-2022 走看看