简体   繁体   English

如何使用ngrepeat和bootstrap ui使用angularjs创建分页?

[英]How to create pagination with angularjs with ngrepeat and bootstrap ui?

What is the simpliest way to create pagination ? 创建分页的最简单方法是什么? I'd liket to use ng-repeat where i go through the items list and every item has id and name attributes. 我想在项目列表中使用ng-repeat,每个项目都有id和name属性。 The next code works for example 15 items and show 2 page but i'd like to show the first page the first 10 item and the other page shows the other items...and so on if i have a lot of items. 下一个代码可用于例如15个项目并显示2页,但是我想在第一页中显示前10个项目,而其他页面则显示其他项目...等等,如果我有很多项目。

My code what i tried: 我尝试的代码:

<table>
    <thead><th>id</th><th>name</th></thead>
    <tbody><tr ng-repeat="item in items">
        <td>{{item.id}}</td>
        <td>{{item.name}}</td>
    </tr></tbody>
</table>
<uib-pagination total-items="totalItems" ng-model="currentPage" max-size="maxSize" class="pagination-sm" boundary-links="true" force-ellipses="true"></uib-pagination>
<script>
$scope.items = [] //here i have for example 15 items..it comes through rest
totalItems = $scope.items.length; //15
$scope.currentPage=1;//1. page
$scope.maxSize=4; //maximum 4 page show
</script>

Could someone helps to me? 有人可以帮我吗? Thanks a lot! 非常感谢!

You can take a look at the fiddler here . 你可以在这里看看提琴手。 Basically I have created a custom filter which will take the current page number and page size as input and then slice out the items that needs to be shown in the UI. 基本上,我创建了一个自定义过滤器,该过滤器将当前页码和页面大小作为输入,然后切出需要在UI中显示的项目。

Filter 过滤

myApp.filter('paginate', function() {
  return function(items, page, pageSize) {
    if (items === undefined || items === null)
      return [];
    if (pageSize === undefined || pageSize === null || pageSize < 0 || pageSize >= items.length)
      return items;
    var filtered = [];
    page = page - 1;
    console.log(page * pageSize + " " + parseInt((page * pageSize) + pageSize));
    filtered = items.slice(page * pageSize, parseInt((page * pageSize) + pageSize));
    return filtered;
  }
});

HTML HTML

<div ng-controller="MyCtrl">
  <div ng-repeat="item in items | paginate: currentPage : pageSize">
    {{item}}
  </div>
  <input type="number" ng-model="currentPage" />
</div>

使用ui-bootstrap的分页指令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angularjs UI在ngRepeat中崩溃 - Angularjs UI collapse in ngRepeat AngularJS-ui-bootstrap分页错误 - AngularJS - ui-bootstrap pagination error 如何使用Angular.js和UI Bootstrap创建分页 - How Create Pagination with Angular.js and UI Bootstrap 如果日期小于当前日期,则禁用ui bootstrap datepicker-AngularJS ngRepeat - Disable ui bootstrap datepicker if the date is less than the current date - Angularjs ngRepeat 如何为ui-bootstrap datepicker创建angularJs包装器指令? - How to create an angularJs wrapper directive for a ui-bootstrap datepicker? 如何对使用UI Bootstrap Pagination分页的AngularJS中的所有记录进行排序和筛选 - How to sort and filter all records in AngularJS that are paginated using UI Bootstrap Pagination 如何使用angular-ui bootstrap在angularjs中实现服务器端分页。 - How to implement server side pagination in angularjs using angular-ui bootstrap.? AngularJS-ui-bootstrap分页。 在单页上隐藏按钮 - AngularJS - ui-bootstrap pagination. Hide buttons on single page AngularJS-ui-bootstrap分页“显示更多结果”不起作用 - AngularJS - ui-bootstrap pagination “show more results” not working 如何在Angular UI Bootstrap Pagination中生成链接 - How to generate links in Angular UI Bootstrap Pagination
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM