Bảng AngularJS
- Trang trước Http AngularJS
- Trang tiếp theo Chọn AngularJS
Cú pháp ng-repeat rất phù hợp để hiển thị bảng.
Hiển thị dữ liệu trong bảng
Hiển thị bảng bằng AngularJS rất đơn giản:
Mô hình AngularJS
<div ng-app="myApp" ng-controller="customersCtrl"> <table> <tr ng-repeat="x in names"> <td>{{ x.Ten }}</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ị bằng CSS样式
Để làm cho nó trông đẹp hơn, bạn có thể thêm một số CSS vào trang:
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ị bằng bộ lọc orderBy
Để sắp xếp bảng, hãy thêm orderBy
Bộ lọc:
Mô hình AngularJS
<table> <tr ng-repeat="x in names | orderBy : 'QuocGia'"> <td>{{ x.Ten }}</td> <td>{{ x.Country }}</td> </tr> </table>
Hiển thị bằng bộ lọc uppercase
Để hiển thị bằng chữ in hoa, hãy thêm uppercase
Bộ lọc:
Mô hình AngularJS
<table> <tr ng-repeat="x in names"> <td>{{ x.Ten }}</td> <td>{{ x.QuocGia | uppercase }}</td> </tr> </table>
Hiển thị chỉ số bảng ($index)
Để hiển thị chỉ số bảng, hãy thêm $index
của <td>:
Mô hình AngularJS
<table> <tr ng-repeat="x in names"> <td>{{ $index + 1 }}</td> <td>{{ x.Ten }}</td> <td>{{ x.Country }}</td> </tr> </table>
Sử dụng $even và $odd
Mô hình 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>
- Trang trước Http AngularJS
- Trang tiếp theo Chọn AngularJS