AngularJS中的过滤器filter用法完全解析(2)

yourApp.filter('orderObjectBy', function() { return function(items, field, reverse) { var filtered = []; angular.forEach(items, function(item) { filtered.push(item); }); filtered.sort(function (a, b) { return (a[field] > b[field] ? 1 : -1); }); if(reverse) filtered.reverse(); return filtered; }; });

该过滤器将对象转换成标准的数组并把它通过您指定字段排序。您可以使用orderObjectBy过滤器酷似ORDERBY,包括字段名后一个布尔值,以指定的顺序是否应该得到扭转。换句话说,假的是升序,真正的下降。html调用

<li ng-repeat="item in items | orderObjectBy:'color':true">{{ item.color }}</li>


排序搜索

<input type="text" ng-model="search" placeholder="Search"> <thead> <tr> <!-- ng-class="{dropup:true}" --> <th ng-click="changeOrder('id')" ng-class="{dropup: order === ''}"> 产品编号 <span ng-class="{orderColor: orderType === 'id'}"></span> </th> <th ng-click="changeOrder('name')" ng-class="{dropup: order === ''}"> 产品名称 <span ng-class="{orderColor: orderType === 'name'}"></span> </th> <th ng-click="changeOrder('price')" ng-class="{dropup: order === ''}"> 产品价格 <span ng-class="{orderColor: orderType === 'price'}"></span> </th> </tr> </thead> <tbody> <tr ng-repeat="item in productData | filter: search | orderBy:order + orderType"> <td>{{item.id}}</td> <td>{{item.name}}</td> <td>{{item.price | currency: '¥'}}</td> </tr> </tbody>

angularjs

//默认排序字段 $scope.orderType = 'id'; $scope.order = '-'; $scope.changeOrder = function(type) { console.log(type); $scope.orderType = type; if ($scope.order === '') { $scope.order = '-'; }else{ $scope.order = ''; } }

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgywwy.html