Angular JS Magazine

vineri, 26 septembrie 2014

Custom and Built-in Filters

In this tip you can see how to combine a custom filter with a built-in filter. Suppose that we have the ATP single rankings list, and we need to crop a sub-list starting from a specified rank and returning, from that rank further, a certain number of players. For this we will use:

  • a custom filter for jumping over a number of players
  • the limitTo built-in filter, to limit the number of selected players

<!DOCTYPE html>
<html ng-app="atpModule">
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="../angular/angular.min.js"></script>
    <link href="../bootstrap/css/bootstrap.css" rel="stylesheet"/>
    <link href="../bootstrap/css/bootstrap-theme.css" rel="stylesheet"/>
    <script>
        angular.module("atpModule", [])
                .controller("atpCtrl", function ($scope) {
                })
                .filter('jumpRanks', function () {
                    return function (players, ranking) {
                        if (angular.isArray(players) && angular.isNumber(ranking)) {
                            if (ranking > players.length || ranking < 1) {
                                return players;
                            } else {
                                return players.slice(ranking);
                            }
                        }
                    };
                });

    </script>
</head>
<body>
<div ng-controller="atpCtrl" ng-init="atp=[
     { name: 'Nadal, Rafael (ESP)', rank: 1, age: '28', prizemoney: 66149345 },
     { name: 'Djokovic, Novak (SRB)', rank: 2, age: '27', prizemoney: 70704129 },
     { name: 'Federer, Roger (SUI)', rank: 3, age: '33', prizemoney: 84827704 },
     { name: 'Wawrinka, Stan (SUI)', rank: 4, age: '29', prizemoney: 13155060 },
     { name: 'Ferrer, David (ESP)', rank: 5, age: '32',  prizemoney: 24034072 },
     { name: 'Tsonga, Jo-Wilfried (FRA)', rank: 6, age: '29', prizemoney: 1708240 },
     { name: 'Simon, Gilles (FRA)', rank: 7, age: '29', prizemoney: 760469 },
     { name: 'Lopez, Feliciano (ESP)', rank: 8, age: '33', prizemoney: 1100579 },
     { name: 'Benneteau, Julien (FRA)', rank: 9, age: '32', prizemoney: 617688 },
     { name: 'Verdasco, Fernando (ESP)', rank: 10, age: '30', prizemoney: 689219 }]">

    <div id="atpPanel" class="panel">
        <h3 class="panel-header">ATP SINGLES RANKINGS</h3>
        <table class="table">
            <thead>
            <tr>
                <th>Rank</th>
                <th>Name</th>
                <th>Age</th>
                <th>Prize Money</th>
            </tr>
            </thead>
            <tbody>
            <tr ng-repeat="item in atp | jumpRanks:7 | limitTo:3">
                <td>{{item.rank}}</td>
                <td>{{item.name}}</td>
                <td>{{item.age}}</td>
                <td>{{item.prizemoney | currency:"$"}}</td>
            </tr>
            </tbody>
        </table>
    </div>
</div>
</body>
</html>

The last three players in the list:

Niciun comentariu:

Trimiteți un comentariu