AngularJS 表格

ng-repeat 指令非常适合显示表格。


用表格展示数据

AngularJS 的表格显示非常简单:

AngularJS 实例
  1. <!DOCTYPE html>
  2. <html>
  3. <script src="https://cdn.bootcdn.net/ajax/libs/angular.js/1.8.2/angular.min.js"></script>
  4. <body>
  5. <div ng-app="myApp" ng-controller="customersCtrl">
  6. <table>
  7. <tr ng-repeat="x in names">
  8. <td>{{ x.Name }}</td>
  9. <td>{{ x.Country }}</td>
  10. </tr>
  11. </table>
  12. </div>
  13. <script>
  14. var app = angular.module('myApp', []);
  15. app.controller('customersCtrl', function($scope, $http) {
  16. $http.get("/znadmin/md/2849/customers.json")
  17. .then(function (response) {$scope.names = response.data.records;});
  18. });
  19. </script>
  20. </body>
  21. </html>

用 CSS 样式展示

为了让页面更美观,可以在页面中添加一些 CSS:

CSS 样式
  1. <!DOCTYPE html>
  2. <html>
  3. <style>
  4. table, th , td {
  5. border: 1px solid grey;
  6. border-collapse: collapse;
  7. padding: 5px;
  8. }
  9. table tr:nth-child(odd) {
  10. background-color: #f1f1f1;
  11. }
  12. table tr:nth-child(even) {
  13. background-color: #ffffff;
  14. }
  15. </style>
  16. <script src="https://cdn.bootcdn.net/ajax/libs/angular.js/1.8.2/angular.min.js"></script>
  17. <body>
  18. <div ng-app="myApp" ng-controller="customersCtrl">
  19. <table>
  20. <tr ng-repeat="x in names">
  21. <td>{{ x.Name }}</td>
  22. <td>{{ x.Country }}</td>
  23. </tr>
  24. </table>
  25. </div>
  26. <script>
  27. var app = angular.module('myApp', []);
  28. app.controller('customersCtrl', function($scope, $http) {
  29. $http.get("/znadmin/md/2849/customers.json")
  30. .then(function (response) {$scope.names = response.data.records;});
  31. });
  32. </script>
  33. </body>
  34. </html>

使用 orderBy 过滤器显示

要对表进行排序,请添加 orderBy 过滤器:

AngularJS 实例
  1. <!DOCTYPE html>
  2. <html>
  3. <style>
  4. table, th , td {
  5. border: 1px solid grey;
  6. border-collapse: collapse;
  7. padding: 5px;
  8. }
  9. table tr:nth-child(odd) {
  10. background-color: #f1f1f1;
  11. }
  12. table tr:nth-child(even) {
  13. background-color: #ffffff;
  14. }
  15. </style>
  16. <script src="https://cdn.bootcdn.net/ajax/libs/angular.js/1.8.2/angular.min.js"></script>
  17. <body>
  18. <div ng-app="myApp" ng-controller="customersCtrl">
  19. <table>
  20. <tr ng-repeat="x in names | orderBy : 'Country'">
  21. <td>{{ x.Name }}</td>
  22. <td>{{ x.Country }}</td>
  23. </tr>
  24. </table>
  25. </div>
  26. <script>
  27. var app = angular.module('myApp', []);
  28. app.controller('customersCtrl', function($scope, $http) {
  29. $http.get("/znadmin/md/2849/customers.json")
  30. .then(function (response) {$scope.names = response.data.records;});
  31. });
  32. </script>
  33. </body>
  34. </html>

用 uppercase 过滤器显示

要小写展示, 添加一个 uppercase 过滤器:

AngularJS 实例
  1. <!DOCTYPE html>
  2. <html>
  3. <style>
  4. table, th , td {
  5. border: 1px solid grey;
  6. border-collapse: collapse;
  7. padding: 5px;
  8. }
  9. table tr:nth-child(odd) {
  10. background-color: #f1f1f1;
  11. }
  12. table tr:nth-child(even) {
  13. background-color: #ffffff;
  14. }
  15. </style>
  16. <script src="https://cdn.bootcdn.net/ajax/libs/angular.js/1.8.2/angular.min.js"></script>
  17. <body>
  18. <div ng-app="myApp" ng-controller="customersCtrl">
  19. <table>
  20. <tr ng-repeat="x in names">
  21. <td>{{ x.Name }}</td>
  22. <td>{{ x.Country | uppercase }}</td>
  23. </tr>
  24. </table>
  25. </div>
  26. <script>
  27. var app = angular.module('myApp', []);
  28. app.controller('customersCtrl', function($scope, $http) {
  29. $http.get("/znadmin/md/2849/customers.json")
  30. .then(function (response) {$scope.names = response.data.records;});
  31. });
  32. </script>
  33. </body>
  34. </html>

显示表格索引 ($index)

要显示表格索引,请添加一个带有 $index 的 <td> :

AngularJS 实例
  1. <!DOCTYPE html>
  2. <html>
  3. <style>
  4. table, th , td {
  5. border: 1px solid grey;
  6. border-collapse: collapse;
  7. padding: 5px;
  8. }
  9. table tr:nth-child(odd) {
  10. background-color: #f1f1f1;
  11. }
  12. table tr:nth-child(even) {
  13. background-color: #ffffff;
  14. }
  15. </style>
  16. <script src="https://cdn.bootcdn.net/ajax/libs/angular.js/1.8.2/angular.min.js"></script>
  17. <body>
  18. <div ng-app="myApp" ng-controller="customersCtrl">
  19. <table>
  20. <tr ng-repeat="x in names">
  21. <td>{{ $index + 1 }}</td>
  22. <td>{{ x.Name }}</td>
  23. <td>{{ x.Country }}</td>
  24. </tr>
  25. </table>
  26. </div>
  27. <script>
  28. var app = angular.module('myApp', []);
  29. app.controller('customersCtrl', function($scope, $http) {
  30. $http.get("/znadmin/md/2849/customers.json")
  31. .then(function (response) {$scope.names = response.data.records;});
  32. });
  33. </script>
  34. </body>
  35. </html>

使用 $even 与 $odd

AngularJS 实例
  1. <!DOCTYPE html>
  2. <html>
  3. <style>
  4. table, td {
  5. border: 1px solid grey;
  6. border-collapse: collapse;
  7. padding: 5px;
  8. }
  9. </style>
  10. <script src="https://cdn.bootcdn.net/ajax/libs/angular.js/1.8.2/angular.min.js"></script>
  11. <body>
  12. <div ng-app="myApp" ng-controller="customersCtrl">
  13. <table>
  14. <tr ng-repeat="x in names">
  15. <td ng-if="$odd" style="background-color:#f1f1f1">
  16. {{ x.Name }}</td>
  17. <td ng-if="$even">
  18. {{ x.Name }}</td>
  19. <td ng-if="$odd" style="background-color:#f1f1f1">
  20. {{ x.Country }}</td>
  21. <td ng-if="$even">
  22. {{ x.Country }}</td>
  23. </tr>
  24. </table>
  25. </div>
  26. <script>
  27. var app = angular.module('myApp', []);
  28. app.controller('customersCtrl', function($scope, $http) {
  29. $http.get("/znadmin/md/2849/customers.json")
  30. .then(function (response) {$scope.names = response.data.records;});
  31. });
  32. </script>
  33. </body>
  34. </html>

分类导航