AngularJS 表格

AngularJS ng-repeat 指令可以完美的显示表格

数据准备

假设我们有一个存储了客户信息的 JSON 文件 customer.json 内容如下

{
"records":[
{"Name":"Alfreds Futterkiste","City":"Berlin","Country":"Germany"},
{"Name":"Ana Trujillo Emparedados y helados","City":"México D.F.","Country":"Mexico"},
{"Name":"Antonio Moreno Taquería","City":"México D.F.","Country":"Mexico"},
{"Name":"Around the Horn","City":"London","Country":"UK"},
{"Name":"B's Beverages","City":"London","Country":"UK"},
{"Name":"Berglunds snabbköp","City":"Luleå","Country":"Sweden"},
{"Name":"Blauer See Delikatessen","City":"Mannheim","Country":"Germany"},
{"Name":"Blondel père et fils","City":"Strasbourg","Country":"France"},
{"Name":"Bólido Comidas preparadas","City":"Madrid","Country":"Spain"},
{"Name":"Bon app'","City":"Marseille","Country":"France"},
{"Name":"Bottom-Dollar Marketse","City":"Tsawassen","Country":"Canada"},
{"Name":"Cactus Comidas para llevar","City":"Buenos Aires","Country":"Argentina"},
{"Name":"Centro comercial Moctezuma","City":"México D.F.","Country":"Mexico"},
{"Name":"Chop-suey Chinese","City":"Bern","Country":"Switzerland"},
{"Name":"Comércio Mineiro","City":"São Paulo","Country":"Brazil"}
]
}

在表格中显示数据

下面是对 ng-repeat 的简单使用

<!DOCTYPE html>
<meta charset="utf-8">
<link href="/static/next/css/tryit.css?v=2017082407" rel="stylesheet"/>
<script src="https://cdn.staticfile.org/angular.js/1.6.3/angular.min.js"></script>
<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>
<footer>简单教程,简单编程<br/>Copyright © 简单教程 www.twle.cn</footer>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $http.get("/static/tiy/html/angularjs/customer.json")
    .then(function (result) {
        $scope.names = result.data.records;
    });
});
</script>

运行范例 »

废弃声明 (v1.5)

v1.5 中 $httpsuccesserror 方法已废弃,请使用 then 方法替代

如果你使用的是 v1.5 以下版本,可以使用以下代码

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $http.get("/static/tiy/html/angularjs/customer.json")
    .success(function (response) {$scope.names = response.records;});
});
</script>

添加 CSS 样式

为了让页面更加美观,我们可以在页面中使用 CSS

<style>
table, th , td {
  border: 1px solid #ddd;  
  border-collapse: collapse;
  padding: 5px;
}

table tr:nth-child(odd)
{  
  background-color: #eee;
}

table tr:nth-child(even)
{
  background-color: #ffffff;
}
</style>

运行范例 »

使用 orderBy 过滤器排序

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

运行范例 »

使用 uppercase 过滤器将国家转换为大写

<table>
    <tr ng-repeat="x in names">
        <td>{{ x.Name }}</td>
        <td>{{ x.Country | uppercase }}</td>  
    </tr>
</table>

运行范例 »

显示序号 ( $index )

<table>
    <tr ng-repeat="x in names">    
    <td>{{ $index + 1 }}</td>
    <td>{{ x.Name }}</td>
    <td>{{ x.Country }}</td>  
</tr>
</table>

运行范例 »

使用 $even 和 $odd 奇偶行

<table>
    <tr ng-repeat="x in names">
        <td ng-if="$odd" style="background-color:#eee">{{ x.Name }}</td>
        <td ng-if="$even">{{ x.Name }}</td>
        <td ng-if="$odd" style="background-color:#eee">{{ x.Country }}</td>
        <td ng-if="$even">{{ x.Country }}</td>
    </tr>
</table>

运行范例 »

学习 AngularJS

关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

  简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2022 简单教程 twle.cn All Rights Reserved.