/*
file: pagination.js
functions for pagination of table data
one page contains nrows_perpage rows
one block contains npages
one block of table data is loaded in the DOM
one "page" = one element "tbody"
only one page is made visible (style="display: table-row-group")
the other pages ("tbody's") are not shown (style="display: none")
max_rows = total number of rows
i.e.: if we have 236 rows and npages= 10 and nrows_perpage = 10:
total pages = 24 (last page contains only 6 rows)
total blocks = 3 (last block contains only 4 pages)
slow forward/rewind ('>'/'<'): per page
fast forward/rewind ('>>'/'<<'): per block
hidden paginators (<<,<,> or >>) are used for alignment.
*/
function collapse_oldpage(idfrom) {
// hide the page (= tbody) where we came from
tbody = "tbody"+idfrom;
var p = document.getElementById(tbody);
if(p.style.display == 'table-row-group') {
p.style.display = 'none';
}
}
function change_pagination(sort,id,npages,nrows_perpage,max_rows) {
/*
before displaying a new page get the pagination right
we create a new div and use jquery's "replaceWith" to put the
new div in place.
The arguments:
sort: sort order for the rows, needed as argument for show_page
id: id of page which was clicked
npages: number of pages to present at once (with only one page visible, the rest hidden)
nrows_perpage: number of rows per page
max_rows: total number of rows in the database
*/
var replace = '
';
$("#pagination").replaceWith(replace);
}
function show_page(sort,id,idfrom,npages,nrows,max_rows) {
// change the div pagination
// if max_rows = -1 (only at the start) we need to get it
if (max_rows == -1) {
var max_rows = $.ajax({ url: "max_rows.php", async: false }).responseText;
}
change_pagination(sort,id,npages,nrows,max_rows);
// change the old page from visible (style.display == 'table-row-group') to hidden (style.display = 'none')
// if idfrom == -1 (start) there's nothing to collapse
if (idfrom != -1) {
collapse_oldpage(idfrom);
}
// calculate rest if id == lastpageid
rest = npages;
lastpageid = Math.ceil(max_rows/nrows);
if ((id == lastpageid) && (id%npages != 0)) {
rest = id%npages;
}
// get new rows from the database
// do this only when we press next ('>', or '>>') or previous ('<', or '<<') or if we press the first or last pageid and we come from another block
if ( (id%npages == 1 && (id > idfrom || id <= (idfrom - npages))) || (id%npages == 0 && (idfrom - id == 1)) || (id == lastpageid && (id - rest >= idfrom)) ) {
// we need to get new rows from the database, show a "loading" message.
$("#main").replaceWith("Loading new data...
");
// get new rows
// determine begin row for first page (id = clicked page)
if (id == lastpageid) {
if (id%npages == 0) {
from = (id - npages) * nrows;
} else {
from = (id - (id%npages)) * nrows;
}
} else {
if (id%npages == 0) {
from = (id - npages) * nrows;
} else {
// we pressed 1, nextpage or nextblock.
from = (id - 1) * nrows;
}
}
// we need npages*nrows to display
to = npages * nrows;
// start the new div
var newrows = '';
// get all rows using an ajax-call to get_rows.php
// get_rows.php return the table with the new rows
// get_rows.php makes the correct page (=tbody) visible
var rows = $.ajax({ url: "get_rows.php",
data: "sort="+sort+"&id="+id+"&npages="+npages+"&nrows="+nrows+"&max_rows="+max_rows+"&from="+from+"&to="+to,
async: false
}).responseText;
// close our div "main"
newrows = newrows+rows+'
';
// use jqeury's "replaceWith" to replace the existing div with id = "main" with the new data
$("#main").replaceWith(newrows);
} else {
// we did not load new data
// we already have hidden the old page (with collapse_oldpage(idfrom))
// make page (= tbody) visible with id which we clicked in the navigation div
$("#tbody"+id).attr("style", "display:table-row-group");
}
}
function sorted_rows(sort,startid,npages,nrows,max_rows) {
$("#main").replaceWith("Loading new data...
");
change_pagination(sort,startid,npages,nrows,max_rows);
from = (startid - 1) * nrows;
to = npages * nrows;
var newrows = '';
var rows = $.ajax({ url: "get_rows.php",
data: "sort="+sort+"&id="+startid+"&npages="+npages+"&nrows="+nrows+"&max_rows="+max_rows+"&from="+from+"&to="+to,
async: false
}).responseText;
newrows = newrows+rows+'
';
$("#main").replaceWith(newrows);
}