Displaying a list of items is a common task for most web applications. Usually, our data will be relatively large and cannot be displayed well in a single page. In this case, we need to display the data in a page form, with the function of going to the previous page and the next page. Now I am learning angular, using angularjs pagination, based on directive implementation, and using bootstrap in style, and directly add tags to the html code to call it.
Let's take a look at the renderings first
Example code
app.directive('pagePagination', function(){ return { restrict : 'E', template : '<div><ul><li ng-class="page.style" ng-repeat="page in pageList"><a href="{{ page.link }}">{{ page.name }}</a></li></ul><ul ng-if="pageList[0]"><li><span>Total<b>{{ pageRecord }}</b> records/Total<b>{{ pageCount }}</b> Pages</span></li></ul></div>', replace : true, scope : { "pageId" : "=", "pageRecord" : "=", "pageSize" : "=", "pageUrlTemplate" : "=" }, controller : ['$scope', function($scope){ $scope.getLink = function(pageId){ return $scope.pageUrlTemplate.replace("{ PAGE}", pageId); }; $scope.getPageList = function(){ var page = []; var firstPage = parseInt(( $scope.pageId - 1 ) / $scope._pageSize ) * $scope._pageSize + 1; page.push({ name : 'Home', style : $scope.pageId == 1 ? "disabled" : "", link : $scope.getLink(1) }); page.push({ name : 'Previous page', style : $scope.pageId == 1 ? "disabled" : "", link : $scope.getLink(1) }); for( var pageId = firstPage; pageId < firstPage + 10; pageId ++){ if( pageId >= 1 && pageId <= $scope.pageCount ){ page.push({ name : pageId, link : $scope.getLink(pageId), style : pageId == $scope.pageId ? "active" : "" }); } } page.push({ name : 'Next page', style : $scope.pageId == $scope.pageCount ? "disabled" : "", link : $scope.getLink($scope.pageCount) }); page.push({ name : 'Last page', style : $scope.pageId == $scope.pageCount ? "disabled" : "", link : $scope.getLink($scope.pageCount) }); return page; }; $scope.pageInit = function(){ if( !$scope.pageId || !$scope.pageRecord ){ setTimeout(function(){ $scope.$apply(function(){ $scope.pageInit(); }); }, 10); }else{ if( !!$scope.pageSize ){ $scope._pageSize = parseInt($scope.pageSize); }else{ $scope._pageSize = 10; } $scope.pageId = parseInt($scope.pageId); $scope.pageCount = parseInt(( $scope.pageRecord - 1 ) / $scope._pageSize ) + 1; if( $scope.pageId < 1 ){ $scope.pageId = 1; }else if( $scope.pageId > $scope.pageCount ){ $scope.pageId = $scope.pageCount; } $scope.pageLoad = true; $scope.pageList = $scope.getPageList(); } }; $scope.pageLoad = false; $scope.pageInit(); }] }});Call code:
<page-pagination page-id="pageId" page-record="recordCount" page-url-template="urlTemplate"></page-pagination>
The above is the entire content of the angular.js pagination code. I hope it will be helpful to everyone's learning, and I hope everyone will support Wulin.com more.