AngularJS-Tabellen


Die Direktive ng-repeat eignet sich perfekt zum Anzeigen von Tabellen.


Anzeigen von Daten in einer Tabelle

Das Anzeigen von Tabellen mit Winkeln 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>

Anzeige mit CSS-Stil

Um es schön zu machen, fügen Sie der Seite etwas CSS hinzu:

CSS-Stil

<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>


Anzeige mit orderBy-Filter

Um die Tabelle zu sortieren, fügen Sie einen orderBy -Filter hinzu: 

AngularJS-Beispiel

<table>
  <tr ng-repeat="x in names | orderBy : 'Country'">
    <td>{{ x.Name }}</td>
    <td>{{ x.Country }}</td>
  </tr>
</table>

Anzeige mit Großbuchstabenfilter

Um Großbuchstaben anzuzeigen, fügen Sie einen Großbuchstabenfilter hinzu : 

AngularJS-Beispiel

<table>
  <tr ng-repeat="x in names">
    <td>{{ x.Name }}</td>
    <td>{{ x.Country | uppercase }}</td>
  </tr>
</table>

Anzeigen des Tabellenindex ($index)

Um den Tabellenindex anzuzeigen, fügen Sie ein <td> mit $index hinzu : 

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>