您好,登錄后才能下訂單哦!
這篇文章給大家介紹使用angularjs怎么實現一個前端分頁控件,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
用法:
angular-pagination.js代碼:
/** * angularjs分頁控件 */ angular.module('myModule', []).directive('myPagination', function () { return { restrict: 'EA', replace: true, scope: { option: '=pageOption' }, template: '<ul class="pagination">' + '<li ng-click="pageClick(p)" ng-repeat="p in page" class="{{option.curr==p?\'active\':\'\'}}">' + '<a href="javascript:;" rel="external nofollow" >{{p}}</a>' + '</li>' + '</ul>', link: function ($scope) { //容錯處理 if (!$scope.option.curr || isNaN($scope.option.curr) || $scope.option.curr < 1) $scope.option.curr = 1; if (!$scope.option.all || isNaN($scope.option.all) || $scope.option.all < 1) $scope.option.all = 1; if ($scope.option.curr > $scope.option.all) $scope.option.curr = $scope.option.all; if (!$scope.option.count || isNaN($scope.option.count) || $scope.option.count < 1) $scope.option.count = 10; //得到顯示頁數的數組 $scope.page = getRange($scope.option.curr, $scope.option.all, $scope.option.count); //綁定點擊事件 $scope.pageClick = function (page) { if (page == '«') { page = parseInt($scope.option.curr) - 1; } else if (page == '»') { page = parseInt($scope.option.curr) + 1; } if (page < 1) page = 1; else if (page > $scope.option.all) page = $scope.option.all; //點擊相同的頁數 不執行點擊事件 if (page == $scope.option.curr) return; if ($scope.option.click && typeof $scope.option.click === 'function') { $scope.option.click(page); $scope.option.curr = page; $scope.page = getRange($scope.option.curr, $scope.option.all, $scope.option.count); } }; //返回頁數范圍(用來遍歷) function getRange(curr, all, count) { //計算顯示的頁數 curr = parseInt(curr); all = parseInt(all); count = parseInt(count); var from = curr - parseInt(count / 2); var to = curr + parseInt(count / 2) + (count % 2) - 1; //顯示的頁數容處理 if (from <= 0) { from = 1; to = from + count - 1; if (to > all) { to = all; } } if (to > all) { to = all; from = to - count + 1; if (from <= 0) { from = 1; } } var range = []; for (var i = from; i <= to; i++) { range.push(i); } range.push('»'); range.unshift('«'); return range; } } } });
index.html代碼:
<!doctype html> <html ng-app="app"> <head> <meta charset="UTF-8"> <title>Angularjs分頁控件</title> <script src="angular.min.js"></script> <!-- 引入angularjs文件,而且是1.x版本的 --> <script src="angular-pagination.js"></script> <!-- 引入angularjs分頁控件 --> <script src="app.js"></script> <!-- 引入app.js --> </head> <body> <!-- 控制器 --> <div ng-controller="myCtrl"> <!-- 分頁控件指令 ,可以是元素 或者 屬性 --> <my-pagination page-option="option"></my-pagination> </div> </body> </html>
app.js代碼:
//引入 'myModele' 模塊 var app = angular.module('app', ['myModule']); app.contriller('myCtrl', function($scope){ //設置分頁的參數 $scope.option = { curr: 1, //當前頁數 all: 20, //總頁數 count: 10, //最多顯示的頁數,默認為10 //點擊頁數的回調函數,參數page為點擊的頁數 click: function (page) { console.log(page); //這里可以寫跳轉到某個頁面等... } } });
關于使用angularjs怎么實現一個前端分頁控件就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。