Page effect:
Description: 1. Enter a name or age in the input box to filter the data in the table
2. Click on the name to sort the last Name field. The sort method can be reversed.
3. Click Age, you can sort the age field. The sort method can be reversed.
Code implementation:
<script> angular.module(‘myApp‘,[])
.controller(‘Aaa‘,[‘$scope‘,‘$filter‘,function($scope,$filter){ var oriArr = [
{ name : "red" , age : "20" },
{ name : "yellow" , age : "40" },
{ name : "blue" , age : "30" },
{ name : "green" , age : "10" }
];
$scope.dataList = oriArr;
$scope.fnSort = function(arg){
arguments.callee[‘fnSort‘+arg] = !arguments.callee[‘fnSort‘+arg];
$scope.dataList = $filter(‘orderBy‘)($scope.dataList , arg , arguments.callee[‘fnSort‘+arg] );
};
$scope.fnFilter = function(){
$scope.dataList = $filter(‘filter‘)( oriArr , $scope.filterVal );
};
}]); </script>
</head>
<body>
<div ng-controller="Aaa">
<input type="text" ng-model="filterVal"><input type="button" ng-click="fnFilter()" value="search">
<table border="1">
<tr>
<th ng-click="fnSort(‘name‘)">name</th>
<th ng-click="fnSort(‘age‘)">age</th>
</tr>
<tr ng-repeat="data in dataList">
<td>{{ data.name }}</td>
<td>{{ data.age }}</td>
</tr>
</table>
</div>
</body>
Sorting and filtering in the Angularjs