AngularJS 表格
ng-repeat 指令可以完美的顯示表格。
在表格中顯示數據
使用 angular 顯示表格是非常簡單的:
AngularJS 實例
<div ng-app="" ng-controller="customersController">
<table>
<tr ng-repeat="x in names">
<td>{{ x.Name }}</td>
<td>{{ x.Country }}</td>
</tr>
</table>
</div>
<script>
function customersController($scope,$http) {
$http.get("/statics/demosource/Customers_JSON.php")
.success(function(response) {$scope.names = response;});
}
</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 過濾器:
AngularJS 實例
<table>
<tr ng-repeat="x in names | orderBy : 'Country'">
<td>{{ x.Name }}</td>
<td>{{ x.Country }}</td>
</tr>
</table>
Try it Yourself ?
使用 uppercase 濾器
如果字母要轉換為大寫,可以添加 uppercase 過濾器:
AngularJS 實例
<table>
<tr ng-repeat="x in names">
<td>{{ x.Name }}</td>
<td>{{ x.Country | uppercase}}</td>
</tr>
</table>
嘗試一下 ?