File: example-widget-pager-ajax.html

package info (click to toggle)
jquery-tablesorter 1%3A2.31.3%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,056 kB
  • sloc: javascript: 19,495; sh: 14; makefile: 8
file content (417 lines) | stat: -rw-r--r-- 15,595 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>jQuery tablesorter 2.0 - Pager Widget - Ajax</title>

	<!-- jQuery -->
	<script src="js/jquery-latest.min.js"></script>

	<!-- Demo stuff -->
	<link href="css/jq.css" rel="stylesheet">
	<link href="css/prettify.css" rel="stylesheet">
	<script src="js/prettify.js"></script>
	<script src="js/docs.js"></script>

	<!-- Tablesorter: required -->
	<link href="../css/theme.blue.css" rel="stylesheet">
	<script src="../js/jquery.tablesorter.js"></script>
	<script src="../js/jquery.tablesorter.widgets.js"></script>

	<!-- Tablesorter: optional -->
	<link rel="stylesheet" href="../addons/pager/jquery.tablesorter.pager.css">
	<script src="../js/widgets/widget-pager.js"></script>

	<!-- Tablesorter: optional -->
	<script src="../js/jquery.tablesorter.widgets.js"></script>

	<script id="js">$(function() {

	// Initialize tablesorter
	// ***********************
	$("table")
		.tablesorter({
			theme: 'blue',
			widthFixed: true,
			sortLocaleCompare: true, // needed for accented characters in the data
			sortList: [ [0,1] ],
			widgets: ['zebra', 'filter', 'pager'],
			widgetOptions: {

				// css class names of pager arrows
				pager_css: {
					container   : 'tablesorter-pager',
					errorRow    : 'tablesorter-errorRow', // error information row (don't include period at beginning)
					disabled    : 'disabled'              // class added to arrows @ extremes (i.e. prev/first arrows "disabled" on first page)
				},

				// jQuery selectors
				pager_selectors: {
					container   : '.pager',       // target the pager markup (wrapper)
					first       : '.first',       // go to first page arrow
					prev        : '.prev',        // previous page arrow
					next        : '.next',        // next page arrow
					last        : '.last',        // go to last page arrow
					gotoPage    : '.gotoPage',    // go to page selector - select dropdown that sets the current page
					pageDisplay : '.pagedisplay', // location of where the "output" is displayed
					pageSize    : '.pagesize'     // page size selector - select dropdown that sets the "size" option

				},

				// output default: '{page}/{totalPages}'
				// possible variables: {size}, {page}, {totalPages}, {filteredPages}, {startRow}, {endRow}, {filteredRows} and {totalRows}
				// also {page:input} & {startRow:input} will add a modifiable input in place of the value
				pager_output: '{startRow} - {endRow} / {filteredRows} ({totalRows})', //'{startRow} to {endRow} of {totalRows} rows', // '{page}/{totalPages}'

				// apply disabled classname to the pager arrows when the rows at either extreme is visible
				pager_updateArrows: true,

				// starting page of the pager (zero based index)
				pager_startPage: 0,

				// Reset pager to this page after filtering; set to desired page number
				// (zero-based index), or false to not change page at filter start
				pager_pageReset: 0,

				// Number of visible rows
				pager_size: 25,

				// f true, child rows will be counted towards the pager set size
				pager_countChildRows: false,

				// Save pager page & size if the storage script is loaded (requires $.tablesorter.storage in jquery.tablesorter.widgets.js)
				pager_savePages: true,

				// Saves tablesorter paging to custom key if defined. Key parameter name
				// used by the $.tablesorter.storage function. Useful if you have
				// multiple tables defined
				pager_storageKey: "tablesorter-pager",

				// if true, the table will remain the same height no matter how many records are displayed. The space is made up by an empty
				// table row set to a height to compensate; default is false
				pager_fixedHeight: false,

				// remove rows from the table to speed up the sort of large tables.
				// setting this to false, only hides the non-visible rows; needed if you plan to add/remove rows with the pager enabled.
				pager_removeRows: false, // removing rows in larger tables speeds up the sort

				// If you want to use ajaxUrl placeholders, here is an example:
				// ajaxUrl: "http:/mydatabase.com?page={page}&size={size}&{sortList:col}"
				// where {page} is replaced by the page number (or use {page+1} to get a one-based index),
				// {size} is replaced by the number of records to show,
				// {sortList:col} adds the sortList to the url into a "col" array, and {filterList:fcol} adds
				// the filterList to the url into an "fcol" array.
				// So a sortList = [[2,0],[3,0]] becomes "&col[2]=0&col[3]=0" in the url
				// and a filterList = [[2,Blue],[3,13]] becomes "&fcol[2]=Blue&fcol[3]=13" in the url
				pager_ajaxUrl: 'assets/City{page}.json?{filterList:filter}&{sortList:column}',

				// use this option to manipulate and/or add additional parameters to the ajax url
				pager_customAjaxUrl:  function(table, url) {
						// manipulate the url string as you desire
						// url += '&currPage=' + window.location.pathname;
						// trigger my custom event
						$(table).trigger('changingUrl', url);
						// send the server the current page
						return url;
				},

				// ajax error callback from $.tablesorter.showError function
				// pager_ajaxError: function( config, xhr, settings, exception ) { return exception; };
				// returning false will abort the error message
				pager_ajaxError: null,

				// modify the $.ajax object to allow complete control over your ajax requests
				pager_ajaxObject: {
					type: 'GET', // default setting
					dataType: 'json'
				},

				// Set this option to false if your table data is preloaded into the
				// table, but you are still using ajax
				pager_processAjaxOnInit: true,

				// When processAjaxOnInit is set to false, set this option to contain
				// the total number of rows and filtered rows to prevent an initial ajax
				// call
				initialRows: {
					total: 100,
					filtered: 100
				},

				// 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
				// ]
				pager_ajaxProcessing: function(data) {
					if (data && data.hasOwnProperty('rows')) {
						var indx, r, row, c, d = data.rows,
						// total number of rows (required)
						total = data.total_rows,
						// array of header names (optional)
						headers = data.headers,
						// cross-reference to match JSON key within data (no spaces)
						headerXref = headers.join(',').replace(/\s+/g,'').split(','),
						// all rows: array of arrays; each internal array has the table cell data for that row
						rows = [],
						// len should match pager set size (c.size)
						len = d.length;
						// this will depend on how the json is set up - see City0.json
						// rows
						for ( r=0; r < len; r++ ) {
							row = []; // new row array
							// cells
							for ( c in d[r] ) {
								if (typeof(c) === "string") {
									// match the key with the header to get the proper column index
									indx = $.inArray( c, headerXref );
									// add each table cell data to row array
									if (indx >= 0) {
										row[indx] = d[r][c];
									}
								}
							}
							rows.push(row); // add new row array to rows array
						}
						// in version 2.10, you can optionally return $(rows) a set of table rows within a jQuery object
						return [ total, rows, headers ];
					}
				}
			}

		});
});</script>

