AngularJS 表格

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

在表格中显示数据

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

Customers_JSON.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"
        }
    ]
}
<!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"> //ng-repeat写在tr中
                    <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("Customers_JSON.json")
                    .success(function(response) {
                        $scope.names = response.records;
                    });
            });
        </script>
    </body>
</html>

使用 CSS 样式

<style>
table, td  {
  border: 1px solid grey;
  border-collapse: collapse;
  padding: 5px;
}
</style>

使用 orderBy 过滤器

 <table>
  <tr ng-repeat="x in names | orderBy : 'Country'"> //orderBy是写在ng-repeat指令中的,By大写
    <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:#f1f1f1 "> //ng-if 指令不同于 ng-hide, ng-hide 隐藏元素,而 ng-if 是从 DOM 中移除元素。
                        {{$index+1}}</td>
                    <td ng-if="$even ">
                        {{$index+1}}</td>
                    <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>

 

posted @ 2016-07-12 18:29  Chris_在IT道路上前行  阅读(133)  评论(0编辑  收藏  举报