AngularJS 表格


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


在表格中显示数据

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

  1. <div ng-app="myApp" ng-controller="customersCtrl">
  2. <table>
  3. <tr ng-repeat="x in names">
  4. <td>{{ x.Name}}</td>
  5. <td>{{ x.Country}}</td>
  6. </tr>
  7. </table>
  8. </div>
  9. <script>
  10. var app = angular.module('myApp',[]);
  11. app.controller('customersCtrl',function($scope, $http){
  12. $http.get("test.php")
  13. .success(function(response){$scope.names = response.records;});
  14. });
  15. </script>
 
moyu:
 

使用 CSS 样式

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

  1. <style>
  2. table, th , td {
  3. border:1px solid grey;
  4. border-collapse: collapse;
  5. padding:5px;
  6. }
  7. table tr:nth-child(odd){
  8. background-color:#f1f1f1;
  9. }
  10. table tr:nth-child(even){
  11. background-color:#ffffff;
  12. }
  13. </style>
结果:

使用 orderBy 过滤器

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

  1. <table>
  2. <tr ng-repeat="x in names | orderBy : 'Country'">
  3. <td>{{ x.Name}}</td>
  4. <td>{{ x.Country}}</td>
  5. </tr>
  6. </table>
结果:

使用 uppercase 过滤器

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

  1. <table>
  2. <tr ng-repeat="x in names">
  3. <td>{{ x.Name}}</td>
  4. <td>{{ x.Country| uppercase }}</td>
  5. </tr>
  6. </table>
结果:

显示序号 ($index)

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

  1. <table>
  2. <tr ng-repeat="x in names">
  3. <td>{{ $index +1}}</td>
  4. <td>{{ x.Name}}</td>
  5. <td>{{ x.Country}}</td>
  6. </tr>
  7. </table>
魔芋结果:

使用 $even 和 $odd

  1. <table>
  2. <tr ng-repeat="x in names">
  3. <td ng-if="$odd" style="background-color:#f1f1f1">{{ x.Name}}</td>
  4. <td ng-if="$even">{{ x.Name}}</td>
  5. <td ng-if="$odd" style="background-color:#f1f1f1">{{ x.Country}}</td>
  6. <td ng-if="$even">{{ x.Country}}</td>
  7. </tr>
  8. </table>
结果:

最新文章

  1. 基于select的python聊天室程序
  2. mysql的多表查询
  3. javascript优化--04高质量编码
  4. 一个对称加密、解密的方法C#工具类
  5. 解决ajax请求cors跨域问题
  6. 10105 - Polynomial Coefficients
  7. spark2.0系列《一》—— RDD VS. DataFrame VS. DataSet
  8. 深入理解Java NIO
  9. 2018面向对象程序设计(java)课程学习进度条
  10. c#多线程与委托(转)
  11. 在linux系统中安装VSCode(Visual Studio Code)和图标的创建方式
  12. Method for balancing binary search trees
  13. 手脱PE Pack v1.0
  14. java检验银行卡号
  15. python 面试题(2)
  16. SSH整合需要的jar包
  17. DRF视图集的使用
  18. jquery 插件:chosen
  19. 机房重构——泛型和“DataTable”
  20. python基础——2(基本数据类型及运算符)

热门文章

  1. 全分布式的Hadoop虚拟机安装
  2. bzoj 1603: [Usaco2008 Oct]打谷机【瞎搞】
  3. (快排)51NOD 1018 排序
  4. 编写第一Spring程序
  5. .net中RSA加密解密
  6. Storm概念学习系列之storm的优化
  7. 网站开发综合技术 三 JavaScript的DOM操作
  8. jquery实现点击进入新的页面。(jquery实现超链接)
  9. redis学习-字典
  10. FCC 基础JavaScript 练习2