AngularJS 表格
ng-repeat 指令非常適合顯示錶格。
在表格中顯示資料
使用 Angular 顯示錶格非常簡單
AngularJS Example
<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>
自己動手試一試 »