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 相关文章推荐
js函数使用技巧之 setTimeout(function(){},0)
Feb 09 Javascript
基于jquery的复制网页内容到WORD的实现代码
Feb 16 Javascript
jquery ready(fn)事件使用介绍
Aug 21 Javascript
JS控件ASP.NET的treeview控件全选或者取消(示例代码)
Dec 16 Javascript
JavaScript通过正则表达式实现表单验证电话号码
Mar 07 Javascript
谈谈对offsetleft兼容性的理解
Nov 11 Javascript
js Canvas实现的日历时钟案例分享
Dec 25 Javascript
H5图片压缩与上传实例
Apr 21 Javascript
浅谈Angular路由守卫
Aug 26 Javascript
js实现rem自动匹配计算font-size的示例
Nov 18 Javascript
Vue波纹按钮组件制作
Apr 30 Javascript
JS运算符优先级与表达式示例详解
Sep 04 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过程中的一些注意点的总结
2013/10/25 PHP
PHP提示Warning:phpinfo() has been disabled函数禁用的解决方法
2014/12/17 PHP
jQuery+PHP实现的掷色子抽奖游戏实例
2015/01/04 PHP
php实现mysql数据库分表分段备份
2015/06/18 PHP
编写PHP脚本使WordPress的主题支持Widget侧边栏
2015/12/14 PHP
PHP 扩展Memcached命令用法实例总结
2020/06/04 PHP
ext form 表单提交数据的方法小结
2008/08/08 Javascript
IE浏览器打印的页眉页脚设置解决方法
2009/12/08 Javascript
一个简单的js鼠标划过切换效果
2010/06/30 Javascript
JS+CSS制作DIV层可(最小化/拖拽/排序)功能实现代码
2013/02/25 Javascript
ff下JQuery无法监听input的keyup事件的解决方法
2013/12/12 Javascript
node.js中的fs.renameSync方法使用说明
2014/12/16 Javascript
深入理解JavaScript系列(43):设计模式之状态模式详解
2015/03/04 Javascript
动态加载jQuery的方法
2015/06/16 Javascript
es6新特性之 class 基本用法解析
2018/05/05 Javascript
Node.js的进程管理的深入理解
2019/01/09 Javascript
原生js实现each方法实例代码详解
2019/05/27 Javascript
jquery绑定事件 bind和on的用法与区别分析
2020/05/22 jQuery
[03:43]2014DOTA2西雅图国际邀请赛 newbee战队巡礼
2014/07/07 DOTA
python2.7删除文件夹和删除文件代码实例
2013/12/18 Python
使用BeautifulSoup爬虫程序获取百度搜索结果的标题和url示例
2014/01/19 Python
python实现简单http服务器功能
2018/09/17 Python
PyCharm在新窗口打开项目的方法
2019/01/17 Python
Python-while 计算100以内奇数和的方法
2019/06/11 Python
python自带tkinter库实现棋盘覆盖图形界面
2019/07/17 Python
python获取指定日期范围内的每一天,每个月,每季度的方法
2019/08/08 Python
Python3.6+selenium2.53.6自动化测试_读取excel文件的方法
2019/09/06 Python
python构建指数平滑预测模型示例
2019/11/21 Python
pytorch实现从本地加载 .pth 格式模型
2020/02/14 Python
python 如何在测试中使用 Mock
2021/03/01 Python
C# Debug和Testing相关面试题
2015/10/25 面试题
行政复议答复书
2015/07/01 职场文书
2016年助残日旅游活动总结
2016/04/01 职场文书
励志正能量20句:送给所有为梦想拼搏的人
2019/11/11 职场文书
面试提问mysql一张表到底能存多少数据
2022/03/13 MySQL
Redis官方可视化工具RedisInsight安装使用教程
2022/04/19 Redis