Angular JS Magazine

luni, 22 septembrie 2014

Sorting Items - part 1

In this tip you can see how to use the orderBy built-in Angular filter for sorting asc/dsc a list of players by name:

<!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"/>
</head>
<body>
<div ng-app="" 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">Sort players by names (ASC)</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 | orderBy:'name'">
                <td>{{item.rank}}</td>
                <td>{{item.name}}</td>
                <td>{{item.weight}}</td>
                <td>{{item.height}}</td>
            </tr>
            </tbody>
        </table>
        <span class="label label-primary">Sort players by names (DSC)</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 | orderBy:'-name'">
                <td>{{item.rank}}</td>
                <td>{{item.name}}</td>
                <td>{{item.weight}}</td>
                <td>{{item.height}}</td>
            </tr>
            </tbody>
        </table>
    </div>
</div>
</body>
</html>