Angular JS Magazine

sâmbătă, 30 august 2014

Filtering for Counting

This tip shows how to count how many players listed in a table are available to play (have the available property equal to true):

<!DOCTYPE html>
<head lang="en">
    <meta charset="UTF-8">
    <script src="../angular/angular.min.js"></script>
    <link href="../bootstrap/css/bootstrap.css" rel="stylesheet" />
    <link href="../bootstrap/css/bootstrap-theme.css" rel="stylesheet" />
<div ng-app="" ng-init="atp=[
     { name: 'Nadal, Rafael (ESP)', rank: 1, available: true },
     { name: 'Djokovic, Novak (SRB)', rank: 2, available: true },
     { name: 'Federer, Roger (SUI)', rank: 3, available: false },
     { name: 'Wawrinka, Stan (SUI)', rank: 4, available: true },
     { name: 'Ferrer, David (ESP)', rank: 5, available: false }]">

    <div id="atpPanel" class="panel">
        <h3 class="panel-header">ATP SINGLES RANKINGS</h3>
        <table class="table">
            <tr ng-repeat="item in atp">
        There are <span class="label label-info">{{(atp | filter: {available: 'true'}).length}}</span> player(s) available!