AngularJS 表格
- 上一頁 AngularJS Http
- 下一頁 AngularJS 選擇
ng-repeat 指令非常適合顯示表格。
在表格中顯示數據
使用 AngularJS 顯示表格非常簡單:
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>
使用 CSS 樣式顯示
為了讓它看起來更漂亮,可以向頁面添加一些 CSS:
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>
使用 orderBy 過濾器顯示
要對表進行排序,請添加 orderBy
過濾器:
AngularJS Example
<table> <tr ng-repeat="x in names | orderBy : 'Country'"> <td>{{ x.Name }}</td> <td>{{ x.Country }}</td> </tr> </table>
使用 uppercase 過濾器顯示
要以大寫形式顯示,請添加 uppercase
過濾器:
AngularJS Example
<table> <tr ng-repeat="x in names"> <td>{{ x.Name }}</td> <td>{{ x.Country | uppercase }}</td> </tr> </table>
顯示表索引 ($index)
要顯示表索引,請添加帶有 $index
的 <td>:
AngularJS Example
<table> <tr ng-repeat="x in names"> <td>{{ $index + 1 }}</td> <td>{{ x.Name }}</td> <td>{{ x.Country }}</td> </tr> </table>
使用 $even 和 $odd
AngularJS Example
<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>
- 上一頁 AngularJS Http
- 下一頁 AngularJS 選擇