</head>
<body id="pager-demo">
	<div id="banner">
		<h1>table<em>sorter</em></h1>
		<h2>Pager Widget - Ajax</h2>
		<h3>Flexible client-side table sorting</h3>
		<a href="index.html">Back to documentation</a>
	</div>

	<div id="main">

	<p class="tip">
		<em>NOTE!</em>
	</p>
	<ul>
		<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.
			<br><br>
		</li>

		<li>This pager WIDGET <em>can not</em> be applied to the original tablesorter.</li>
		<li>Do not use this widget along with the pager plugin.</li>
		<li>The pager.css file also works with this pager widget.</li>
		<li>This widget is still in <span class="beta">development</span> as it has not been throughly tested.</li>

		<li>Extensive documentation has not been included, as all functioning is essentially identical to the pager addon, but here are some important differences:
			<ul>
				<li>All of the options are now set within the <code>widgetOptions</code>.</li>
				<li>Most option names have only been modified by adding <code>pager_</code> as a prefix.</li>
				<li>Exceptions include moving all applied css class names into a <code>pager_css</code> option and all selectors into <code>pager_selectors</code>, including the original <code>container</code> option.</li>
				<li>See the Javascript code below for a full example of how to use this widget with <strong>ajax</strong>.</li>
			</ul>
		</li>
		<li>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>
	</ul>

	<h1>Demo</h1>
