AngularJS tut 12: Bảng ( tables)
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ị table index ($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 $even và $odd
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>