您好,登錄后才能下訂單哦!
這篇文章給大家介紹使用angularjs怎么實(shí)現(xiàn)一個(gè)前端分頁(yè)控件,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
用法:
angular-pagination.js代碼:
/** * angularjs分頁(yè)控件 */ 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) { //容錯(cuò)處理 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; //得到顯示頁(yè)數(shù)的數(shù)組 $scope.page = getRange($scope.option.curr, $scope.option.all, $scope.option.count); //綁定點(diǎn)擊事件 $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; //點(diǎn)擊相同的頁(yè)數(shù) 不執(zhí)行點(diǎn)擊事件 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); } }; //返回頁(yè)數(shù)范圍(用來(lái)遍歷) function getRange(curr, all, count) { //計(jì)算顯示的頁(yè)數(shù) curr = parseInt(curr); all = parseInt(all); count = parseInt(count); var from = curr - parseInt(count / 2); var to = curr + parseInt(count / 2) + (count % 2) - 1; //顯示的頁(yè)數(shù)容處理 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分頁(yè)控件</title> <script src="angular.min.js"></script> <!-- 引入angularjs文件,而且是1.x版本的 --> <script src="angular-pagination.js"></script> <!-- 引入angularjs分頁(yè)控件 --> <script src="app.js"></script> <!-- 引入app.js --> </head> <body> <!-- 控制器 --> <div ng-controller="myCtrl"> <!-- 分頁(yè)控件指令 ,可以是元素 或者 屬性 --> <my-pagination page-option="option"></my-pagination> </div> </body> </html>
app.js代碼:
//引入 'myModele' 模塊 var app = angular.module('app', ['myModule']); app.contriller('myCtrl', function($scope){ //設(shè)置分頁(yè)的參數(shù) $scope.option = { curr: 1, //當(dāng)前頁(yè)數(shù) all: 20, //總頁(yè)數(shù) count: 10, //最多顯示的頁(yè)數(shù),默認(rèn)為10 //點(diǎn)擊頁(yè)數(shù)的回調(diào)函數(shù),參數(shù)page為點(diǎn)擊的頁(yè)數(shù) click: function (page) { console.log(page); //這里可以寫(xiě)跳轉(zhuǎn)到某個(gè)頁(yè)面等... } } });
關(guān)于使用angularjs怎么實(shí)現(xiàn)一個(gè)前端分頁(yè)控件就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。