jquery/build/docs/js/pager.js

114 lines
2.7 KiB
JavaScript
Raw Normal View History

2006-08-13 03:05:31 +00:00
$.fn.alphaPager = function(fn,type) {
type = type || "char";
if ( fn == undefined ) {
fn = function(a){ return _clean( $.fn.text.apply( a.childNodes ) ); };
} else if ( fn.constructor == Number ) {
var n = fn;
fn = function(a){ return _clean( $.fn.text.apply( [a.childNodes[ n ]] ) ); };
}
function _clean(a){
switch (type) {
case "char":
return a.substr(0,1).toUpperCase();
case "word":
return /^([a-z0-9]+)/.exec(a)[1];
}
return a;
}
return this.pager( fn );
2006-08-09 05:44:05 +00:00
};
2006-08-13 03:05:31 +00:00
$.fn.pager = function(step) {
2006-08-09 05:44:05 +00:00
var types = {
UL: "li",
OL: "li",
DL: "dt",
2006-08-13 03:05:31 +00:00
TABLE: "tr"
2006-08-09 05:44:05 +00:00
};
return this.each(function(){
2006-08-13 03:05:31 +00:00
var type = types[this.nodeName];
var pagedUI = type == "tr" ? $("tbody",this) : $(this);
var rows = $(type, pagedUI);
2006-08-09 05:44:05 +00:00
var curPage = 0;
var names = [], num = [];
if ( !step || step.constructor != Function ) {
step = step || 10;
if (rows.length > step)
for ( var i = 0; i <= rows.length; i += step ) {
names.push( names.length + 1 );
2006-08-13 03:05:31 +00:00
num.push( [ i, step ] );
2006-08-09 05:44:05 +00:00
}
} else {
var last;
rows.each(function(){
2006-08-13 03:05:31 +00:00
var l = step( this );
2006-08-09 05:44:05 +00:00
if ( l != last ) {
2006-08-13 03:05:31 +00:00
names.push( l );
2006-08-09 05:44:05 +00:00
var pre = num.length ? num[ num.length - 1 ][0] + num[ num.length - 1 ][1] : 0;
num.push( [ pre, 0 ] );
last = l;
}
num[ num.length - 1 ][1]++;
});
}
2006-08-13 03:05:31 +00:00
if ( names.length > 1 ) {
var pager = $(this).prev("ul.nav-page").empty();
if ( !pager.length )
pager = $("<ul class='nav-page'></ul>");
2006-08-09 05:44:05 +00:00
for ( var i = 0; i < names.length; i++ )
$("<a href=''></a>").attr({
rel: i, innerHTML: names[i]
}).click(function() {
2006-08-09 05:44:05 +00:00
return handleCrop( this.rel );
}).wrap("<li></li>").parent().appendTo(pager);
pager.insertBefore( this );
var prev = $("<a href=''>&laquo; Prev</a>").click(function(){
return handleCrop( --curPage );
}).wrap("<li class='prev'></li>").parent().prependTo(pager);
var next = $("<a href=''>Next &raquo;</a>").click(function(){
return handleCrop( ++curPage );
}).wrap("<li class='next'></li>").parent().appendTo(pager);
handleCrop( 0 );
}
function handleCrop( page ) {
curPage = page - 0;
var s = num[ curPage ][0];
2006-08-13 03:05:31 +00:00
var e = num[ curPage ][1];
2006-08-09 05:44:05 +00:00
if ( !curPage ) prev.hide();
else prev.show();
if ( curPage == names.length - 1 ) next.hide();
else next.show();
$("li",pager)
.removeClass("cur")
.eq( curPage + 1 )
.addClass("cur");
2006-08-13 03:05:31 +00:00
pagedUI.empty().append(
jQuery.makeArray( rows ).slice( s, s + e )
2006-08-13 03:05:31 +00:00
);
2006-08-09 05:44:05 +00:00
return false;
}
});
};