Tabel AngularJS


Direktif ng-repeat sangat cocok untuk menampilkan tabel.


Menampilkan Data dalam Tabel

Menampilkan tabel dengan sudut sangat sederhana:

Contoh AngularJS

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

Menampilkan dengan Gaya CSS

Untuk membuatnya bagus, tambahkan beberapa CSS ke halaman:

Gaya CSS

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


Tampilan dengan orderBy Filter

Untuk mengurutkan tabel, tambahkan filter orderBy

Contoh AngularJS

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

Tampilan dengan Filter huruf besar

Untuk menampilkan huruf besar, tambahkan filter  huruf besar :

Contoh AngularJS

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

Tampilkan Indeks Tabel ($index)

Untuk menampilkan indeks tabel, tambahkan <td> dengan $index

Contoh AngularJS

<table>
  <tr ng-repeat="x in names">
    <td>{{ $index + 1 }}</td>
    <td>{{ x.Name }}</td>
    <td>{{ x.Country }}</td>
  </tr>
</table>

Menggunakan $genap dan $ganjil

Contoh AngularJS

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