AngularJS 表格

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

在表格中显示数据

使用 AngularJS 显示表格非常简单:

AngularJS 实例

<div ng-app="myApp" ng-controller="customersCtrl">

    <td>{{ x.Nama }}</td>
    
{{ x.Country }}
</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


  <tr ng-repeat="x in names | orderBy : 'Nasyon'">
    <td>{{ x.Nama }}</td>
    
{{ x.Country }}

亲自试一试

使用 uppercase 过滤器显示

要以大写形式显示,请添加 uppercase 过滤器:

AngularJS Example


    <td>{{ x.Nama }}</td>
    <td>{{ x.Nasyon | uppercase }}</td>
  

亲自试一试

显示表索引 ($index)

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

AngularJS Example


    <td>{{ $index + 1 }}</td>
    <td>{{ x.Nama }}</td>
    
{{ x.Country }}

亲自试一试

使用 $even 和 $odd

AngularJS Example

{{ x.Name }} {{ x.Name }} {{ x.Country }} {{ x.Country }}

亲自试一试