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

可紊 提交于 2020-01-19 12:56:05

问题


I am trying to keep pagination in my Angular application using uib-pagination. I am unable to get proper way to do this.

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>

Controller

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

I am able to see the pagination bar but no action is performed with that and the entire table data is rendered even after giving total-items as 10.

How exactly uib-pagination works and how is the data (in this case aCandidates) is linked to pagination.


回答1:


The part you are missing is that you have to have a function to get the current page's data from your entire array. I've added a function called setPagingData() to handle this.

You can see this in the forked 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>



回答2:


For newer versions, change to the following code:

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

The <uib-pagination> tag is no longer supported. It has to be given as an attribute.




回答3:


If you are using Angular-2 you can try ngx-bootstrap. It has number of bootstrap components that can be integrated with Angular-2

After installing ngx-bootstrap, just try the following

Your html template | 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>

Your component | pagination-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);
  }
}

This would render like



来源:https://stackoverflow.com/questions/37702042/how-to-use-ui-bootstrap-pagination-for-a-table-generated-using-ng-repeat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!