Created
January 26, 2011 22:23
-
-
Save jaredhoyt/797617 to your computer and use it in GitHub Desktop.
Custom pagination with results per page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
results = { | |
ajaxRequest : null, | |
ajaxLoader : '<div class="ajax_loader"><div class="overlay"></div><div class="message">Loading...</div></div>', | |
displayLoader : function(container){ | |
container = container || $('div.records:first'); | |
if ($(container).find('div.ajax_loader').length) { | |
return; | |
} | |
// Insert ajax loader into DOM | |
var loader = $(this.ajaxLoader).prependTo(container); | |
// Center ajax loader | |
loader.children('div.message').css({ | |
left : $('div.ajax_loader').width()/2 - 77, | |
top : $('div.ajax_loader').height()/2 - 29 | |
}); | |
loader.show(); | |
}, | |
load : function(url, container) { | |
url = url || window.location; | |
container = container || $('div.records:first'); | |
var index = null; | |
$('div.records').each(function(i){ | |
if (this == container[0]) { | |
index = i; | |
} | |
}); | |
// Display ajax loader | |
this.displayLoader(container); | |
// Cancel any current ajax requests | |
if (this.ajaxRequest) { | |
this.ajaxRequest.abort(); | |
} | |
// Load records via AJAX | |
this.ajaxRequest = $.ajax({ | |
data : $('select[name^="data[Filter]"]').serializeArray(), | |
success : function(response){ | |
$(container).html($(response).find('div.records').eq(index)); | |
}, | |
type : "POST", | |
url : url | |
}); | |
} | |
}; | |
pagination = { | |
current : function(field){ | |
var url = $('#pagination_url').val(); | |
var pattern = new RegExp(field + ':([0-9]+)', 'i'); | |
var matches = url.match(pattern); | |
return matches ? parseInt(matches[1]) : false; | |
}, | |
update : function(field, value){ | |
var url = $('#pagination_url').val(); | |
var pattern = new RegExp(field + ':[0-9]+', 'i'); | |
if (this.current(field)) { | |
results.load(url.replace(pattern, field + ':' + value)); | |
} else { | |
results.load(url + '/' + field + ':' + value); | |
} | |
} | |
}; | |
jQuery(function(){ | |
// Load results when filter is changed | |
$('select[name^="data[Filter]"]').change(function(){ | |
results.load(); | |
}); | |
// Load results when pagination is changed | |
$('div.records').delegate('ul.pagination li.next.btn', 'click', function(){ | |
pagination.update('page', pagination.current('page') + 1); | |
}); | |
$('div.records').delegate('ul.pagination li.prev.btn', 'click', function(){ | |
pagination.update('page', pagination.current('page') - 1); | |
}); | |
$('div.records').delegate('ul.pagination li.total', 'click', function(){ | |
pagination.update('page', parseInt($(this).text())); | |
}); | |
$('div.records').delegate('ul.pagination input.current_page', 'change', function(){ | |
pagination.update('page', $(this).val()); | |
}); | |
$('div.records').delegate('#pagination_limit', 'change', function(){ | |
pagination.update('limit', $(this).val()); | |
}); | |
$('div.records').delegate('table th a', 'click', function(event){ | |
results.load(this.href); | |
event.preventDefault(); | |
}); | |
// Select all rows in table | |
$('div.records').delegate('input.select_column', 'click', function(){ | |
var rows = $(this).parents('div.records:first').find('input.select_row'); | |
if ($(this).is(':checked')) { | |
rows.attr('checked', 'checked'); | |
} else { | |
rows.removeAttr('checked'); | |
} | |
}); | |
// Select single row in table | |
$('div.records').delegate('input.select_row', 'click', function(){ | |
}); | |
// Mass record updating | |
$('select.more_actions').live('change', function(){ | |
var container = $(this).parents('div.widget:first').children('div.records'); | |
if ($(this).val() && $('input.select_row:checked').length) { | |
if (confirm('Are you sure you want to perform this action?')) { | |
var controller = $(this).attr('data-controller'); | |
var action = $(this).val(); | |
var remaining = $('input.select_row:checked').length; | |
results.displayLoader(container); | |
$('input.select_row:checked').each(function(){ | |
$.get('/' + controller + '/' + action + '/' + $(this).val(), function(){ | |
remaining -= 1; | |
// Reload results when all call are completed | |
if (remaining == 0) { | |
results.load(null, container); | |
} | |
}); | |
}); | |
} | |
} | |
$(this).val(''); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input type="hidden" id="pagination_url" value="<?php echo $this->Paginator->url(); ?>" /> | |
<?php if ($this->Paginator->counter(array('format' => '%pages%')) > 1) : ?> | |
<div class="pagination_container"> | |
<ul class="pagination"> | |
<li class="btn prev<?php if (!$this->Paginator->hasPrev()) { echo ' disabled'; } ?>" title="Go to the previous page"></li> | |
<li class="input"><input type="text" value="<?php echo $this->Paginator->counter(array('format' => '%page%'));?>" class="current_page" /></li> | |
<li class="of">of</li> | |
<li class="total" title="Go to the last page"><?php echo $this->Paginator->counter(array('format' => '%pages%'));?></li> | |
<li class="btn next<?php if (!$this->Paginator->hasNext()) { echo ' disabled'; } ?>" title="Go to the next page"></li> | |
</ul> | |
</div> | |
<?php endif; ?> | |
<?php if ($this->Paginator->counter(array('format' => '%count%')) > 20) : ?> | |
<div class="pagination_limit"> | |
<?php $params = $this->Paginator->params(); ?> | |
<?php echo $this->Form->input('pagination_limit', array('id' => 'pagination_limit', 'label' => 'Show Per Page:', 'selected' => $params['options']['limit'], 'options' => array(20 => 20, 50 => 50, 100 => 100))); ?> | |
</div> | |
<?php endif; ?> | |
<div class="record_count"><?php echo $this->Paginator->counter(array('format' => 'Results <strong>%start%</strong> - <strong>%end%</strong> of <strong>%count%</strong>')); ?></div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment