1. 程式人生 > >angular.js實現列表orderby排序

angular.js實現列表orderby排序

<html ng-app>
<head>
    <title>order by</title> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width,initial-scale=1" />
    <link href="../book/css/bootstrap.css" rel="stylesheet" type="text/css" /> 
    <script src="../file/angular-1.0.1.min.js" type="text/javascript"></script>
</head>  
<body>
    <div class="table-responsive" ng-controller="demoController"
>
       <table id="tb" class="table table-bordered table-hover">
        <thead>
          <tr>
            <th class="col-md-2">編號</th>
            <th class="col-md-4">國家</th>
            <th class="col-md-4">名稱</th> 
            <th class="col-md-3">年齡</th> 
          </tr>          
        </thead>
        <tbody id="tbody" ng-repeat="stu in data | orderBy : '-age' | limitTo: 10
"> 
           <tr class="{{ cls($index) }}">
             <td>{{ $index + 1 }}</td><td>{{ stu.country }}</td><td>{{ stu.name }}</td><td>{{ stu.age }}</td>
           </tr>  
        </tbody>
      </table>
     </div> 
     <script type="text/javascript">
         function demoController($scope,$window) {
             $scope.data = [
                { seq: 1, name: "魏國", country: "曹操",age : 45 },
                { seq: 2, name: "魏國", country: "張遼" ,age: 34},
                { seq: 3, name: "魏國", country: "司馬懿" ,age: 36 },
                { seq: 4, name: "魏國", country: "夏侯淳",age: 40 },
                { seq: 5, name: "蜀國", country: "劉備",age: 50 },
                { seq: 6, name: "蜀國", country: "關羽",age: 45 },
                { seq: 7, name: "蜀國", country: "張飛",age: 46 },
                { seq: 8, name: "蜀國", country: "趙雲",age: 35 },
                { seq: 9, name: "吳國", country: "孫權" ,age: 30 },
                { seq: 10, name: "吳國", country: "周瑜",age: 32 },
                { seq: 11, name: "吳國", country: "魯肅",age: 33 },
                { seq: 12, name: "吳國", country: "黃蓋",age: 55 }
             ];
                $scope.cls = function(i) { 
                    return (i + 1) % 4 == 1 ? "active" : "";
                }
         }
     </script>
</body>