Angular JS Magazine

sâmbătă, 20 septembrie 2014

Selecting Items via filter Filter

The built-in filter fitler can be used to select the items based on different selection criteria. The selection criteria can be indicated using an expression, a map object used to match property values, or using a function. The map and function case are exemplified below:

<!DOCTYPE html>
<html>
<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) {
                    $scope.selectPlayers = function (item) {
                        return (item.height == "185 cm" || item.weight == "183 lbs (83 kg)");
                    };
                });
    </script>
</head>
<body>
<div ng-app="atpModule" ng-controller="atpCtrl" ng-init="atp=[
     { name: 'Nadal, Rafael (ESP)', rank: 1, age: '28 (03.06.1986)', birthplace: 'Manacor, Mallorca, Spain', height: '185 cm', weight: '188 lbs (85 kg)' },
     { name: 'Djokovic, Novak (SRB)', rank: 2, age: '27 (22.05.1987)', birthplace: 'Belgrade, Serbia', height: '188 cm', weight: '176 lbs (80 kg)' },
     { name: 'Federer, Roger (SUI)', rank: 3, age: '33 (08.08.1981)', birthplace: 'Basel, Switzerland', height: '185 cm', weight: '187 lbs (85 kg)' },
     { name: 'Wawrinka, Stan (SUI)', rank: 4, age: '29 (28.03.1985)', birthplace: 'Lausanne, Switzerland', height: '183 cm', weight: '179 lbs (81 kg)' },
     { name: 'Ferrer, David (ESP)', rank: 5, age: '32 (02.04.1982)', birthplace: 'Javea, Spain', height: '175 cm', weight: '160 lbs (73 kg)' },
     { name: 'Tsonga, Jo-Wilfried (FRA)', rank: 11, age: '29 (17.04.1985)', birthplace: 'Le Mans, France', height: '188 cm', weight: '200 lbs (91 kg)' },
     { name: 'Simon, Gilles (FRA)', rank: 26, age: '29 (27.12.1984)', birthplace: 'Nice, France', height: '183 cm', weight: '154 lbs (70 kg)' },
     { name: 'Mayer, Leonardo (ARG)', rank: 25, age: '27 (15.05.1987)', birthplace: 'Corrientes, Argentina', height: '188 cm', weight: '183 lbs (83 kg)' }]">

    <div id="atpPanel" class="panel">
        <h3 class="panel-header">ATP SINGLES RANKINGS</h3>
        <span class="label label-primary">Display players with height equal with 188 cm</span>
        <table class="table">
            <thead>
            <tr>
                <th>Rank</th>
                <th>Name</th>
                <th>Weight</th>
                <th>Height</th>
            </tr>
            </thead>
            <tbody>
            <tr ng-repeat="item in atp | filter:{height: '188 cm'}">
                <td>{{item.rank}}</td>
                <td>{{item.name}}</td>
                <td>{{item.weight}}</td>
                <td>{{item.height}}</td>
            </tr>
            </tbody>
        </table>
        <span class="label label-primary">Display players with height equal with 185 cm or weight equal with 183 lbs (83 kg)</span>
        <table class="table">
            <thead>
            <tr>
                <th>Rank</th>
                <th>Name</th>
                <th>Weight</th>
                <th>Height</th>
            </tr>
            </thead>
            <tbody>
            <tr ng-repeat="item in atp | filter: selectPlayers">
                <td>{{item.rank}}</td>
                <td>{{item.name}}</td>
                <td>{{item.weight}}</td>
                <td>{{item.height}}</td>
            </tr>
            </tbody>
        </table>
    </div>
</div>
</body>
</html>