AngularJS入门教程之表格实例详解


Posted in Javascript onJuly 27, 2016

AngularJS 表格

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

在表格中显示数据

使用 angular 显示表格是非常简单的:

AngularJS 实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>

<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("/try/angularjs/data/Customers_JSON.php")
 .success(function (response) {$scope.names = response.records;});
});
</script>

</body>
</html>

运行结果:

Alfreds Futterkiste Germany
Ana Trujillo Emparedados y helados Mexico
Antonio Moreno Taquería Mexico
Around the Horn UK
B's Beverages UK
Berglunds snabbköp Sweden
Blauer See Delikatessen Germany
Blondel père et fils France
Bólido Comidas preparadas Spain
Bon app' France
Bottom-Dollar Marketse Canada
Cactus Comidas para llevar Argentina
Centro comercial Moctezuma Mexico
Chop-suey Chinese Switzerland
Comércio Mineiro Brazil

 使用 CSS 样式

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

CSS 样式

</style>
</head>
<body>

<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("http://www.runoob.com/try/angularjs/data/Customers_JSON.php")
 .success(function (response) {$scope.names = response.records;});
});
</script>

</body>
</html>

 运行结果:

Alfreds Futterkiste Germany
Ana Trujillo Emparedados y helados Mexico
Antonio Moreno Taquería Mexico
Around the Horn UK
B's Beverages UK
Berglunds snabbköp Sweden
Blauer See Delikatessen Germany
Blondel père et fils France
Bólido Comidas preparadas Spain
Bon app' France
Bottom-Dollar Marketse Canada
Cactus Comidas para llevar Argentina
Centro comercial Moctezuma Mexico
Chop-suey Chinese Switzerland
Comércio Mineiro Brazil

 使用 orderBy 过滤器

排序显示,可以使用 orderBy 过滤器:

AngularJS 实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
<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>
</head>
<body>

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

<table>
 <tr ng-repeat="x in names | orderBy : 'Country'">
 <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("/try/angularjs/data/Customers_JSON.php")
 .success(function (response) {$scope.names = response.records;});
});
</script>

</body>
</html>

 运行效果:

 

Cactus Comidas para llevar Argentina
Comércio Mineiro Brazil
Bottom-Dollar Marketse Canada
Blondel père et fils France
Bon app' France
Alfreds Futterkiste Germany
Blauer See Delikatessen Germany
Ana Trujillo Emparedados y helados Mexico
Antonio Moreno Taquería Mexico
Centro comercial Moctezuma Mexico
Bólido Comidas preparadas Spain
Berglunds snabbköp Sweden
Chop-suey Chinese Switzerland
Around the Horn UK
B's Beverages UK

 使用 uppercase 过滤器

使用 uppercase 过滤器转换为大写:

AngularJS 实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
<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>
</head>
<body>

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

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

</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
 $http.get("/try/angularjs/data/Customers_JSON.php")
 .success(function (response) {$scope.names = response.records;});
});
</script>

</body>
</html>

运行效果:

Alfreds Futterkiste GERMANY
Ana Trujillo Emparedados y helados MEXICO
Antonio Moreno Taquería MEXICO
Around the Horn UK
B's Beverages UK
Berglunds snabbköp SWEDEN
Blauer See Delikatessen GERMANY
Blondel père et fils FRANCE
Bólido Comidas preparadas SPAIN
Bon app' FRANCE
Bottom-Dollar Marketse CANADA
Cactus Comidas para llevar ARGENTINA
Centro comercial Moctezuma MEXICO
Chop-suey Chinese SWITZERLAND
Comércio Mineiro BRAZIL

显示序号 ($index)

表格显示序号可以在 <td> 中添加 $index:

AngularJS 实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
<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>
</head>
<body>

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

<table>
 <tr ng-repeat="x in names">
 <td>{{ $index + 1 }}</td>
 <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("/try/angularjs/data/Customers_JSON.php")
 .success(function (response) {$scope.names = response.records;});
});
</script>

</body>
</html>

运行效果:

1 Alfreds Futterkiste Germany
2 Ana Trujillo Emparedados y helados Mexico
3 Antonio Moreno Taquería Mexico
4 Around the Horn UK
5 B's Beverages UK
6 Berglunds snabbköp Sweden
7 Blauer See Delikatessen Germany
8 Blondel père et fils France
9 Bólido Comidas preparadas Spain
10 Bon app' France
11 Bottom-Dollar Marketse Canada
12 Cactus Comidas para llevar Argentina
13 Centro comercial Moctezuma Mexico
14 Chop-suey Chinese Switzerland
15 Comércio Mineiro Brazil

使用 $even 和 $odd

AngularJS 实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
<style>
table, td {
 border: 1px solid grey;
 border-collapse: collapse;
 padding: 5px;
}
</style>
</head>
<body>

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

<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>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
 $http.get("/try/angularjs/data/Customers_JSON.php")
 .success(function (response) {$scope.names = response.records;});
});
</script>

