Debian Codegraph @

docs/example-pager-ajax.html

raw symbols SCIP
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta charset="utf-8">
5 <title>jQuery tablesorter 2.0 - Pager plugin - Ajax</title>
6
7 <!-- jQuery -->
8 <script src="js/jquery-latest.min.js"></script>
9
10 <!-- Demo stuff -->
11 <link class="ui-theme" rel="stylesheet" href="css/jquery-ui.min.css">
12 <link href="css/jq.css" rel="stylesheet">
13 <link href="css/prettify.css" rel="stylesheet">
14 <script src="js/jquery-ui.min.js"></script>
15 <script src="js/prettify.js"></script>
16 <script src="js/docs.js"></script>
17
18 <!-- Tablesorter: required -->
19 <link rel="stylesheet" href="../css/theme.blue.css">
20 <script src="../js/jquery.tablesorter.js"></script>
21
22 <!-- Tablesorter pager: required -->
23 <link rel="stylesheet" href="../addons/pager/jquery.tablesorter.pager.css">
24 <script src="../addons/pager/jquery.tablesorter.pager.js"></script>
25
26 <!-- Tablesorter: optional -->
27 <script src="../js/jquery.tablesorter.widgets.js"></script>
28
29 <script id="js">$(function() {
30
31 // Initialize tablesorter
32 // ***********************
33 $("table")
34 .tablesorter({
35 theme: 'blue',
36 widthFixed: true,
37 sortLocaleCompare: true, // needed for accented characters in the data
38 sortList: [ [0,1] ],
39 widgets: ['zebra', 'filter']
40 })
41
42 // initialize the pager plugin
43 // ****************************
44 .tablesorterPager({
45
46 // **********************************
47 // Description of ALL pager options
48 // **********************************
49
50 // target the pager markup - see the HTML block below
51 container: $(".pager"),
52
53 // If you want to use ajaxUrl placeholders, here is an example:
54 // ajaxUrl: "http:/mydatabase.com?page={page}&size={size}&{sortList:col}"
55 // where {page} is replaced by the page number (or use {page+1} to get a one-based index),
56 // {size} is replaced by the number of records to show,
57 // {sortList:col} adds the sortList to the url into a "col" array, and {filterList:fcol} adds
58 // the filterList to the url into an "fcol" array.
59 // So a sortList = [[2,0],[3,0]] becomes "&col[2]=0&col[3]=0" in the url
60 // and a filterList = [[2,Blue],[3,13]] becomes "&fcol[2]=Blue&fcol[3]=13" in the url
61 ajaxUrl : 'assets/City{page}.json?{filterList:filter}&{sortList:column}',
62
63 // use this option to manipulate and/or add additional parameters to the ajax url
64 customAjaxUrl: function(table, url) {
65 // manipulate the url string as you desire
66 // url += '&currPage=' + window.location.pathname;
67
68 // trigger a custom event; if you want
69 $(table).trigger('changingUrl', url);
70 // send the server the current page
71 return url;
72 },
73
74 // ajax error callback from $.tablesorter.showError function
75 // ajaxError: function( config, xhr, settings, exception ) { return exception; };
76 // returning false will abort the error message
77 ajaxError: null,
78
79 // add more ajax settings here
80 // see http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings
81 ajaxObject: {
82 type: 'GET', // default setting
83 dataType: 'json'
84 },
85
86 // process ajax so that the following information is returned:
87 // [ total_rows (number), rows (array of arrays), headers (array; optional) ]
88 // example:
89 // [
90 // 100, // total rows
91 // [
92 // [ "row1cell1", "row1cell2", ... "row1cellN" ],
93 // [ "row2cell1", "row2cell2", ... "row2cellN" ],
94 // ...
95 // [ "rowNcell1", "rowNcell2", ... "rowNcellN" ]
96 // ],
97 // [ "header1", "header2", ... "headerN" ] // optional
98 // ]
99 // OR
100 // return [ total_rows, $rows (jQuery object; optional), headers (array; optional) ]
101 ajaxProcessing: function(data) {
102 if (data && data.hasOwnProperty('rows')) {
103 var indx, r, row, c, d = data.rows,
104 // total number of rows (required)
105 total = data.total_rows,
106 // array of header names (optional)
107 headers = data.headers,
108 // cross-reference to match JSON key within data (no spaces)
109 headerXref = headers.join(',').replace(/\s+/g,'').split(','),
110 // all rows: array of arrays; each internal array has the table cell data for that row
111 rows = [],
112 // len should match pager set size (c.size)
113 len = d.length;
114 // this will depend on how the json is set up - see City0.json
115 // rows
116 for ( r=0; r < len; r++ ) {
117 row = []; // new row array
118 // cells
119 for ( c in d[r] ) {
120 if (typeof(c) === "string") {
121 // match the key with the header to get the proper column index
122 indx = $.inArray( c, headerXref );
123 // add each table cell data to row array
124 if (indx >= 0) {
125 row[indx] = d[r][c];
126 }
127 }
128 }
129 rows.push(row); // add new row array to rows array
130 }
131 // in version 2.10, you can optionally return $(rows) a set of table rows within a jQuery object
132 return [ total, rows, headers ];
133 }
134 },
135
136 // Set this option to false if your table data is preloaded into the table, but you are still using ajax
137 processAjaxOnInit: true,
138
139 // output string - default is '{page}/{totalPages}';
140 // possible variables: {size}, {page}, {totalPages}, {filteredPages}, {startRow}, {endRow}, {filteredRows} and {totalRows}
141 // also {page:input} & {startRow:input} will add a modifiable input in place of the value
142 output: '{startRow} to {endRow} ({totalRows})',
143
144 // apply disabled classname (cssDisabled option) to the pager arrows when the rows
145 // are at either extreme is visible; default is true
146 updateArrows: true,
147
148 // starting page of the pager (zero based index)
149 page: 0,
150
151 // Number of visible rows - default is 10
152 size: 25,
153
154 // Saves the current pager page size and number (requires storage widget)
155 savePages: true,
156 // Saves tablesorter paging to custom key if defined.
157 // Key parameter name used by the $.tablesorter.storage function.
158 // Useful if you have multiple tables defined
159 storageKey: 'tablesorter-pager',
160
161 // Reset pager to this page after filtering; set to desired page number (zero-based index),
162 // or false to not change page at filter start
163 pageReset: 0,
164
165 // if true, the table will remain the same height no matter how many records are displayed.
166 // The space is made up by an empty table row set to a height to compensate; default is false
167 fixedHeight: false,
168
169 // remove rows from the table to speed up the sort of large tables.
170 // setting this to false, only hides the non-visible rows; needed if you plan to
171 // add/remove rows with the pager enabled.
172 removeRows: false,
173
174 // If true, child rows will be counted towards the pager set size
175 countChildRows: false,
176
177 // css class names of pager arrows
178 cssNext : '.next', // next page arrow
179 cssPrev : '.prev', // previous page arrow
180 cssFirst : '.first', // go to first page arrow
181 cssLast : '.last', // go to last page arrow
182 cssGoto : '.gotoPage', // page select dropdown - select dropdown that set the "page" option
183
184 cssPageDisplay : '.pagedisplay', // location of where the "output" is displayed
185 cssPageSize : '.pagesize', // page size selector - select dropdown that sets the "size" option
186
187 // class added to arrows when at the extremes; see the "updateArrows" option
188 // (i.e. prev/first arrows are "disabled" when on the first page)
189 cssDisabled : 'disabled', // Note there is no period "." in front of this class name
190 cssErrorRow : 'tablesorter-errorRow' // error information row
191
192 });
193
194 });</script>
195
196 </head>
197 <body id="pager-demo">
198 <div id="banner">
199 <h1>table<em>sorter</em></h1>
200 <h2>Pager plugin - Ajax</h2>
201 <h3>Flexible client-side table sorting</h3>
202 <a href="index.html">Back to documentation</a>
203 </div>
204
205 <div id="main">
206
207 <p></p>
208 <br>
209 <div class="accordion">
210
211 <h3><a href="#">Notes</a></h3>
212 <div>
213 <ul>
214 <li>In <span class="version updated">v2.26.0</span>, a page size of "all" will be passed to the server when set; <span class="label warning">Note</span> this change may break current server methods on number of pages to return.</li>
215 <li>In <span class="version">v2.13.3</span>, the "ID" column has a default filter setting of "&gt;30" and a descending sort, but neither is applied as this demo is not connected a server (just a basic JSON file).</li>
216 <li>In <span class="version">v2.11</span>, the pager now stores any object returned by the `ajaxProcessing` function in `table.config.pager.ajaxData` (see the ajaxProcessing section below for more details).</li>
217 <li>In <span class="version updated">v2.10</span>, the <code>ajaxProcessing</code> function was updated to only require a total number of rows to be returned, also instead of returning an array of table rows, you can build the table yourself and just return the jQuery object containing those rows. The addon triggers an update.</li>
218 <li><code>{filterList:fcol}</code> was added to the <code>ajaxUrl</code> in version 2.6.</li>
219 <li><code>{sortList:col}</code> was added to the <code>ajaxUrl</code> in version 2.4.5.</li>
220 <li>This update to the pager plugin that interacts with a database via ajax was added in version 2.0.32 and can be applied to the original tablesorter.</li>
221 <li>The <code>ajaxUrl</code> and <code>ajaxProcessing</code> function are both required options for this interaction to work properly.</li>
222 <li>The <code>ajaxUrl</code> contains a replaceable string to send the requested page (<code>{page}</code>), block size (<code>{size}</code>) or sort order (<code>{sortList:name}</code>).</li>
223 <li>The table header and footer text will be updated to match the JSON "header column #" text; but there is an issue with the table rendering improperly if the number of columns in the HTML and the number of columns in the JSON don't match.</li>
224 <li><strong>Limitations of this demo</strong>:
225 <ul>
226 <li>This demo will not work when viewing it locally (except in Firefox) due to communication restrictions between the browser and your desktop.</li>
227 <li>The record size is limited to 25 records because this demo is not interacting with an actual database, but with four JSON files containing 25 records each.</li>
228 <li>Sorting of columns is disabled in this demo because no table data is cached when <code>serverSideSorting</code> is enabled. <del>Sorting of columns is enabled in this demo by setting the <code>serverSideSorting</code> option to <code>false</code> after initialization. It only sorts the current table contents because there is no server to return sorted data</del>.</li>
229 <li>The filters will only update the "Current Ajax url" because again, we're just working with JSON files here.</li>
230 </ul>
231 </li>
232 </ul>
233 <p class="small">* If you have a different JSON format and need help with the processing code, please ask the question on <a href="http://stackoverflow.com/questions/tagged/tablesorter">StackOverflow</a> or message me directly (gmail; wowmotty). Please don't open an issue for help with code.</p>
234 </div>
235
236 <h3><a href="#">ajaxProcessing</a></h3>
237 <div>
238 The <code>ajaxProcessing</code> function is needed to convert your custom JSON format into an array usable by the pager plugin (modified in 2.1.3)<br>
239 <br>
240 So, given this custom JSON format (this is only an example):
241 <pre class="prettyprint lang-javascript">{
242 "total_rows": 80,
243
244 "headers" : [
245 "ID", "Name", "Country Code", "District", "Population"
246 ],
247
248 "rows" : [{
249 "ID": 1,
250 "Name": "Kabul",
251 "CountryCode": "AFG",
252 "District": "Kabol",
253 "Population": 1780000
254 }, {
255 "ID": 2,
256 "Name": "Qandahar",
257 "CountryCode": "AFG",
258 "District": "Qandahar",
259 "Population": 237500
260 }, {
261 ...
262 }, {
263 "ID": 25,
264 "Name": "Haarlemmermeer",
265 "CountryCode": "NLD",
266 "District": "Noord-Holland",
267 "Population": 110722
268 }]
269 }</pre>
270 <h3>OBJECT returned</h3>
271 <ul>
272 <li>In <span class="version">v2.11</span>, the <code>ajaxProcessing</code> can just return the above object directly (<strong>or don't even bother setting an <code>ajaxProcessing</code> function</strong>).
273 So, if an object is returned by the <code>ajaxProcessing</code> function, the data is stored in <code>table.config.pager.ajaxData</code>:
274 <ul>
275 <li>The object should contain attributes for <code>total</code> (numeric), <code>headers</code> (array) and <code>rows</code> (array of arrays).</li>
276 <li>A replacement <code>output</code> option can also be loaded via this method and must be included in the <code>output</code> attribute (i.e. <code>ajaxData.output</code>).</li>
277 <li>Additional attributes are also available to the output display by using the attribute key wrapped in curly brackets (e.g. <code>{extra}</code> from <code>ajaxData.extra</code>).</li>
278 <li>Additional attributes can also be objects or arrays and can be accessed via the output string as <code>{extra:0}</code> (for arrays) or <code>{extra:key}</code> for objects.</li>
279 <li>The page number is processed first, so it would be possible to use this string <code>{extra:{page}}</code> (<code>{page}</code> is a one-based index), or if you need a different value use <code>{page+1}</code> (zero-based index plus any number), or <code>{page-1}</code> (zero-based index minus any number).</li>
280 <li>For more details, please see <a href="https://github.com/Mottie/tablesorter/issues/326">issue #326</a>.</li>
281 </ul>
282 </li>
283 </ul>
284 <h3>ARRAY returned</h3>
285 <ul>
286 <li>The <code>ajaxProcessing</code> function can return the data in the following format <code>[ total, rows, headers (optional) ]</code>,
287 <br>or in version 2.9+ <code>[ rows, total, headers (optional) ]</code>,
288 <br>or in <span class="version">v2.10</span>, return a jQuery object within the array <code>[ total, $rows ]</code>, or just <code>[ total ]</code>:
289 <pre class="prettyprint lang-javascript">[
290 // total # rows contained in the database
291 80,
292 // row data: array of arrays; each internal array has the table cell data for that row
293 [
294 [ 1, "Kabul", "AFG", "Kabol", 1780000 ], // [ "row1cell1", "row1cell2", ... "row1cellN" ],
295 [ 2, "Qandahar", "AFG", "Qandahar", 237500 ], // [ "row2cell1", "row2cell2", ... "row2cellN" ],
296 ...
297 [ 25, "Haarlemmermeer", "NLD", "Noord-Holland", 110722 ] // [ "rowNcell1", "rowNcell2", ... "rowNcellN" ]
298 ],
299 [ "ID", "Name", "Country Code", "District", "Population" ] // [ "Header1", "Header2", ... "HeaderN" ] (optional)
300 ]</pre></li>
301 </ul>
302 </div>
303
304 </div>
305
306 <h1>Demo</h1>
307 Original Ajax url: <span id="origurl"></span><br>
308 Current Ajax url: <span id="url"></span>
309 <br>
310 <table class="tablesorter">
311 <thead>
312 <tr class="tablesorter-ignoreRow">
313 <td class="pager sorter-false" colspan="5">
314 <img src="../addons/pager/icons/first.png" class="first" alt="First" />
315 <img src="../addons/pager/icons/prev.png" class="prev" alt="Prev" />
316 <span class="pagedisplay"></span> <!-- this can be any element, including an input -->
317 <img src="../addons/pager/icons/next.png" class="next" alt="Next" />
318 <img src="../addons/pager/icons/last.png" class="last" alt="Last" />
319 <select class="pagesize">
320 <option value="25">25</option>
321 </select>
322 </td>
323 </tr>
324 <tr>
325 <th data-value="&gt;30">1</th>
326 <th>2</th>
327 <th>3</th>
328 <th>4</th>
329 <th>5</th>
330 </tr>
331 </thead>
332 <tfoot>
333 <tr>
334 <th>1</th>
335 <th>2</th>
336 <th>3</th>
337 <th>4</th>
338 <th>5</th>
339 </tr>
340 <tr>
341 <td class="pager" colspan="5">
342 <img src="../addons/pager/icons/first.png" class="first" alt="First" />
343 <img src="../addons/pager/icons/prev.png" class="prev" alt="Prev" />
344 <span class="pagedisplay"></span> <!-- this can be any element, including an input -->
345 <img src="../addons/pager/icons/next.png" class="next" alt="Next" />
346 <img src="../addons/pager/icons/last.png" class="last" alt="Last" />
347 <select class="pagesize">
348 <option value="25">25</option>
349 </select>
350 </td>
351 </tr>
352 </tfoot>
353 <tbody>
354 </tbody>
355 </table>
356
357 <h1>Javascript</h1>
358 <div id="javascript">
359 <pre class="prettyprint lang-javascript"></pre>
360 </div>
361
362 <h1>CSS</h1>
363 <div>
364 <pre class="prettyprint lang-css">/* pager wrapper, div */
365 .pager {
366 padding: 5px;
367 }
368 /* pager wrapper, in thead/tfoot */
369 td.pager {
370 background-color: #e6eeee;
371 }
372 /* pager navigation arrows */
373 .pager img {
374 vertical-align: middle;
375 margin-right: 2px;
376 }
377 /* pager output text */
378 .pager .pagedisplay {
379 font-size: 11px;
380 padding: 0 5px 0 5px;
381 width: 50px;
382 text-align: center;
383 }
384
385 /*** loading ajax indeterminate progress indicator ***/
386 #tablesorterPagerLoading {
387 background: rgba(255,255,255,0.8) url(icons/loading.gif) center center no-repeat;
388 position: absolute;
389 z-index: 1000;
390 }
391
392 /*** css used when "updateArrows" option is true ***/
393 /* the pager itself gets a disabled class when the number of rows is less than the size */
394 .pager.disabled {
395 display: none;
396 }
397 /* hide or fade out pager arrows when the first or last row is visible */
398 .pager img.disabled {
399 /* visibility: hidden */
400 opacity: 0.5;
401 filter: alpha(opacity=50);
402 }</pre>
403 </div>
404
405 <h1>HTML</h1>
406 <div id="html">
407 <pre class="prettyprint lang-html">&lt;table class=&quot;tablesorter&quot;&gt;
408 &lt;thead&gt;
409 &lt;tr class=&quot;tablesorter-ignoreRow&quot;&gt;
410 &lt;td class=&quot;pager&quot; colspan=&quot;5&quot;&gt;
411 &lt;img src=&quot;../addons/pager/icons/first.png&quot; class=&quot;first&quot;/&gt;
412 &lt;img src=&quot;../addons/pager/icons/prev.png&quot; class=&quot;prev&quot;/&gt;
413 &lt;span class=&quot;pagedisplay&quot;&gt;&lt;/span&gt; &lt;!-- this can be any element, including an input --&gt;
414 &lt;img src=&quot;../addons/pager/icons/next.png&quot; class=&quot;next&quot;/&gt;
415 &lt;img src=&quot;../addons/pager/icons/last.png&quot; class=&quot;last&quot;/&gt;
416 &lt;select class=&quot;pagesize&quot;&gt;
417 &lt;option value=&quot;25&quot;&gt;25&lt;/option&gt;
418 &lt;/select&gt;
419 &lt;/td&gt;
420 &lt;/tr&gt;
421 &lt;tr&gt;
422 &lt;th&gt;1 data-value=&quot;&amp;gt;30&quot;&lt;/th&gt; &lt;!-- thead text will be updated from the JSON; make sure the number of columns matches the JSON data --&gt;
423 &lt;th&gt;2&lt;/th&gt;
424 &lt;th&gt;3&lt;/th&gt;
425 &lt;th&gt;4&lt;/th&gt;
426 &lt;th&gt;5&lt;/th&gt;
427 &lt;/tr&gt;
428 &lt;/thead&gt;
429 &lt;tfoot&gt;
430 &lt;tr&gt;
431 &lt;th&gt;1&lt;/th&gt; &lt;!-- tfoot text will be updated at the same time as the thead --&gt;
432 &lt;th&gt;2&lt;/th&gt;
433 &lt;th&gt;3&lt;/th&gt;
434 &lt;th&gt;4&lt;/th&gt;
435 &lt;th&gt;5&lt;/th&gt;
436 &lt;/tr&gt;
437 &lt;tr&gt;
438 &lt;td class=&quot;pager&quot; colspan=&quot;5&quot;&gt;
439 &lt;img src=&quot;../addons/pager/icons/first.png&quot; class=&quot;first&quot;/&gt;
440 &lt;img src=&quot;../addons/pager/icons/prev.png&quot; class=&quot;prev&quot;/&gt;
441 &lt;span class=&quot;pagedisplay&quot;&gt;&lt;/span&gt; &lt;!-- this can be any element, including an input --&gt;
442 &lt;img src=&quot;../addons/pager/icons/next.png&quot; class=&quot;next&quot;/&gt;
443 &lt;img src=&quot;../addons/pager/icons/last.png&quot; class=&quot;last&quot;/&gt;
444 &lt;select class=&quot;pagesize&quot;&gt;
445 &lt;option value=&quot;25&quot;&gt;25&lt;/option&gt;
446 &lt;/select&gt;
447 &lt;/td&gt;
448 &lt;/tr&gt;
449 &lt;/tfoot&gt;
450 &lt;tbody&gt; &lt;!-- tbody will be loaded via JSON --&gt;
451 &lt;/tbody&gt;
452 &lt;/table&gt;</pre>
453
454 </div>
455
456 <div class="next-up">
457 <hr />
458 Next up: <a href="example-pager-filtered.html">Pager plugin + filter widget &rsaquo;&rsaquo;</a>
459 </div>
460
461 </div>
462
463 <script>
464 var $url = $('#url');
465 $('table')
466
467 // show current URL for the DEMO ONLY
468 .on('changingUrl', function(e, url) {
469 $url.html(url);
470 })
471
472 .on('pagerInitialized', function() {
473 // allow THIS demo to sort the content; this variable is automatically set to true when ajax
474 // is used as there isn't any way to sort the server side data from the client side.
475 this.config.serverSideSorting = false;
476 // show original highlighted URL
477 $('#origurl').html( this.config.pager.ajaxUrl.replace(/(\{.*?\})/g, '<span class="results">$1</span>') );
478 });
479 </script>
480
481 </body>
482 </html>