Kyle
Kyle

Reputation: 4298

Server side pagination with tablesorter

Is server side pagination possible with tablesorter's pager plugin? It looks like the default options require you to load all your rows into browser's memory at once. Since I have so many records this isn't really possible, I would prefer to load one page at a time. Does the tablesorter pager plugin support this? If so, what am I missing, because the documentation shows this example:

// process ajax so that the data object is returned along with the total number of rows
    // example: { "data" : [{ "ID": 1, "Name": "Foo", "Last": "Bar" }], "total_rows" : 100 }
    ajaxProcessing: function(ajax){
      if (ajax && ajax.hasOwnProperty('data')) {
        // return [ "data", "total_rows" ];
        return [ ajax.data, ajax.total_rows ];
      }
    },

That, and other examples I've been able to find, look like it loads all rows at once into memory in the ajax processing function.

http://mottie.github.com/tablesorter/docs/example-pager.html

Update:

After seeing the AJAX pager at http://mottie.github.com/tablesorter/docs/example-pager-ajax.html I have a few questions still:

  1. sortList = [[2,0],[3,0]]. In the Ajax URL, do I do the conversion to the real format of &col[2]=0&col[3]=0 myself?
  2. The documentation for the ajaxProcessing says:

    process ajax so that the following information is returned: // [ total_rows (number), rows (array of arrays), headers (array; optional) ]

It looks like total_rows is the number of rows in the database, not the number of rows in the browser memory or shown in the table. Is that correct? Next question: I understand the format of the "rows" array of arrays. But which rows are actually supposed to be in it? The documentation says it's "all rows" but is it just the current page of rows that is being displayed in the table? Is it all of the rows that the user has paged through thus far? I assume it isn't all of the rows that are in the DB because that would ruin the point entirely.

Upvotes: 4

Views: 7235

Answers (1)

Mottie
Mottie

Reputation: 86483

To include the sort column and direction just include the server side variable col in the example within the url template:

ajaxUrl : "http:/mydatabase.com?page={page}&size={size}&{sortList:col}&{filterList:fcol}",

The {page} is the current page the user is viewing and {size} is the number of rows to show in the browser.

Include &{sortList:col} (with col matching the server side variable for sort column and direction) to include sorting. And include &{filterLost:fcol} (with fcol matching the server side varibale for filtering columns) to include any filtering. The pager plugin formats the string into &col[2]=0&col[3]=0 (or whatever) for you.

If you look at the ajaxProcessing function, all it does is reformat the ajax data from your server of the current set of rows to display (not all rows) to match this required format:

// process ajax so that the following information is returned:
// [ total_rows (number), rows (array of arrays), headers (array; optional) ]
// example:
[
  100,  // total rows
  [
    [ "row1cell1", "row1cell2", ... "row1cellN" ],
    [ "row2cell1", "row2cell2", ... "row2cellN" ],
    ...
    [ "rowNcell1", "rowNcell2", ... "rowNcellN" ]
  ],
  [ "header1", "header2", ... "headerN" ] // optional
]

If you have an indeterminate number of rows in your database, just return 0... it should still work, but then the totalPages and totalRows variables will be inaccurate.

Upvotes: 2

Related Questions