| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<meta charset="utf-8"> |
| 5 |
<title>jQuery tablesorter 2.0 - Pager Widget - Ajax</title> |
| 6 |
|
| 7 |
|
| 8 |
<script src="js/jquery-latest.min.js"></script> |
| 9 |
|
| 10 |
|
| 11 |
<link href="css/jq.css" rel="stylesheet"> |
| 12 |
<link href="css/prettify.css" rel="stylesheet"> |
| 13 |
<script src="js/prettify.js"></script> |
| 14 |
<script src="js/docs.js"></script> |
| 15 |
|
| 16 |
|
| 17 |
<link href="../css/theme.blue.css" rel="stylesheet"> |
| 18 |
<script src="../js/jquery.tablesorter.js"></script> |
| 19 |
<script src="../js/jquery.tablesorter.widgets.js"></script> |
| 20 |
|
| 21 |
|
| 22 |
<link rel="stylesheet" href="../addons/pager/jquery.tablesorter.pager.css"> |
| 23 |
<script src="../js/widgets/widget-pager.js"></script> |
| 24 |
|
| 25 |
|
| 26 |
<script src="../js/jquery.tablesorter.widgets.js"></script> |
| 27 |
|
| 28 |
<script id="js">$(function() { |
| 29 |
|
| 30 |
// Initialize tablesorter |
| 31 |
// *********************** |
| 32 |
$("table") |
| 33 |
.tablesorter({ |
| 34 |
theme: 'blue', |
| 35 |
widthFixed: true, |
| 36 |
sortLocaleCompare: true, // needed for accented characters in the data |
| 37 |
sortList: [ [0,1] ], |
| 38 |
widgets: ['zebra', 'filter', 'pager'], |
| 39 |
widgetOptions: { |
| 40 |
|
| 41 |
// css class names of pager arrows |
| 42 |
pager_css: { |
| 43 |
container : 'tablesorter-pager', |
| 44 |
errorRow : 'tablesorter-errorRow', // error information row (don't include period at beginning) |
| 45 |
disabled : 'disabled' // class added to arrows @ extremes (i.e. prev/first arrows "disabled" on first page) |
| 46 |
}, |
| 47 |
|
| 48 |
// jQuery selectors |
| 49 |
pager_selectors: { |
| 50 |
container : '.pager', // target the pager markup (wrapper) |
| 51 |
first : '.first', // go to first page arrow |
| 52 |
prev : '.prev', // previous page arrow |
| 53 |
next : '.next', // next page arrow |
| 54 |
last : '.last', // go to last page arrow |
| 55 |
gotoPage : '.gotoPage', // go to page selector - select dropdown that sets the current page |
| 56 |
pageDisplay : '.pagedisplay', // location of where the "output" is displayed |
| 57 |
pageSize : '.pagesize' // page size selector - select dropdown that sets the "size" option |
| 58 |
|
| 59 |
}, |
| 60 |
|
| 61 |
// output default: '{page}/{totalPages}' |
| 62 |
// possible variables: {size}, {page}, {totalPages}, {filteredPages}, {startRow}, {endRow}, {filteredRows} and {totalRows} |
| 63 |
// also {page:input} & {startRow:input} will add a modifiable input in place of the value |
| 64 |
pager_output: '{startRow} - {endRow} / {filteredRows} ({totalRows})', //'{startRow} to {endRow} of {totalRows} rows', // '{page}/{totalPages}' |
| 65 |
|
| 66 |
// apply disabled classname to the pager arrows when the rows at either extreme is visible |
| 67 |
pager_updateArrows: true, |
| 68 |
|
| 69 |
// starting page of the pager (zero based index) |
| 70 |
pager_startPage: 0, |
| 71 |
|
| 72 |
// Reset pager to this page after filtering; set to desired page number |
| 73 |
// (zero-based index), or false to not change page at filter start |
| 74 |
pager_pageReset: 0, |
| 75 |
|
| 76 |
// Number of visible rows |
| 77 |
pager_size: 25, |
| 78 |
|
| 79 |
// f true, child rows will be counted towards the pager set size |
| 80 |
pager_countChildRows: false, |
| 81 |
|
| 82 |
// Save pager page & size if the storage script is loaded (requires $.tablesorter.storage in jquery.tablesorter.widgets.js) |
| 83 |
pager_savePages: true, |
| 84 |
|
| 85 |
// Saves tablesorter paging to custom key if defined. Key parameter name |
| 86 |
// used by the $.tablesorter.storage function. Useful if you have |
| 87 |
// multiple tables defined |
| 88 |
pager_storageKey: "tablesorter-pager", |
| 89 |
|
| 90 |
// if true, the table will remain the same height no matter how many records are displayed. The space is made up by an empty |
| 91 |
// table row set to a height to compensate; default is false |
| 92 |
pager_fixedHeight: false, |
| 93 |
|
| 94 |
// remove rows from the table to speed up the sort of large tables. |
| 95 |
// setting this to false, only hides the non-visible rows; needed if you plan to add/remove rows with the pager enabled. |
| 96 |
pager_removeRows: false, // removing rows in larger tables speeds up the sort |
| 97 |
|
| 98 |
// If you want to use ajaxUrl placeholders, here is an example: |
| 99 |
// ajaxUrl: "http:/mydatabase.com?page={page}&size={size}&{sortList:col}" |
| 100 |
// where {page} is replaced by the page number (or use {page+1} to get a one-based index), |
| 101 |
// {size} is replaced by the number of records to show, |
| 102 |
// {sortList:col} adds the sortList to the url into a "col" array, and {filterList:fcol} adds |
| 103 |
// the filterList to the url into an "fcol" array. |
| 104 |
// So a sortList = [[2,0],[3,0]] becomes "&col[2]=0&col[3]=0" in the url |
| 105 |
// and a filterList = [[2,Blue],[3,13]] becomes "&fcol[2]=Blue&fcol[3]=13" in the url |
| 106 |
pager_ajaxUrl: 'assets/City{page}.json?{filterList:filter}&{sortList:column}', |
| 107 |
|
| 108 |
// use this option to manipulate and/or add additional parameters to the ajax url |
| 109 |
pager_customAjaxUrl: function(table, url) { |
| 110 |
// manipulate the url string as you desire |
| 111 |
// url += '&currPage=' + window.location.pathname; |
| 112 |
// trigger my custom event |
| 113 |
$(table).trigger('changingUrl', url); |
| 114 |
// send the server the current page |
| 115 |
return url; |
| 116 |
}, |
| 117 |
|
| 118 |
// ajax error callback from $.tablesorter.showError function |
| 119 |
// pager_ajaxError: function( config, xhr, settings, exception ) { return exception; }; |
| 120 |
// returning false will abort the error message |
| 121 |
pager_ajaxError: null, |
| 122 |
|
| 123 |
// modify the $.ajax object to allow complete control over your ajax requests |
| 124 |
pager_ajaxObject: { |
| 125 |
type: 'GET', // default setting |
| 126 |
dataType: 'json' |
| 127 |
}, |
| 128 |
|
| 129 |
// Set this option to false if your table data is preloaded into the |
| 130 |
// table, but you are still using ajax |
| 131 |
pager_processAjaxOnInit: true, |
| 132 |
|
| 133 |
// When processAjaxOnInit is set to false, set this option to contain |
| 134 |
// the total number of rows and filtered rows to prevent an initial ajax |
| 135 |
// call |
| 136 |
initialRows: { |
| 137 |
total: 100, |
| 138 |
filtered: 100 |
| 139 |
}, |
| 140 |
|
| 141 |
// process ajax so that the following information is returned: |
| 142 |
// [ total_rows (number), rows (array of arrays), headers (array; optional) ] |
| 143 |
// example: |
| 144 |
// [ |
| 145 |
// 100, // total rows |
| 146 |
// [ |
| 147 |
// [ "row1cell1", "row1cell2", ... "row1cellN" ], |
| 148 |
// [ "row2cell1", "row2cell2", ... "row2cellN" ], |
| 149 |
// ... |
| 150 |
// [ "rowNcell1", "rowNcell2", ... "rowNcellN" ] |
| 151 |
// ], |
| 152 |
// [ "header1", "header2", ... "headerN" ] // optional |
| 153 |
// ] |
| 154 |
pager_ajaxProcessing: function(data) { |
| 155 |
if (data && data.hasOwnProperty('rows')) { |
| 156 |
var indx, r, row, c, d = data.rows, |
| 157 |
// total number of rows (required) |
| 158 |
total = data.total_rows, |
| 159 |
// array of header names (optional) |
| 160 |
headers = data.headers, |
| 161 |
// cross-reference to match JSON key within data (no spaces) |
| 162 |
headerXref = headers.join(',').replace(/\s+/g,'').split(','), |
| 163 |
// all rows: array of arrays; each internal array has the table cell data for that row |
| 164 |
rows = [], |
| 165 |
// len should match pager set size (c.size) |
| 166 |
len = d.length; |
| 167 |
// this will depend on how the json is set up - see City0.json |
| 168 |
// rows |
| 169 |
for ( r=0; r < len; r++ ) { |
| 170 |
row = []; // new row array |
| 171 |
// cells |
| 172 |
for ( c in d[r] ) { |
| 173 |
if (typeof(c) === "string") { |
| 174 |
// match the key with the header to get the proper column index |
| 175 |
indx = $.inArray( c, headerXref ); |
| 176 |
// add each table cell data to row array |
| 177 |
if (indx >= 0) { |
| 178 |
row[indx] = d[r][c]; |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
rows.push(row); // add new row array to rows array |
| 183 |
} |
| 184 |
// in version 2.10, you can optionally return $(rows) a set of table rows within a jQuery object |
| 185 |
return [ total, rows, headers ]; |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
}); |
| 191 |
});</script> |
| 192 |
|
| 193 |
</head> |
| 194 |
<body id="pager-demo"> |
| 195 |
<div id="banner"> |
| 196 |
<h1>table<em>sorter</em></h1> |
| 197 |
<h2>Pager Widget - Ajax</h2> |
| 198 |
<h3>Flexible client-side table sorting</h3> |
| 199 |
<a href="index.html">Back to documentation</a> |
| 200 |
</div> |
| 201 |
|
| 202 |
<div id="main"> |
| 203 |
|
| 204 |
<p class="tip"> |
| 205 |
<em>NOTE!</em> |
| 206 |
</p> |
| 207 |
<ul> |
| 208 |
<li> |
| 209 |
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. |
| 210 |
<br><br> |
| 211 |
</li> |
| 212 |
|
| 213 |
<li>This pager WIDGET <em>can not</em> be applied to the original tablesorter.</li> |
| 214 |
<li>Do not use this widget along with the pager plugin.</li> |
| 215 |
<li>The pager.css file also works with this pager widget.</li> |
| 216 |
<li>This widget is still in <span class="beta">development</span> as it has not been throughly tested.</li> |
| 217 |
|
| 218 |
<li>Extensive documentation has not been included, as all functioning is essentially identical to the pager addon, but here are some important differences: |
| 219 |
<ul> |
| 220 |
<li>All of the options are now set within the <code>widgetOptions</code>.</li> |
| 221 |
<li>Most option names have only been modified by adding <code>pager_</code> as a prefix.</li> |
| 222 |
<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> |
| 223 |
<li>See the Javascript code below for a full example of how to use this widget with <strong>ajax</strong>.</li> |
| 224 |
</ul> |
| 225 |
</li> |
| 226 |
<li>The "ID" column has a default filter setting of ">30" and a descending sort, but neither is applied as this demo is not connected a server (just a basic JSON file).</li> |
| 227 |
</ul> |
| 228 |
|
| 229 |
<h1>Demo</h1> |
| 230 |
Original Ajax url: <span id="origurl"></span><br> |
| 231 |
Current Ajax url: <span id="url"></span> |
| 232 |
<br> |
| 233 |
<table class="tablesorter"> |
| 234 |
<thead> |
| 235 |
<tr class="tablesorter-ignoreRow"> |
| 236 |
<td class="pager sorter-false" colspan="5"> |
| 237 |
<img src="../addons/pager/icons/first.png" class="first" alt="First" /> |
| 238 |
<img src="../addons/pager/icons/prev.png" class="prev" alt="Prev" /> |
| 239 |
<span class="pagedisplay"></span> |
| 240 |
<img src="../addons/pager/icons/next.png" class="next" alt="Next" /> |
| 241 |
<img src="../addons/pager/icons/last.png" class="last" alt="Last" /> |
| 242 |
<select class="pagesize"> |
| 243 |
<option value="25">25</option> |
| 244 |
</select> |
| 245 |
</td> |
| 246 |
</tr> |
| 247 |
<tr> |
| 248 |
<th data-value=">30">1</th> |
| 249 |
<th>2</th> |
| 250 |
<th>3</th> |
| 251 |
<th>4</th> |
| 252 |
<th>5</th> |
| 253 |
</tr> |
| 254 |
</thead> |
| 255 |
<tfoot> |
| 256 |
<tr> |
| 257 |
<th>1</th> |
| 258 |
<th>2</th> |
| 259 |
<th>3</th> |
| 260 |
<th>4</th> |
| 261 |
<th>5</th> |
| 262 |
</tr> |
| 263 |
<tr> |
| 264 |
<td class="pager" colspan="5"> |
| 265 |
<img src="../addons/pager/icons/first.png" class="first" alt="First" /> |
| 266 |
<img src="../addons/pager/icons/prev.png" class="prev" alt="Prev" /> |
| 267 |
<span class="pagedisplay"></span> |
| 268 |
<img src="../addons/pager/icons/next.png" class="next" alt="Next" /> |
| 269 |
<img src="../addons/pager/icons/last.png" class="last" alt="Last" /> |
| 270 |
<select class="pagesize"> |
| 271 |
<option value="25">25</option> |
| 272 |
</select> |
| 273 |
</td> |
| 274 |
</tr> |
| 275 |
</tfoot> |
| 276 |
<tbody> |
| 277 |
</tbody> |
| 278 |
</table> |
| 279 |
|
| 280 |
<h1>Javascript</h1> |
| 281 |
<div id="javascript"> |
| 282 |
<pre class="prettyprint lang-javascript"></pre> |
| 283 |
</div> |
| 284 |
|
| 285 |
<h1>CSS</h1> |
| 286 |
<div> |
| 287 |
<pre class="prettyprint lang-css">/* pager wrapper, div */ |
| 288 |
.pager { |
| 289 |
padding: 5px; |
| 290 |
} |
| 291 |
/* pager wrapper, in thead/tfoot */ |
| 292 |
td.pager { |
| 293 |
background-color: #e6eeee; |
| 294 |
} |
| 295 |
/* pager navigation arrows */ |
| 296 |
.pager img { |
| 297 |
vertical-align: middle; |
| 298 |
margin-right: 2px; |
| 299 |
} |
| 300 |
/* pager output text */ |
| 301 |
.pager .pagedisplay { |
| 302 |
font-size: 11px; |
| 303 |
padding: 0 5px 0 5px; |
| 304 |
width: 50px; |
| 305 |
text-align: center; |
| 306 |
} |
| 307 |
|
| 308 |
/*** loading ajax indeterminate progress indicator ***/ |
| 309 |
#tablesorterPagerLoading { |
| 310 |
background: rgba(255,255,255,0.8) url(icons/loading.gif) center center no-repeat; |
| 311 |
position: absolute; |
| 312 |
z-index: 1000; |
| 313 |
} |
| 314 |
|
| 315 |
/*** css used when "updateArrows" option is true ***/ |
| 316 |
/* the pager itself gets a disabled class when the number of rows is less than the size */ |
| 317 |
.pager.disabled { |
| 318 |
display: none; |
| 319 |
} |
| 320 |
/* hide or fade out pager arrows when the first or last row is visible */ |
| 321 |
.pager img.disabled { |
| 322 |
/* visibility: hidden */ |
| 323 |
opacity: 0.5; |
| 324 |
filter: alpha(opacity=50); |
| 325 |
}</pre> |
| 326 |
</div> |
| 327 |
|
| 328 |
<h1>HTML</h1> |
| 329 |
<div id="html"> |
| 330 |
<pre class="prettyprint lang-html"><!-- jQuery --> |
| 331 |
<script src="js/jquery-latest.min.js"></script> |
| 332 |
|
| 333 |
<!-- Tablesorter: required --> |
| 334 |
<link href="css/theme.blue.css" rel="stylesheet"> |
| 335 |
<script src="js/jquery.tablesorter.js"></script> |
| 336 |
<script src="js/jquery.tablesorter.widgets.js"></script> |
| 337 |
|
| 338 |
<!-- Tablesorter: pager widget --> |
| 339 |
<link href="css/jquery.tablesorter.pager.css" rel="stylesheet"> |
| 340 |
<script src="js/widget-pager.js"></script> |
| 341 |
|
| 342 |
<table class="tablesorter"> |
| 343 |
<thead> |
| 344 |
<tr class="tablesorter-ignoreRow"> |
| 345 |
<td class="pager" colspan="5"> |
| 346 |
<img src="../addons/pager/icons/first.png" class="first"/> |
| 347 |
<img src="../addons/pager/icons/prev.png" class="prev"/> |
| 348 |
<span class="pagedisplay"></span> <!-- this can be any element, including an input --> |
| 349 |
<img src="../addons/pager/icons/next.png" class="next"/> |
| 350 |
<img src="../addons/pager/icons/last.png" class="last"/> |
| 351 |
<select class="pagesize"> |
| 352 |
<option value="25">25</option> |
| 353 |
</select> |
| 354 |
</td> |
| 355 |
</tr> |
| 356 |
<tr> |
| 357 |
<th>1</th> <!-- thead text will be updated from the JSON; make sure the number of columns matches the JSON data --> |
| 358 |
<th>2</th> |
| 359 |
<th>3</th> |
| 360 |
<th>4</th> |
| 361 |
<th>5</th> |
| 362 |
</tr> |
| 363 |
</thead> |
| 364 |
<tfoot> |
| 365 |
<tr> |
| 366 |
<th>1</th> <!-- tfoot text will be updated at the same time as the thead --> |
| 367 |
<th>2</th> |
| 368 |
<th>3</th> |
| 369 |
<th>4</th> |
| 370 |
<th>5</th> |
| 371 |
</tr> |
| 372 |
<tr> |
| 373 |
<td class="pager" colspan="5"> |
| 374 |
<img src="../addons/pager/icons/first.png" class="first"/> |
| 375 |
<img src="../addons/pager/icons/prev.png" class="prev"/> |
| 376 |
<span class="pagedisplay"></span> <!-- this can be any element, including an input --> |
| 377 |
<img src="../addons/pager/icons/next.png" class="next"/> |
| 378 |
<img src="../addons/pager/icons/last.png" class="last"/> |
| 379 |
<select class="pagesize"> |
| 380 |
<option value="25">25</option> |
| 381 |
</select> |
| 382 |
</td> |
| 383 |
</tr> |
| 384 |
</tfoot> |
| 385 |
<tbody> <!-- tbody will be loaded via JSON --> |
| 386 |
</tbody> |
| 387 |
</table></pre> |
| 388 |
|
| 389 |
</div> |
| 390 |
|
| 391 |
<div class="next-up"> |
| 392 |
<hr /> |
| 393 |
Next up: <a href="example-pager-filtered.html">Pager plugin + filter widget ››</a> |
| 394 |
</div> |
| 395 |
|
| 396 |
</div> |
| 397 |
|
| 398 |
<script> |
| 399 |
var $url = $('#url'); |
| 400 |
$('table') |
| 401 |
|
| 402 |
// show current URL for the DEMO ONLY |
| 403 |
.on('changingUrl', function(e, url) { |
| 404 |
$url.html(url); |
| 405 |
}) |
| 406 |
|
| 407 |
.on('pagerInitialized', function() { |
| 408 |
// allow THIS demo to sort the content; this variable is automatically set to true when ajax |
| 409 |
// is used as there isn't any way to sort the server side data from the client side. |
| 410 |
this.config.serverSideSorting = false; |
| 411 |
// show original highlighted URL |
| 412 |
$('#origurl').html( this.config.widgetOptions.pager_ajaxUrl.replace(/(\{.*?\})/g, '<span class="results">$1</span>') ); |
| 413 |
}); |
| 414 |
</script> |
| 415 |
|
| 416 |
</body> |
| 417 |
</html> |