이번 플러그인은 pager 입니다.
http://www.j-dee.com/2008/12/22/jquery-pager-plugin/
이 pager의 각 단위는 span 으로 렌더링 됩니다. 그래서 디자인 하기가 편합니다..^^;;
그런데 이 pager는 페이징 방식이...다이얼방식(표현이 맞나 모르겠네요..@.@) 입니다.
즉 현재 활성화된 페이지가 가운데 오는 방식이죠...외국 사이트에서는 이러한 방식을 많이 쓰고 있습니다만..
우리의 고객들은....싫어 하더군요..ㅡ.ㅡ;
그래서 전통적인 방식의 기능도 추가 하였습니다.
사용법은 아래와 같습니다.
var initializePager = function(){ if(parseInt($('#totalCnt').val()) >= 0 ){ var cur = $.getUrlVars()['pageCnt']; var rowCount = parseInt($('#rowCnt').val()); $('#pager').pager({ pagerType : 'classic', pagenumber : rowCount==0?1:(cur==null?1:cur), pagecount : rowCount==0?1:parseInt($('#totalCnt').val())%parseInt($('#rowCnt').val())==0?parseInt($('#totalCnt').val())/parseInt($('#rowCnt').val()):parseInt($('#totalCnt').val())/parseInt($('#rowCnt').val()) + 1, buttonClickCallback: pagerCallback }); } }; // 페이저를 클릭 했을 경우 function pagerCallback(pageClickNumber){ var pageCnt = $.getUrlVars()['pageCnt']; if(pageCnt == null){ if(location.href.indexOf('?') > 0 && $.getUrlVars().length > 0){ location.href = location.href+'&pageCnt='+pageClickNumber; }else{ location.href = location.href+'?pageCnt='+pageClickNumber; } } else{ var reg = /pageCnt=(\d+)/gi; location.href = location.href.replace(reg, 'pageCnt='+pageClickNumber); } return false; }
공통 js 파일에 위와 같이 선언해 놓고..
pager가 필요한 페이지에서만 위 함수를 콜하면 되겠습니다...
pager.js 의 전체 코드는 아래와 같습니다.
/* * jQuery pager plugin * Version 1.0 (12/22/2008) * @requires jQuery v1.2.6 or later * * Example at: http://jonpauldavies.github.com/JQuery/Pager/PagerDemo.html * * Copyright (c) 2008-2009 Jon Paul Davies * Dual licensed under the MIT and GPL licenses: * http://www.opensource.org/licenses/mit-license.php * http://www.gnu.org/licenses/gpl.html * * Read the related blog post and contact the author at http://www.j-dee.com/2008/12/22/jquery-pager-plugin/ * * This version is far from perfect and doesn't manage it's own state, therefore contributions are more than welcome! * * Usage: .pager({ pagenumber: 1, pagecount: 15, buttonClickCallback: PagerClickTest }); * * Where pagenumber is the visible page number * pagecount is the total number of pages to display * buttonClickCallback is the method to fire when a pager button is clicked. * * buttonClickCallback signiture is PagerClickTest = function(pageclickednumber) * Where pageclickednumber is the number of the page clicked in the control. * * The included Pager.CSS file is a dependancy but can obviously tweaked to your wishes * Tested in IE6 IE7 Firefox & Safari. Any browser strangeness, please report. */ (function ($) { var pagerType = 'classic'; $.fn.pager = function (options) { var opts = $.extend({}, $.fn.pager.defaults, options); pagerType = opts.pagerType; return this.each(function () { // empty out the destination element and then render out the pager // with the supplied options $(this).empty().append(renderpager(parseInt(options.pagenumber), parseInt(options.pagecount), options.buttonClickCallback)); // specify correct cursor activity $('.pages span').mouseover(function () { document.body.style.cursor = "pointer"; }).mouseout(function () { document.body.style.cursor = "auto"; }); }); }; // render and return the pager with the supplied options function renderpager(pagenumber, pagecount, buttonClickCallback) { // setup $pager to hold render var $pager = $('<div class="pages"></div>'); // add in the previous and next buttons $pager.append(renderButton('first', pagenumber, pagecount, buttonClickCallback)).append(renderButton('prev', pagenumber, pagecount, buttonClickCallback)); // pager currently only handles 10 viewable pages ( could be easily // parameterized, maybe in next version ) so handle edge cases var startPoint = 1; var endPoint = 9; if (pagerType == 'classic') { endPoint = 10; startPoint = parseInt((pagenumber - 1) / 10) * 10 + 1; endPoint = parseInt((pagenumber - 1) / 10) * 10 + 10; if (endPoint > pagecount) { endPoint = pagecount; startPoint = parseInt(pagecount / 10) * 10 + 1; } } else { if (pagenumber > 4) { startPoint = pagenumber - 4; endPoint = pagenumber + 4; } if (endPoint > pagecount) { startPoint = pagecount - 8; endPoint = pagecount; } } if (startPoint < 1) { startPoint = 1; } // loop thru visible pages and render buttons for (var page = startPoint; page <= endPoint; page++) { var currentButton = $('<span class="page-number">' + (page) + '</span>'); page == pagenumber ? currentButton.addClass('pgCurrent') : currentButton.click(function () { buttonClickCallback(this.firstChild.data); }); currentButton.appendTo($pager); } // render in the next and last buttons before returning the whole // rendered control back. $pager.append(renderButton('next', pagenumber, pagecount, buttonClickCallback)).append(renderButton('last', pagenumber, pagecount, buttonClickCallback)); return $pager; } // renders and returns a 'specialized' button, ie 'next', 'previous' etc. // rather than a page number button function renderButton(buttonLabel, pagenumber, pagecount, buttonClickCallback) { var $Button = $('<span class="pgNext"> </span>'); var destPage = 1; // work out destination page for required button type switch (buttonLabel) { case "first": destPage = 1; $Button.addClass('first'); break; case "prev": if (pagerType == 'classic') { if (pagenumber < 10) { destPage = 1; } else { destPage = parseInt((pagenumber - 1) / 10) * 10; if (destPage == 0) { destPage = 1; } } } else { destPage = pagenumber - 1; } $Button.addClass('pre'); break; case "next": if (pagerType == 'classic') { destPage = parseInt((pagenumber - 1) / 10) * 10 + 10 + 1; if (destPage > pagecount) { destPage = parseInt(pagecount / 10) * 10 + 1; } } else { destPage = pagenumber + 1; } $Button.addClass('next'); break; case "last": destPage = pagecount; $Button.addClass('last'); break; } // disable and 'grey' out buttons if not needed. if (buttonLabel == "first" || buttonLabel == "prev") { pagenumber <= 1 ? $Button.addClass('pgEmpty') : $Button.click(function () { buttonClickCallback(destPage); }); } else if (buttonLabel == "next") { if (pagerType == 'classic') { parseInt((pagenumber - 1) / 10) * 10 + 10 >= pagecount ? $Button.addClass('pgEmpty') : $Button.click(function () { buttonClickCallback(destPage); }); } else { pagenumber >= pagecount ? $Button.addClass('pgEmpty') : $Button.click(function () { buttonClickCallback(destPage); }); } } else { pagenumber >= pagecount ? $Button.addClass('pgEmpty') : $Button.click(function () { buttonClickCallback(destPage); }); } return $Button; } // pager defaults. hardly worth bothering with in this case but used as // placeholder for expansion in the next version $.fn.pager.defaults = { pagenumber: 1, pagecount: 1 }; })(jQuery);