AngularJS入门教程之表格实例详解
程序员文章站
2022-03-20 19:11:03
angularjs 表格
ng-repeat 指令可以完美的显示表格。
在表格中显示数据
使用 angular 显示表格是非常简单的:
angula...
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 表格资料的整理,后续继续补充,希望能帮助到有需要的同学。
下一篇: 找出正确手机号码