分页AngularJS数据表

时间:2017-11-27 15:11:25

标签: javascript angularjs pagination

我在用户表中收集了一些JSON(虚拟)数据。

var root = 'https://jsonplaceholder.typicode.com';

// Create an Angular module named "usersApp"
var app = angular.module("usersApp", []);

// Create controller for the "usersApp" module
app.controller("usersCtrl", ["$scope", "$http", function($scope, $http) {
  var url = root + "/users"
  $http.get(url)
    .then(function(data) {
      $scope.users = data.data;
    });
}]);
.search-box {
  margin: 5px;
}

.panel-heading {
  font-weight: bold;
}

.table-container .panel-body {
  padding: 0;
}

.table-container table tr th {
  font-weight: bold;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<div class="container" data-ng-app="usersApp">
  <div class="panel panel-default table-container">
    <div class="panel-heading">Users</div>
    <div class="panel-body" data-ng-controller="usersCtrl">
      <div class="row">
        <div class="col-sm-12">
          <div class="form-group search-box">
            <input type="text" class="form-control" id="search" placeholder="Search User" data-ng-model="search">
          </div>
        </div>
        <div class="col-sm-12">
          <table class="table table-striped" id="dataTable">
            <thead>
              <tr>
                <th>Full name</th>
                <th>Email</th>
                <th>City</th>
                <th>Street</th>
                <th>Suite</th>                 
              </tr>
            </thead>
            <tbody>
              <tr data-ng-repeat="user in users|filter:search">
                <td>{{user.name}}</td>
                <td><a href="mailto:{{user.email}}">{{user.email}}</a></td>
                <td>{{user.address.city}}</td>
                <td>{{user.address.street}}</td>
                <td>{{user.address.suite}}</td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>

该表只有10行,但在实际应用中,需要管理更多数据,因此需要对其进行分页。上面的每页可以有3行(第4行除外,它应该有一行)。

问题(S):

  1. AngularJS是否有内置的简单方法来对这样的表进行分页?
  2. 如果没有,最好的选择是什么?

1 个答案:

答案 0 :(得分:0)

您可以使用 ngInfiniteScroll 进行分页。 https://sroze.github.io/ngInfiniteScroll/