AngularJS: Table trong AngularJS
Chỉ thị ng-repeat là cách tốt nhất để hiển thị bảng dữ liệu trong AngularJS.
Hiển thị dữ liệu trong bảng
Việc hiển thị bảng với AngularJS rất đơn giản.
Ví dụ:
<!DOCTYPE html> <html> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script> <body> <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("service-http-customers.php") .then(function (response) {$scope.names = response.data.records;}); }); </script> </body> </html>
Hiển thị với style CSS
Để kết quả hiển thị được đẹp thì ta đưa vô trang một số thuộc tính CSS. Ví dụ:
<!DOCTYPE html> <html> <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> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script> <body> <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("service-http-customers.php") .then(function (response) {$scope.names = response.data.records;}); }); </script> </body> </html>
Display with orderBy Filter
Để sắp xếp bảng ta thêm filter orderBy. Ví dụ:
<!DOCTYPE html> <html> <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> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script> <body> <div ng-app="myApp" ng-controller="customersCtrl"> <table> <tr ng-repeat="x in names | orderBy : 'Country'"> <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("service-http-customers.php") .then(function (response) {$scope.names = response.data.records;}); }); </script> </body> </html>
Hiển thị với filter uppercase
Để hiển thị chữ in hoa ta sử dụng filter uppercase. Ví dụ:
<!DOCTYPE html> <html> <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> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script> <body> <div ng-app="myApp" ng-controller="customersCtrl"> <table> <tr ng-repeat="x in names"> <td>{{ x.Name }}</td> <td>{{ x.Country | uppercase }}</td> </tr> </table> </div> <script> var app = angular.module('myApp', []); app.controller('customersCtrl', function($scope, $http) { $http.get("service-http-customers.php") .then(function (response) {$scope.names = response.data.records;}); }); </script> </body> </html>
Hiển thị chỉ mục bảng ($index)
Để hiển thị chỉ mục của bảng ta thêm một cột <td> chứa $index. Ví dụ:
<!DOCTYPE html> <html> <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> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script> <body> <div ng-app="myApp" ng-controller="customersCtrl"> <table> <tr ng-repeat="x in names"> <td>{{ $index + 1 }}</td> <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("service-http-customers.php") .then(function (response) {$scope.names = response.data.records;}); }); </script> </body> </html>
Sử dụng $even và $odd
Ví dụ:
<!DOCTYPE html> <html> <style> table, td { border: 1px solid grey; border-collapse: collapse; padding: 5px; } </style> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script> <body> <div ng-app="myApp" ng-controller="customersCtrl"> <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> </div> <script> var app = angular.module('myApp', []); app.controller('customersCtrl', function($scope, $http) { $http.get("service-http-customers.php") .then(function (response) {$scope.names = response.data.records;}); }); </script> </body> </html