</body>
</html>

运行效果:

Alfreds Futterkiste Germany
Ana Trujillo Emparedados y helados Mexico
Antonio Moreno Taquería Mexico
Around the Horn UK
B's Beverages UK
Berglunds snabbköp Sweden
Blauer See Delikatessen Germany
Blondel père et fils France
Bólido Comidas preparadas Spain
Bon app' France
Bottom-Dollar Marketse Canada
Cactus Comidas para llevar Argentina
Centro comercial Moctezuma Mexico
Chop-suey Chinese Switzerland
Comércio Mineiro Brazil

以上就是对AngularJS 表格资料的整理,后续继续补充,希望能帮助到有需要的同学。

Javascript 相关文章推荐
个人总结的一些关于String、Function、Array的属性和用法
Jan 10 Javascript
获取dom元素那些讨厌的位置封装代码
Jun 23 Javascript
深入理解JavaScript系列(8) S.O.L.I.D五大原则之里氏替换原则LSP
Jan 15 Javascript
JavaScript正则表达式之multiline属性的应用
Jun 16 Javascript
JavaScript数据结构与算法之集合(Set)
Jan 29 Javascript
Three.js学习之文字形状及自定义形状
Aug 01 Javascript
微信小程序 开发之滑块视图容器(swiper)详解及实例代码
Feb 22 Javascript
手机端转换rem适应
Apr 01 Javascript
详解滑动穿透(锁body)终极探索
Apr 16 Javascript
vue设置全局访问接口API地址操作
Aug 14 Javascript
ES11新增的这9个新特性,你都掌握了吗
Oct 15 Javascript
vue实现顶部菜单栏
Nov 08 Javascript
基于jQuery实现仿微博发布框字数提示
Jul 27 #Javascript
AngularJS入门教程之Select(选择框)详解
Jul 27 #Javascript
关于JS中的方法是否加括号的问题
Jul 27 #Javascript
AngularJS入门教程之 XMLHttpRequest实例讲解
Jul 27 #Javascript
AngularJS入门教程之服务(Service)
Jul 27 #Javascript
AngularJS 过滤器的简单实例
Jul 27 #Javascript
AngularJS延迟加载html template
Jul 27 #Javascript
You might like
PHP header()函数使用详细(301、404等错误设置)
2013/04/17 PHP
php获取YouTube视频信息的方法
2015/02/11 PHP
PHP 验证登陆类分享
2015/03/13 PHP
PHP 中 Orientation 属性判断上传图片是否需要旋转
2015/10/16 PHP
ZendFramework2连接数据库操作实例
2017/04/18 PHP
javascript 打印内容方法小结
2009/11/04 Javascript
js中更短的 Array 类型转换
2011/10/30 Javascript
jQuery.prototype.init选择器构造函数源码思路分析
2013/02/05 Javascript
jQuery+ajax实现鼠标单击修改内容的思路
2014/06/29 Javascript
js闭包的用途详解
2014/11/09 Javascript
Javascript的闭包详解
2014/12/26 Javascript
node.js读取文件到字符串的方法
2015/06/29 Javascript
Jquery中使用show()与hide()方法动画显示和隐藏图片
2015/10/08 Javascript
js实现常用排序算法
2016/08/09 Javascript
快速解决js中window.location.href不工作的问题
2016/11/02 Javascript
Vuex2.0+Vue2.0构建备忘录应用实践
2016/11/30 Javascript
JS动态添加的div点击跳转到另一页面实现代码
2017/09/30 Javascript
js精确的加减乘除实例
2017/11/14 Javascript
vue拖拽组件 vuedraggable API options实现盒子之间相互拖拽排序
2019/07/08 Javascript
百度小程序之间的页面通信过程详解
2019/07/18 Javascript
详解Vue.js和layui日期控件冲突问题解决办法
2019/07/25 Javascript
nodejs nedb 封装库与使用方法示例
2020/02/06 NodeJs
Angular 多模块项目构建过程
2020/02/13 Javascript
[03:10]2014DOTA2 TI马来劲旅Titan首战告捷目标只是8强
2014/07/10 DOTA
Python备份Mysql脚本
2008/08/11 Python
Python的ORM框架中SQLAlchemy库的查询操作的教程
2015/04/25 Python
Python使用自带的ConfigParser模块读写ini配置文件
2016/06/26 Python
python paramiko利用sftp上传目录到远程的实例
2019/01/03 Python
TensorFlow2.0矩阵与向量的加减乘实例
2020/02/07 Python
加拿大购物频道:The Shopping Channel
2016/07/21 全球购物
Carter’s官方旗舰店:美国受欢迎的婴童服装品牌
2018/01/21 全球购物
软件毕业生个人鉴定
2014/03/03 职场文书
开门红主持词
2014/04/02 职场文书
党的群众路线教育实践活动个人整改方案
2014/09/21 职场文书
党委干部批评与自我批评发言稿
2014/09/28 职场文书
病危通知单
2015/04/17 职场文书