Original Ajax url: <span id="origurl"></span><br>
Current Ajax url: <span id="url"></span>
<br>
<table class="tablesorter">
	<thead>
		<tr class="tablesorter-ignoreRow">
			<td class="pager sorter-false" colspan="5">
				<img src="../addons/pager/icons/first.png" class="first" alt="First" />
				<img src="../addons/pager/icons/prev.png" class="prev" alt="Prev" />
				<span class="pagedisplay"></span> <!-- this can be any element, including an input -->
				<img src="../addons/pager/icons/next.png" class="next" alt="Next" />
				<img src="../addons/pager/icons/last.png" class="last" alt="Last" />
				<select class="pagesize">
					<option value="25">25</option>
				</select>
			</td>
		</tr>
		<tr>
			<th data-value=">30">1</th>
			<th>2</th>
			<th>3</th>
			<th>4</th>
			<th>5</th>
		</tr>
	</thead>
	<tfoot>
		<tr>
			<th>1</th>
			<th>2</th>
			<th>3</th>
			<th>4</th>
			<th>5</th>
		</tr>
		<tr>
			<td class="pager" colspan="5">
				<img src="../addons/pager/icons/first.png" class="first" alt="First" />
				<img src="../addons/pager/icons/prev.png" class="prev" alt="Prev" />
				<span class="pagedisplay"></span> <!-- this can be any element, including an input -->
				<img src="../addons/pager/icons/next.png" class="next" alt="Next" />
				<img src="../addons/pager/icons/last.png" class="last" alt="Last" />
				<select class="pagesize">
					<option value="25">25</option>
				</select>
			</td>
		</tr>
	</tfoot>
	<tbody>
	</tbody>
</table>

	<h1>Javascript</h1>
	<div id="javascript">
		<pre class="prettyprint lang-javascript"></pre>
	</div>

	<h1>CSS</h1>
	<div>
		<pre class="prettyprint lang-css">/* pager wrapper, div */
.pager {
  padding: 5px;
}
/* pager wrapper, in thead/tfoot */
td.pager {
  background-color: #e6eeee;
}
/* pager navigation arrows */
.pager img {
  vertical-align: middle;
  margin-right: 2px;
}
/* pager output text */
.pager .pagedisplay {
  font-size: 11px;
  padding: 0 5px 0 5px;
  width: 50px;
  text-align: center;
}

/*** loading ajax indeterminate progress indicator ***/
#tablesorterPagerLoading {
  background: rgba(255,255,255,0.8) url(icons/loading.gif) center center no-repeat;
  position: absolute;
  z-index: 1000;
}

/*** css used when "updateArrows" option is true ***/
/* the pager itself gets a disabled class when the number of rows is less than the size */
.pager.disabled {
  display: none;
}
/* hide or fade out pager arrows when the first or last row is visible */
.pager img.disabled {
  /* visibility: hidden */
  opacity: 0.5;
  filter: alpha(opacity=50);
}</pre>
	</div>

	<h1>HTML</h1>
	<div id="html">
		<pre class="prettyprint lang-html">&lt;!-- jQuery --&gt;
&lt;script src=&quot;js/jquery-latest.min.js&quot;&gt;&lt;/script&gt;

&lt;!-- Tablesorter: required --&gt;
&lt;link href=&quot;css/theme.blue.css&quot; rel=&quot;stylesheet&quot;&gt;
&lt;script src=&quot;js/jquery.tablesorter.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;js/jquery.tablesorter.widgets.js&quot;&gt;&lt;/script&gt;

