Sorting and filtering in the Angularjs

Source: Internet
Author: User



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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.