Duplicate:
Good jquery pagination plugin to use with json Data…
My JSON data looks like this
You can use the Array.splice-method to create groups of the size you want from the array:
// Parse json etc.
var json = [...];
// Fetch the data for a page from the json-result object
var page = 1,
recPerPage = 5,
// Use Math.max to ensure that we at least start from record 0
startRec = Math.max(page - 1, 0) * 5,
// The end index of Array.splice doesn't have to be within boundaries,
// But if you want to ensure that it does, then use
// Math.min(startRec + recPerPage, json.table.length)
endRec = startRec + recPerPage
recordsToShow = json.table.splice(startRec, endRec);
recordsToShow
now contains an array of records to show for a page. Refactor out page = 1
and take it as a parameter, and do the same for recPerPage = 5
, and you should be good to go. You can use jQuery.each to iterate through recordsToShow
, and use some kind of templating system to create HTML-elements from each record.
You should also add some kind of check to startRec
to ensure that the starting record is within boundaries. If it is not, then either display page 1, or display an error message to the user.