Bảng AngularJS


Lệnh ng-repeat hoàn hảo để hiển thị các bảng.


Hiển thị dữ liệu trong bảng

Hiển thị bảng với góc rất đơn giản:

Ví dụ về 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>

Hiển thị với Kiểu CSS

Để làm cho nó đẹp, hãy thêm một số CSS vào trang:

Kiểu 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>


Hiển thị với thứ tự

Để sắp xếp bảng, hãy thêm một trật tự bằng bộ lọc: 

Ví dụ về AngularJS

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

Hiển thị với Bộ lọc chữ hoa

Để hiển thị chữ hoa, hãy thêm bộ lọc  chữ hoa :

Ví dụ về AngularJS

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

Hiển thị Chỉ mục Bảng ($ index)

Để hiển thị chỉ mục bảng, hãy thêm một <td> với $ index

Ví dụ về AngularJS

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

Sử dụng $ chẵn và $ lẻ

Ví dụ về 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>