Angular JS Magazine

duminică, 24 august 2014

Conditionally Swapping Elements

In order to switch between smaller chucks of content you can use the ng-switch directive. Here it is an example:

<!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.selbtn = {};
                });
    </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)' }]">

    <div id="atpPanel" class="panel">
        <div class="well">
            <div class="radio" ng-repeat="radiobtn in ['None', 'Short', 'Extended', 'Detalied']">
                <label>
                    <input type="radio" ng-model="selbtn.radio"
                           value="{{radiobtn}}" ng-checked="$first"/>
                    {{radiobtn}}
                </label>
            </div>
        </div>
        <h3 class="panel-header">ATP SINGLES RANKINGS</h3>

        <div ng-switch on="selbtn.radio">
            <div ng-switch-default>
                Select how to display data ...
            </div>
            <div ng-switch-when="Short">
                <table class="table">
                    <thead>
                    <tr>
                        <th>Name</th>
                        <th>Rank</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr ng-repeat="item in atp">
                        <td>{{item.name}}</td>
                        <td>{{item.rank}}</td>
                    </tr>
                    </tbody>
                </table>
            </div>
            <div ng-switch-when="Extended">
                <table class="table">
                    <thead>
                    <tr>
                        <th>Name</th>
                        <th>Rank</th>
                        <th>Age</th>
                        <th>Birthplace</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr ng-repeat="item in atp">
                        <td>{{item.name}}</td>
                        <td>{{item.rank}}</td>
                        <td>{{item.age}}</td>
                        <td>{{item.birthplace}}</td>
                    </tr>
                    </tbody>
                </table>
            </div>
            <div ng-switch-when="Detalied">
                <table class="table">
                    <thead>
                    <tr>
                        <th>Name</th>
                        <th>Rank</th>
                        <th>Age</th>
                        <th>Birthplace</th>
                        <th>Height</th>
                        <th>Weight</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr ng-repeat="item in atp">
                        <td>{{item.name}}</td>
                        <td>{{item.rank}}</td>
                        <td>{{item.age}}</td>
                        <td>{{item.birthplace}}</td>
                        <td>{{item.height}}</td>
                        <td>{{item.weight}}</td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
</body>
</html>





Niciun comentariu:

Trimiteți un comentariu