So, I'm receiving a json file through ajax and showing it in a table. I want to paginate that array. So here's what I'm doing
function Paginator(groups){
/**
* This should work.
* The children() function returns a JQuery object that contains the children.
* So you just need to check the size and see if it has at least one child.
* #grouplist > * makes it slightly faster - I think...
*/
if ( $('#grouplist > *').length > 0 ) {
$.each($('#grouplist').children(), function(i, current) {
current.remove();
});
}
//http://davidwalsh.name/javascript-clone-array
var clone_group = groups.slice(0);
var page = _globalpage,
startRec = Math.max(page - 1, 0) * _recPerPage,
endRec = Math.min(startRec + _recPerPage, clone_group.length)
recordsToShow = clone_group.splice(startRec, endRec);
console.log('start '+startRec+' end '+endRec+' page '+ page + ' records to show '+recordsToShow.length);
// loop through the array to populate your list
$.each(recordsToShow, function(i, currentGroup){
console.log('id '+currentGroup.id);
$('#grouplist').append('<tr> /*html with stuff I need*/ </tr>');
});
}
Probably not good practice but every variable that starts with underscore '_' is globaly defined. The only one that changes is _globalpage that gets incremented or decremented by 1 everytime you hit "previous" or "next". I'm thinking of changing those to cookies, once I get this to work properly.
This is the result from the console.log
"start 0 end 10 page 1 records to show 10"
"id 1" through "id 10"
after hitting next
"start 10 end 20 page 2 records to show 20"
"id 11" to "id 30"
What's wrong with this? Thanks in advance