如何使用 UI Bootstrap 分页 table,使用 ng-repeat 生成

How to use UI Bootstrap Pagination for a table, generated using ng-repeat

我正在尝试使用 uib-pagination 在我的 Angular 应用程序中保持分页。我无法找到执行此操作的正确方法。

HTML

<table id="mytable" class="table table-striped">
  <thead>
    <tr class="table-head">
      <th>Name</th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="person in aCandidates">
      <th>
        <div>{{person}}</div>
      </th>
    </tr>
  </tbody>
</table>

控制器

$scope.totalItems = $scope.aCandidates.length;
$scope.currentPage = 1;
$scope.itemsPerPage = 10;

我可以看到分页栏,但没有执行任何操作,即使在将总项目数设为 10 后,整个 table 数据也会呈现。

uib-pagination 的工作原理以及数据(在本例中 aCandidates)如何链接到分页。

你缺少的部分是你必须有一个函数来从你的整个数组中获取当前页面的数据。我添加了一个名为 setPagingData() 的函数来处理这个问题。

你可以在分叉的 plunker

中看到这个

var app = angular.module("plunker", ["ui.bootstrap"]);

app.controller("MainCtrl", function($scope) {
  var allCandidates =
      ["name1", "name2", "name3", "name4", "name5",
       "name6", "name7", "name8", "name9", "name10",
       "name11", "name12", "name13", "name14", "name15",
       "name16", "name17", "name18", "name19", "name20"
      ];

  $scope.totalItems = allCandidates.length;
  $scope.currentPage = 1;
  $scope.itemsPerPage = 5;

  $scope.$watch("currentPage", function() {
    setPagingData($scope.currentPage);
  });

  function setPagingData(page) {
    var pagedData = allCandidates.slice(
      (page - 1) * $scope.itemsPerPage,
      page * $scope.itemsPerPage
    );
    $scope.aCandidates = pagedData;
  }
});
<link data-require="bootstrap-css@*" data-semver="4.0.0-alpha.2" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" />
<script>document.write('<base href="' + document.location + '" />');</script>
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.9/angular.js" data-semver="1.4.9"></script>
<script data-require="ui-bootstrap@*" data-semver="1.3.2" src="https://cdn.rawgit.com/angular-ui/bootstrap/gh-pages/ui-bootstrap-tpls-1.3.2.js"></script>

<div ng-app="plunker">
  <div ng-controller="MainCtrl">
    <table id="mytable" class="table table-striped">
      <thead>
        <tr class="table-head">
          <th>Name</th>
        </tr>
      </thead>
      <tbody>
        <tr ng-repeat="person in aCandidates">
          <th>
            <div>{{person}}</div>
          </th>
        </tr>
      </tbody>
    </table>
    <uib-pagination total-items="totalItems" ng-model="currentPage" items-per-page="itemsPerPage"></uib-pagination>
  </div>
</div>

对于较新的版本,更改为以下代码:

 <ul uib-pagination total-items="totalItems" ng-model="currentPage" items-per-page="itemsPerPage"></ul>

不再支持 <uib-pagination> 标签。它必须作为属性给出。

如果您正在使用 Angular-2,您可以尝试 ngx-bootstrap。它有 bootstrap 个组件,可以与 Angular-2

集成

安装 ngx-bootstrap 后,只需尝试以下操作

您的 html 模板 | pagination.html

<div class="row">
  <div class="col-xs-12 col-12">
    <pagination [totalItems]="totalItems" [(ngModel)]="currentPage" [maxSize]="maxSize" class="pagination-sm"
                [boundaryLinks]="true"></pagination>
  </div>
 </div>

你的组件 | 分页-component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'demo-pagination',
  templateUrl: './pagination.html'
})
export class DemoPagination {
  maxSize: number = 5;
  totalItems: number = 175;
  currentPage: number = 1;
  numPages: number = 0;

  pageChanged(event: any): void {
    console.log('Page changed to: ' + event.page);
    console.log('Number items per page: ' + event.itemsPerPage);
  }
}

这将呈现为