AngularJS-Tabelle
- Vorherige Seite AngularJS-Http
- Nächste Seite AngularJS- Auswahl
Das ng-repeat-Attribut ist sehr gut geeignet, Tabellen anzuzeigen.
Daten in der Tabelle anzeigen
Das Anzeigen einer Tabelle mit AngularJS ist sehr einfach:
AngularJS-Beispiel
<div ng-app="myApp" ng-controller="customersCtrl"> <table> <tr ng-repeat="x in names"> <td>{{ x.Name }}</td> <td>{{ x.Country }}</td> </tr> </table> </div> <script> var app = angular.module('myApp', []); app.controller('customersCtrl', function($scope, $http) { $http.get("customers.php") .then(function (response) {$scope.names = response.data.records;}); }); </script>
Mit CSS-Style anzeigen
Um es besser aussehen zu lassen, können Sie einige CSS hinzufügen:
CSS-Style
<style> table, th , td { border: 1px solid grey; border-collapse: collapse; padding: 5px; } table tr:nth-child(odd) { background-color: #f1f1f1; } table tr:nth-child(even) { background-color: #ffffff; } </style>
Mit dem orderBy Filter anzeigen
Um die Tabelle zu sortieren, fügen Sie bitte orderBy
Filter:
AngularJS-Beispiel
<table> <tr ng-repeat="x in names | orderBy : 'Country'"> <td>{{ x.Name }}</td> <td>{{ x.Country }}</td> </tr> </table>
Mit dem uppercase Filter anzeigen
Um in Großbuchstaben anzuzeigen, fügen Sie bitte uppercase
Filter:
AngularJS-Beispiel
<table> <tr ng-repeat="x in names"> <td>{{ x.Name }}</td> <td>{{ x.Country | uppercase }}</td> </tr> </table>
Tabelleindeix anzeigen ($index)
Um die Tabelleindeix zu zeigen, fügen Sie bitte $index
der <td>:
AngularJS-Beispiel
<table> <tr ng-repeat="x in names"> <td>{{ $index + 1 }}</td> <td>{{ x.Name }}</td> <td>{{ x.Country }}</td> </tr> </table>
Verwendung von $even und $odd
AngularJS-Beispiel
<table> <tr ng-repeat="x in names"> <td ng-if="$odd" style="background-color:#f1f1f1">{{ x.Name }}</td> <td ng-if="$even">{{ x.Name }}</td> <td ng-if="$odd" style="background-color:#f1f1f1">{{ x.Country }}</td> <td ng-if="$even">{{ x.Country }}</td> </tr> </table>
- Vorherige Seite AngularJS-Http
- Nächste Seite AngularJS- Auswahl