&lt;!-- Tablesorter: pager widget --&gt;
&lt;link href=&quot;css/jquery.tablesorter.pager.css&quot; rel=&quot;stylesheet&quot;&gt;
&lt;script src=&quot;js/widget-pager.js&quot;&gt;&lt;/script&gt;

&lt;table class=&quot;tablesorter&quot;&gt;
  &lt;thead&gt;
    &lt;tr class=&quot;tablesorter-ignoreRow&quot;&gt;
      &lt;td class=&quot;pager&quot; colspan=&quot;5&quot;&gt;
        &lt;img src=&quot;../addons/pager/icons/first.png&quot; class=&quot;first&quot;/&gt;
        &lt;img src=&quot;../addons/pager/icons/prev.png&quot; class=&quot;prev&quot;/&gt;
        &lt;span class=&quot;pagedisplay&quot;&gt;&lt;/span&gt; &lt;!-- this can be any element, including an input --&gt;
        &lt;img src=&quot;../addons/pager/icons/next.png&quot; class=&quot;next&quot;/&gt;
        &lt;img src=&quot;../addons/pager/icons/last.png&quot; class=&quot;last&quot;/&gt;
        &lt;select class=&quot;pagesize&quot;&gt;
          &lt;option value=&quot;25&quot;&gt;25&lt;/option&gt;
        &lt;/select&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;1&lt;/th&gt; &lt;!-- thead text will be updated from the JSON; make sure the number of columns matches the JSON data --&gt;
      &lt;th&gt;2&lt;/th&gt;
      &lt;th&gt;3&lt;/th&gt;
      &lt;th&gt;4&lt;/th&gt;
      &lt;th&gt;5&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tfoot&gt;
    &lt;tr&gt;
      &lt;th&gt;1&lt;/th&gt; &lt;!-- tfoot text will be updated at the same time as the thead --&gt;
      &lt;th&gt;2&lt;/th&gt;
      &lt;th&gt;3&lt;/th&gt;
      &lt;th&gt;4&lt;/th&gt;
      &lt;th&gt;5&lt;/th&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td class=&quot;pager&quot; colspan=&quot;5&quot;&gt;
        &lt;img src=&quot;../addons/pager/icons/first.png&quot; class=&quot;first&quot;/&gt;
        &lt;img src=&quot;../addons/pager/icons/prev.png&quot; class=&quot;prev&quot;/&gt;
        &lt;span class=&quot;pagedisplay&quot;&gt;&lt;/span&gt; &lt;!-- this can be any element, including an input --&gt;
        &lt;img src=&quot;../addons/pager/icons/next.png&quot; class=&quot;next&quot;/&gt;
        &lt;img src=&quot;../addons/pager/icons/last.png&quot; class=&quot;last&quot;/&gt;
        &lt;select class=&quot;pagesize&quot;&gt;
          &lt;option value=&quot;25&quot;&gt;25&lt;/option&gt;
        &lt;/select&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tfoot&gt;
  &lt;tbody&gt; &lt;!-- tbody will be loaded via JSON --&gt;
  &lt;/tbody&gt;
&lt;/table&gt;</pre>

		</div>

<div class="next-up">
	<hr />
	Next up: <a href="example-pager-filtered.html">Pager plugin + filter widget &rsaquo;&rsaquo;</a>
</div>

</div>

<script>
var $url = $('#url');
$('table')

	// show current URL for the DEMO ONLY
	.on('changingUrl', function(e, url) {
		$url.html(url);
	})

	.on('pagerInitialized', function() {
		// allow THIS demo to sort the content; this variable is automatically set to true when ajax
		// is used as there isn't any way to sort the server side data from the client side.
		this.config.serverSideSorting = false;
		// show original highlighted URL
		$('#origurl').html( this.config.widgetOptions.pager_ajaxUrl.replace(/(\{.*?\})/g, '<span class="results">$1</span>') );
	});
</script>

</body>
</html>