Angular JS Magazine

sâmbătă, 27 septembrie 2014

Custom Filter For Table Data

In this tip you can see a custom filter capable to filter the tennis players based on the tournament where they play.

<!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"/>
    <link href="../bootstrap/css/bootstrap-glyphicons.css" rel="stylesheet"/>
    <script>
        angular.module("atpModule", [])
                .controller("atpCtrl", function ($scope, $filter) {

                    $scope.setSelectedTournament = function (id) {
                        var idInTournament = $filter("inTournament")($scope.selectedTournaments, id);
                        if (idInTournament != -1) {
                            $scope.selectedTournaments.splice(idInTournament, 1);
                        } else {
                            $scope.selectedTournaments.push(id);
                        }
                        return false;
                    };
                    $scope.isChecked = function (id) {
                        if ($filter("inTournament")($scope.selectedTournaments, id) != -1) {
                            return 'btn glyphicon glyphicon-check';
                        }
                        return false;
                    };
                    $scope.checkAll = function () {
                        $scope.selectedTournaments = [];
                        angular.forEach($scope.tournaments, function (tournament) {
                            $scope.selectedTournaments.push(tournament.id);
                        });
                    };
                })
                .filter('inTournament', function () {
                    return function (items, value) {
                        if (angular.isArray(items)) {
                            return items.indexOf(value);
                        } else {
                            return -1;
                        }
                    }
                })
                .filter('tournamentFilter', function () {
                    return function (players, items) {
                        if (!angular.isUndefined(players) &&
                                 !angular.isUndefined(items) && items.length > 0) {
                            var tempPlayers = [];
                            angular.forEach(items, function (id) {
                                angular.forEach(players, function (player) {
                                    if (angular.equals(player.tournament.id, id)) {
                                        tempPlayers.push(player);
                                    }
                                });
                            });
                            return tempPlayers;
                        } else {
                            return players;
                        }
                    };
                });
    </script>
</head>
<body>
<div ng-controller="atpCtrl"
     ng-init="selectedTournaments = [];
    tournaments =[
                            {id: 1, name: 'Sydney'}, {id: 2, name: 'Basel'}, {id: 3,name: 'Mumbai'}];
    atp=[
                   { name: 'Nadal, Rafael (ESP)', rank: 1, age: '28', prizemoney: 66149345,
                                                                                 tournament: {id: 1, name: 'Sydney'} },
                  { name: 'Djokovic, Novak (SRB)', rank: 2, age: '27', prizemoney: 70704129,
                                                                                 tournament: {id: 2, name: 'Basel'} },
                  { name: 'Federer, Roger (SUI)', rank: 3, age: '33', prizemoney: 84827704,
                                                                                 tournament: {id: 3, name: 'Mumbai'} },
                  { name: 'Wawrinka, Stan (SUI)', rank: 4, age: '29', prizemoney: 13155060,
                                                                                  tournament: {id: 3, name: 'Mumbai'} },
                  { name: 'Ferrer, David (ESP)', rank: 5, age: '32',  prizemoney: 24034072,
                                                                                  tournament: {id: 1, name: 'Sydney'} },
                  { name: 'Tsonga, Jo-Wilfried (FRA)', rank: 6, age: '29', prizemoney: 1708240,                                                                                                                                     tournament: {id: 3, name: 'Mumbai'} },
                  { name: 'Simon, Gilles (FRA)', rank: 7, age: '29', prizemoney: 760469,
                                                                                 tournament: {id: 3, name: 'Mumbai'} },
                  { name: 'Lopez, Feliciano (ESP)', rank: 8, age: '33', prizemoney: 1100579,
                                                                                 tournament: {id: 2, name: 'Basel'} },
                  { name: 'Benneteau, Julien (FRA)', rank: 9, age: '32', prizemoney: 617688,
                                                                                 tournament: {id: 1, name: 'Sydney'} },
                  { name: 'Verdasco, Fernando (ESP)', rank: 10, age: '30', prizemoney: 689219,
                                                                                 tournament: {id: 1, name: 'Sydney'} }]">

    <div>
        <ul class="inline">
            <li>
                <div class="alert alert-info">
                    <h4>Total Tournaments: {{tournaments.length}}</h4>

                </div>
            </li>
            <li>
                <div class="btn-group" data-ng-class="{open: open}">
                    <button class="btn">Filter by Tournament</button>
                    <button class="btn dropdown-toggle" data-ng-click="open=!open">
                            <span class="caret"></span>
                    </button>
                    <ul class="dropdown-menu" aria-labelledby="dropdownMenu">
                        <li>
                            <a ng-click="checkAll()">
                             <i class="glyphicon glyphicon-ok-sign"></i> Check All
                            </a>
                        </li>
                        <li>
                            <a data-ng-click="selectedTournaments=[];">
                              <i class="glyphicon glyphicon-remove-sign"></i> Uncheck All                                                                                           </a>
                        </li>
                        <li class="divider"></li>
                        <li data-ng-repeat="item in tournaments">
                            <div>
                                <a class="btn" ng-click="setSelectedTournament(item.id)">{{item.name}}</a>

                                <div ng-class="isChecked(item.id)"
                                     style="position: relative;float:right;padding-right:5px;"></div>
                            </div>
                        </li>
                    </ul>
                </div>
            </li>
        </ul>
        <hr/>
        <h3>Players/Tournaments Table:</h3>

        <table class="table table-hover table-striped">
            <thead>
            <tr>
                <th>Rank</th>
                <th>Name</th>
                <th>Age</th>
                <th>Prize Money</th>
                <th>Tournament</th>
            </tr>
            </thead>
            <tbody>
            <tr data-ng-repeat="item in filtered = (atp | tournamentFilter:selectedTournaments)">
                <td>{{item.rank}}</td>
                <td>{{item.name}}</td>
                <td>{{item.age}}</td>
                <td>{{item.prizemoney}}</td>
                <td>{{item.tournament.name}}</td>
            </tr>
            </tbody>
        </table>
    </div>
</div>
</body>
</html>



Niciun comentariu:

Trimiteți un comentariu