如何使用 Angular 的 UI Bootstrap 分页指令进行服务器端分页 [英] How can you do server side paging with Angular's UI Bootstrap pagination directive

查看:24
本文介绍了如何使用 Angular 的 UI Bootstrap 分页指令进行服务器端分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们想要使用 Angular 的 UI Bootstrap 分页指令进行服务器端分页.我们知道如何创建一个 RESTful 端点来从我们的服务器提供页面,但没有看到任何关于如何将该端点与 Angular 的 UI Bootstrap 分页指令挂钩的文档.

解决方案

请看下面的小demo

angular.module('app', ['ui.bootstrap']);angular.module('app').controller('PaginationDemoCtrl', function($scope, $http) {$scope.currentPage = 1;$scope.limit= 10;$scope.tracks = [];获取数据();函数 getData() {$http.get("https://api.spotify.com/v1/search?query=iron+&offset="+($scope.currentPage-1)*$scope.limit+"&limit=20&类型=艺术家").then(功能(响应){$scope.totalItems = response.data.artists.totalangular.copy(response.data.artists.items, $scope.tracks)});}//获取页面上的另一部分数据更改$scope.pageChanged = function() {获取数据();};});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.12.0.js"></script><link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="样式表"><body ng-app="app"><div ng-controller="PaginationDemoCtrl"><h4>示例服务器分页</h4><pagination total-items="totalItems" ng-model="currentPage" ng-change="pageChanged()" items-per-page="100"></pagination><ul><li ng-repeat="track in track" style="list-style:none"><img ng-src="{{track.images[2].url}}" alt="" width="160"/>{{track.name}}

Hi we are wanting to do server side paging with Angular's UI Bootstrap pagination directive. We know how to create a RESTful endpoint to serve up the pages from our servers but didn't see any documentations about how to hook that endpoint up to Angular's UI Bootstrap pagination directive.

解决方案

Please see small demo below

angular.module('app', ['ui.bootstrap']);
angular.module('app').controller('PaginationDemoCtrl', function($scope, $http) {

  $scope.currentPage = 1;
 $scope.limit= 10;


  $scope.tracks = [];
  getData();


  function getData() {
    $http.get("https://api.spotify.com/v1/search?query=iron+&offset="+($scope.currentPage-1)*$scope.limit+"&limit=20&type=artist")
      .then(function(response) {
        $scope.totalItems = response.data.artists.total
        angular.copy(response.data.artists.items, $scope.tracks)


      });
  }

//get another portions of data on page changed
  $scope.pageChanged = function() {
    getData();
  };


});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>


<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.12.0.js"></script>

<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">

<body ng-app="app">

  <div ng-controller="PaginationDemoCtrl">
    <h4>Sample Server Pagination</h4>

    <pagination total-items="totalItems" ng-model="currentPage" ng-change="pageChanged()" items-per-page="100"></pagination>
    <ul>
      <li ng-repeat="track in tracks" style="list-style:none">
<img ng-src="{{track.images[2].url}}" alt="" width="160"/>
{{track.name}}</li>
    </ul>

  </div>
</body>

这篇关于如何使用 Angular 的 UI Bootstrap 分页指令进行服务器端分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