mirror of
https://github.com/Mottie/tablesorter.git
synced 2024-11-15 23:54:22 +00:00
Fixed initial filter settings when using pager ajax #388 & ajax pager not recognizing column sorting #408
This commit is contained in:
parent
8888879582
commit
e6696b0a1e
@ -1,6 +1,6 @@
|
||||
/*!
|
||||
* tablesorter pager plugin
|
||||
* updated 10/30/2013
|
||||
* updated 11/8/2013
|
||||
*/
|
||||
/*jshint browser:true, jquery:true, unused:false */
|
||||
;(function($) {
|
||||
@ -236,6 +236,9 @@
|
||||
$t.find('thead tr.' + p.cssErrorRow).remove(); // Clean up any previous error.
|
||||
|
||||
if ( exception ) {
|
||||
if (c.debug) {
|
||||
ts.log('Ajax Error', xhr, exception);
|
||||
}
|
||||
$err = $('<tr class="' + p.cssErrorRow + '"><td style="text-align:center;" colspan="' + hl + '">' + (
|
||||
xhr.status === 0 ? 'Not connected, verify Network' :
|
||||
xhr.status === 404 ? 'Requested page not found [404]' :
|
||||
@ -272,15 +275,13 @@
|
||||
c.$tbodies.eq(0).empty().append(d);
|
||||
} else if (l) {
|
||||
// build table from array
|
||||
if ( l > 0 ) {
|
||||
for ( i = 0; i < l; i++ ) {
|
||||
tds += '<tr>';
|
||||
for ( j = 0; j < d[i].length; j++ ) {
|
||||
// build tbody cells
|
||||
tds += '<td>' + d[i][j] + '</td>';
|
||||
}
|
||||
tds += '</tr>';
|
||||
for ( i = 0; i < l; i++ ) {
|
||||
tds += '<tr>';
|
||||
for ( j = 0; j < d[i].length; j++ ) {
|
||||
// build tbody cells
|
||||
tds += '<td>' + d[i][j] + '</td>';
|
||||
}
|
||||
tds += '</tr>';
|
||||
}
|
||||
// add rows to first tbody
|
||||
c.$tbodies.eq(0).html( tds );
|
||||
@ -314,9 +315,15 @@
|
||||
if (c.showProcessing) {
|
||||
ts.isProcessing(table); // remove loading icon
|
||||
}
|
||||
p.totalPages = Math.ceil( p.totalRows / ( p.size || 10 ) );
|
||||
// make sure last pager settings are saved, prevents multiple server side calls with
|
||||
// the same parameters
|
||||
p.last.totalPages = p.totalPages = Math.ceil( p.totalRows / ( p.size || 10 ) );
|
||||
p.last.currentFilters = p.currentFilters;
|
||||
p.last.sortList = (c.sortList || []).join(',');
|
||||
updatePageDisplay(table, p);
|
||||
fixHeight(table, p);
|
||||
// apply widgets after table has rendered
|
||||
$t.trigger('applyWidgets');
|
||||
if (p.initialized) {
|
||||
$t.trigger('pagerChange', p);
|
||||
$t.trigger('updateComplete');
|
||||
@ -350,17 +357,21 @@
|
||||
p.oldAjaxSuccess(data);
|
||||
}
|
||||
};
|
||||
if (c.debug) {
|
||||
ts.log('ajax initialized', p.ajaxObject);
|
||||
}
|
||||
$.ajax(p.ajaxObject);
|
||||
}
|
||||
},
|
||||
|
||||
getAjaxUrl = function(table, p) {
|
||||
var url = (p.ajaxUrl) ? p.ajaxUrl
|
||||
var c = table.config,
|
||||
url = (p.ajaxUrl) ? p.ajaxUrl
|
||||
// allow using "{page+1}" in the url string to switch to a non-zero based index
|
||||
.replace(/\{page([\-+]\d+)?\}/, function(s,n){ return p.page + (n ? parseInt(n, 10) : 0); })
|
||||
.replace(/\{size\}/g, p.size) : '',
|
||||
sl = table.config.sortList,
|
||||
fl = p.currentFilters || [],
|
||||
sl = c.sortList,
|
||||
fl = p.currentFilters || $(table).data('lastSearch') || [],
|
||||
sortCol = url.match(/\{\s*sort(?:List)?\s*:\s*(\w*)\s*\}/),
|
||||
filterCol = url.match(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/),
|
||||
arry = [];
|
||||
@ -382,10 +393,14 @@
|
||||
});
|
||||
// if the arry is empty, just add the fcol parameter... "&{filterList:fcol}" becomes "&fcol"
|
||||
url = url.replace(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/g, arry.length ? arry.join('&') : filterCol );
|
||||
p.currentFilters = fl;
|
||||
}
|
||||
if ( typeof(p.customAjaxUrl) === "function" ) {
|
||||
url = p.customAjaxUrl(table, url);
|
||||
}
|
||||
if (c.debug) {
|
||||
ts.log('Pager ajax url: ' + url);
|
||||
}
|
||||
return url;
|
||||
},
|
||||
|
||||
@ -433,6 +448,9 @@
|
||||
p.totalPages = 1;
|
||||
$(table).addClass('pagerDisabled').find('tr.pagerSavedHeightSpacer').remove();
|
||||
renderTable(table, table.config.rowsCopy, p);
|
||||
if (table.config.debug) {
|
||||
ts.log('pager disabled');
|
||||
}
|
||||
}
|
||||
// disable size selector
|
||||
p.$size.add(p.$goto).each(function(){
|
||||
@ -442,17 +460,25 @@
|
||||
|
||||
moveToPage = function(table, p, flag) {
|
||||
if ( p.isDisabled ) { return; }
|
||||
var l = p.last,
|
||||
var c = table.config,
|
||||
l = p.last,
|
||||
pg = Math.min( p.totalPages, p.filteredPages );
|
||||
if ( p.page < 0 ) { p.page = 0; }
|
||||
if ( p.page > ( pg - 1 ) && pg !== 0 ) { p.page = pg - 1; }
|
||||
// don't allow rendering multiple times on the same page/size/totalpages/filters
|
||||
if (l.page === p.page && l.size === p.size && l.total === p.totalPages && l.filters === p.currentFilters ) { return; }
|
||||
// don't allow rendering multiple times on the same page/size/totalpages/filters/sorts
|
||||
if ( l.page === p.page && l.size === p.size && l.totalPages === p.totalPages &&
|
||||
(l.currentFilters || []).join(',') === (p.currentFilters || []).join(',') &&
|
||||
l.sortList === (c.sortList || []).join(',') ) { return; }
|
||||
if (c.debug) {
|
||||
ts.log('Pager changing to page ' + p.page);
|
||||
}
|
||||
p.last = {
|
||||
page : p.page,
|
||||
size : p.size,
|
||||
// fixes #408; modify sortList otherwise it auto-updates
|
||||
sortList : (c.sortList || []).join(','),
|
||||
totalPages : p.totalPages,
|
||||
currentFilters : p.currentFilters
|
||||
currentFilters : p.currentFilters || []
|
||||
};
|
||||
if (p.ajax) {
|
||||
getAjax(table, p);
|
||||
@ -460,18 +486,18 @@
|
||||
renderTable(table, table.config.rowsCopy, p);
|
||||
}
|
||||
$.data(table, 'pagerLastPage', p.page);
|
||||
$.data(table, 'pagerUpdateTriggered', true);
|
||||
if (p.initialized && flag !== false) {
|
||||
$(table).trigger('pageMoved', p);
|
||||
c.$table.trigger('pageMoved', p);
|
||||
c.$table.trigger('applyWidgets');
|
||||
}
|
||||
},
|
||||
|
||||
setPageSize = function(table, size, p) {
|
||||
p.size = size;
|
||||
p.$size.val(size);
|
||||
p.size = size || p.size || 10;
|
||||
p.$size.val(p.size);
|
||||
$.data(table, 'pagerLastPage', p.page);
|
||||
$.data(table, 'pagerLastSize', p.size);
|
||||
p.totalPages = Math.ceil( p.totalRows / ( p.size || 10 ) );
|
||||
p.totalPages = Math.ceil( p.totalRows / p.size );
|
||||
moveToPage(table, p);
|
||||
},
|
||||
|
||||
@ -517,14 +543,17 @@
|
||||
p.$goto.removeClass(p.cssDisabled).removeAttr('disabled');
|
||||
p.isDisabled = false;
|
||||
p.page = $.data(table, 'pagerLastPage') || p.page || 0;
|
||||
p.size = $.data(table, 'pagerLastSize') || parseInt(pg.find('option[selected]').val(), 10) || p.size;
|
||||
p.size = $.data(table, 'pagerLastSize') || parseInt(pg.find('option[selected]').val(), 10) || p.size || 10;
|
||||
pg.val(p.size); // set page size
|
||||
p.totalPages = Math.ceil( Math.min( p.totalPages, p.filteredPages ) / ( p.size || 10 ) );
|
||||
p.totalPages = Math.ceil( Math.min( p.totalPages, p.filteredPages ) / p.size );
|
||||
if ( triggered ) {
|
||||
$(table).trigger('update');
|
||||
setPageSize(table, p.size, p);
|
||||
hideRowsSetup(table, p);
|
||||
fixHeight(table, p);
|
||||
if (table.config.debug) {
|
||||
ts.log('pager enabled');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@ -534,8 +563,8 @@
|
||||
if ( !p.ajax ) {
|
||||
c.rowsCopy = rows;
|
||||
p.totalRows = p.countChildRows ? c.$tbodies.eq(0).children().length : rows.length;
|
||||
p.size = $.data(table, 'pagerLastSize') || p.size;
|
||||
p.totalPages = Math.ceil( p.totalRows / ( p.size || 10 ) );
|
||||
p.size = $.data(table, 'pagerLastSize') || p.size || 10;
|
||||
p.totalPages = Math.ceil( p.totalRows / p.size );
|
||||
renderTable(table, rows, p);
|
||||
}
|
||||
};
|
||||
@ -551,34 +580,36 @@
|
||||
$t = c.$table,
|
||||
// added in case the pager is reinitialized after being destroyed.
|
||||
pager = p.$container = $(p.container).addClass('tablesorter-pager').show();
|
||||
if (c.debug) {
|
||||
ts.log('Pager initializing');
|
||||
}
|
||||
p.oldAjaxSuccess = p.oldAjaxSuccess || p.ajaxObject.success;
|
||||
c.appender = $this.appender;
|
||||
|
||||
if (ts.filter && c.widgets.indexOf('filter') >= 0) {
|
||||
// get any default filter settings (data-value attribute) fixes #388
|
||||
p.currentFilters = c.$table.data('lastSearch') || ts.filter.setDefaults(table, c, c.widgetOptions) || [];
|
||||
// set, but don't apply current filters
|
||||
ts.setFilters(table, p.currentFilters, false);
|
||||
}
|
||||
if (p.savePages && ts.storage) {
|
||||
t = ts.storage(table, 'tablesorter-pager') || {}; // fixes #387
|
||||
p.page = isNaN(t.page) ? p.page : t.page;
|
||||
p.size = ( isNaN(t.size) ? p.size : t.size ) || 10;
|
||||
$.data(table, 'pagerLastSize', p.size);
|
||||
}
|
||||
|
||||
$t
|
||||
.unbind('filterStart.pager filterEnd.pager sortEnd.pager disable.pager enable.pager destroy.pager update.pager pageSize.pager')
|
||||
.unbind('filterStart filterEnd sortEnd disable enable destroy update pageSize '.split(' ').join('.pager '))
|
||||
.bind('filterStart.pager', function(e, filters) {
|
||||
$.data(table, 'pagerUpdateTriggered', false);
|
||||
p.currentFilters = filters;
|
||||
})
|
||||
// update pager after filter widget completes
|
||||
.bind('filterEnd.pager sortEnd.pager', function(e) {
|
||||
//Prevent infinite event loops from occuring by setting this in all moveToPage calls and catching it here.
|
||||
if ($.data(table, 'pagerUpdateTriggered')) {
|
||||
$.data(table, 'pagerUpdateTriggered', false);
|
||||
return;
|
||||
}
|
||||
//only run the server side sorting if it has been enabled
|
||||
if (e.type === "filterEnd" || (e.type === "sortEnd" && c.serverSideSorting)) {
|
||||
.bind('filterEnd.pager sortEnd.pager', function() {
|
||||
if (p.initialized) {
|
||||
moveToPage(table, p, false);
|
||||
updatePageDisplay(table, p, false);
|
||||
fixHeight(table, p);
|
||||
}
|
||||
updatePageDisplay(table, p, false);
|
||||
fixHeight(table, p);
|
||||
})
|
||||
.bind('disable.pager', function(e){
|
||||
e.stopPropagation();
|
||||
@ -617,6 +648,7 @@
|
||||
pager.find(ctrls.join(','))
|
||||
.unbind('click.pager')
|
||||
.bind('click.pager', function(e){
|
||||
e.stopPropagation();
|
||||
var i, $t = $(this), l = ctrls.length;
|
||||
if ( !$t.hasClass(p.cssDisabled) ) {
|
||||
for (i = 0; i < l; i++) {
|
||||
@ -626,7 +658,6 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
// goto selector
|
||||
|
@ -44,6 +44,7 @@
|
||||
theme: 'blue',
|
||||
widthFixed: true,
|
||||
sortLocaleCompare: true, // needed for accented characters in the data
|
||||
sortList: [ [0,1] ],
|
||||
widgets: ['zebra', 'filter']
|
||||
})
|
||||
|
||||
@ -180,6 +181,7 @@
|
||||
<h3><a href="#">Notes</a></h3>
|
||||
<div>
|
||||
<ul>
|
||||
<li>In <span class="version">v2.13.3</span>, 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>
|
||||
<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>
|
||||
<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>
|
||||
<li><code>{filterList:fcol}</code> was added to the <code>ajaxUrl</code> in version 2.6.</li>
|
||||
@ -268,9 +270,6 @@
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
<h1>Demo</h1>
|
||||
@ -292,7 +291,7 @@ Current Ajax url: <span id="url"></span>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>1</th>
|
||||
<th data-value=">30">1</th>
|
||||
<th>2</th>
|
||||
<th>3</th>
|
||||
<th>4</th>
|
||||
@ -389,7 +388,7 @@ td.pager {
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>1</th> <!-- thead text will be updated from the JSON; make sure the number of columns matches the JSON data -->
|
||||
<th>1 data-value="&gt;30"</th> <!-- thead text will be updated from the JSON; make sure the number of columns matches the JSON data -->
|
||||
<th>2</th>
|
||||
<th>3</th>
|
||||
<th>4</th>
|
||||
@ -439,7 +438,7 @@ $('table')
|
||||
$url.html(url);
|
||||
})
|
||||
|
||||
.on('pagerInitialized', function(){ console.log('pager init');
|
||||
.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;
|
||||
|
133
docs/example-widget-filter-one-input.html
Normal file
133
docs/example-widget-filter-one-input.html
Normal file
@ -0,0 +1,133 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>jQuery plugin: Tablesorter 2.0 - Filter Widget</title>
|
||||
|
||||
<!-- jQuery -->
|
||||
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js"></script>
|
||||
|
||||
<!-- Demo stuff -->
|
||||
<link class="ui-theme" rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/cupertino/jquery-ui.css">
|
||||
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7/jquery-ui.min.js"></script>
|
||||
<link rel="stylesheet" href="css/jq.css">
|
||||
<link href="css/prettify.css" rel="stylesheet">
|
||||
<script src="js/prettify.js"></script>
|
||||
<script src="js/docs.js"></script>
|
||||
|
||||
<!-- Tablesorter: required -->
|
||||
<link rel="stylesheet" href="../css/theme.blue.css">
|
||||
<script src="../js/jquery.tablesorter.js"></script>
|
||||
<script src="../js/jquery.tablesorter.widgets.js"></script>
|
||||
|
||||
<script id="js">$(function() {
|
||||
|
||||
// call the tablesorter plugin
|
||||
$("table").tablesorter({
|
||||
theme: 'blue',
|
||||
|
||||
// hidden filter input/selects will resize the columns, so try to minimize the change
|
||||
widthFixed : true,
|
||||
|
||||
// initialize zebra striping and filter widgets
|
||||
widgets: ["zebra", "filter"],
|
||||
|
||||
// headers: { 5: { sorter: false, filter: false } },
|
||||
|
||||
widgetOptions : {
|
||||
|
||||
// if true overrides default find rows behaviours and if any column matches query it returns that row
|
||||
filter_anyMatch : true,
|
||||
|
||||
filter_columnFilters: false,
|
||||
|
||||
filter_reset: '.reset'
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
$(".search").keyup(function (e) {
|
||||
$('table').trigger('search', [ [this.value] ]);
|
||||
});
|
||||
|
||||
|
||||
});</script>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<div id="banner">
|
||||
<h1>table<em>sorter</em></h1>
|
||||
<h2>Filter Widget Any Match</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>
|
||||
<ul>
|
||||
<li>This is a demo of the <code>filter_anyMatch</code> option.</li>
|
||||
<li>This method has some severe limitations in that it does not support all of the per column search features!
|
||||
<ul>
|
||||
<li></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>
|
||||
</ul>
|
||||
</p>
|
||||
|
||||
<h1>Demo</h1>
|
||||
|
||||
<div id="demo">
|
||||
<input class="search" type="search">
|
||||
<button type="button" class="reset">Reset Search</button> <!-- targetted by the "filter_reset" option -->
|
||||
|
||||
<table class="tablesorter">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Rank</th>
|
||||
<th>First Name</th>
|
||||
<th>Last Name</th>
|
||||
<th>Age</th>
|
||||
<th>Total</th>
|
||||
<th>Discount</th>
|
||||
<th>Date</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr><td>1</td><td>Philip Aaron</td><td>Johnson Sr Esq</td><td>25</td><td>$5.95</td><td>22%</td><td>Jun 26, 2004 7:22 AM</td></tr>
|
||||
<tr><td>11</td><td>Aaron</td><td>Hibert</td><td>12</td><td>$2.99</td><td>5%</td><td>Aug 21, 2009 12:21 PM</td></tr>
|
||||
<tr><td>12</td><td>Brandon Clark</td><td>Henry Jr</td><td>51</td><td>$42.29</td><td>18%</td><td>Oct 13, 2000 1:15 PM</td></tr>
|
||||
<tr><td>111</td><td>Peter</td><td>Parker</td><td>28</td><td>$9.99</td><td>20%</td><td>Jul 6, 2006 8:14 AM</td></tr>
|
||||
<tr><td>21</td><td>John</td><td>Hood</td><td>33</td><td>$19.99</td><td>25%</td><td>Dec 10, 2002 5:14 AM</td></tr>
|
||||
<tr><td>013</td><td>Clark</td><td>Kent Sr.</td><td>18</td><td>$15.89</td><td>44%</td><td>Jan 12, 2003 11:14 AM</td></tr>
|
||||
<tr><td>005</td><td>Bruce</td><td>Almighty Esq</td><td>45</td><td>$153.19</td><td>44%</td><td>Jan 18, 2021 9:12 AM</td></tr>
|
||||
<tr><td>10</td><td>Alex</td><td>Dumass</td><td>13</td><td>$5.29</td><td>4%</td><td>Jan 8, 2012 5:11 PM</td></tr>
|
||||
<tr><td>16</td><td>Jim</td><td>Franco</td><td>24</td><td>$14.19</td><td>14%</td><td>Jan 14, 2004 11:23 AM</td></tr>
|
||||
<tr><td>166</td><td>Bruce Lee</td><td>Evans</td><td>22</td><td>$13.19</td><td>11%</td><td>Jan 18, 2007 9:12 AM</td></tr>
|
||||
<tr><td>100</td><td>Brenda Lee</td><td>McMasters</td><td>18</td><td>$55.20</td><td>15%</td><td>Feb 12, 2010 7:23 PM</td></tr>
|
||||
<tr><td>55</td><td>Dennis</td><td>Bronson</td><td>65</td><td>$123.00</td><td>32%</td><td>Jan 20, 2001 1:12 PM</td></tr>
|
||||
<tr><td>9</td><td>Martha</td><td>delFuego</td><td>25</td><td>$22.09</td><td>17%</td><td>Jun 11, 2011 10:55 AM</td></tr>
|
||||
</tbody>
|
||||
</table></div>
|
||||
|
||||
<h1>Javascript</h1>
|
||||
<div id="javascript">
|
||||
<pre class="prettyprint lang-javascript"></pre>
|
||||
</div>
|
||||
|
||||
<h1>HTML</h1>
|
||||
<div id="html">
|
||||
<pre class="prettyprint lang-html"></pre>
|
||||
</div>
|
||||
|
||||
<div class="next-up">
|
||||
<hr />
|
||||
Next up: <a href="example-widget-filter-custom.html">jQuery custom filter widget ››</a>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
@ -34,6 +34,7 @@
|
||||
theme: 'blue',
|
||||
widthFixed: true,
|
||||
sortLocaleCompare: true, // needed for accented characters in the data
|
||||
sortList: [ [0,1] ],
|
||||
widgets: ['zebra', 'filter', 'pager'],
|
||||
widgetOptions: {
|
||||
|
||||
@ -175,6 +176,7 @@
|
||||
<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 ">30" and a descending sort, but neither is applied as this demo is not connected a server (just a basic JSON file).</li>
|
||||
</ul>
|
||||
</p>
|
||||
|
||||
@ -198,7 +200,7 @@ Current Ajax url: <span id="url"></span>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>1</th>
|
||||
<th data-value=">30">1</th>
|
||||
<th>2</th>
|
||||
<th>3</th>
|
||||
<th>4</th>
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* Pager widget (beta) for TableSorter 10/30/2013 */
|
||||
/* Pager widget (beta) for TableSorter 11/8/2013 */
|
||||
/*jshint browser:true, jquery:true, unused:false */
|
||||
;(function($){
|
||||
"use strict";
|
||||
@ -7,7 +7,7 @@ var tsp,
|
||||
|
||||
ts.addWidget({
|
||||
id: "pager",
|
||||
priority: 5,
|
||||
priority: 55, // load pager after filter widget
|
||||
options : {
|
||||
// output default: '{page}/{totalPages}'
|
||||
// possible variables: {page}, {totalPages}, {filteredPages}, {startRow}, {endRow}, {filteredRows} and {totalRows}
|
||||
@ -98,7 +98,7 @@ ts.addWidget({
|
||||
}
|
||||
tsp.moveToPage(table, c.pager, false);
|
||||
},
|
||||
remove: function(table, c, wo){
|
||||
remove: function(table, c){
|
||||
tsp.destroyPager(table, c);
|
||||
}
|
||||
});
|
||||
@ -128,24 +128,32 @@ tsp = ts.pager = {
|
||||
last: {}
|
||||
}, c.pager);
|
||||
|
||||
if (c.debug) {
|
||||
ts.log('Pager initializing');
|
||||
}
|
||||
|
||||
// added in case the pager is reinitialized after being destroyed.
|
||||
p.$container = $(s.container).addClass(wo.pager_css.container).show();
|
||||
// goto selector
|
||||
p.$goto = p.$container.find(s.goto);
|
||||
// page size selector
|
||||
p.$size = p.$container.find(s.pageSize);
|
||||
|
||||
p.totalRows = c.$tbodies.eq(0).children().length;
|
||||
|
||||
p.oldAjaxSuccess = p.oldAjaxSuccess || wo.pager_ajaxObject.success;
|
||||
c.appender = tsp.appender;
|
||||
|
||||
if (ts.filter && c.widgets.indexOf('filter') >= 0) {
|
||||
// get any default filter settings (data-value attribute) fixes #388
|
||||
p.currentFilters = c.$table.data('lastSearch') || ts.filter.setDefaults(table, c, wo) || [];
|
||||
// set, but don't apply current filters
|
||||
ts.setFilters(table, p.currentFilters, false);
|
||||
}
|
||||
if (wo.pager_savePages && ts.storage) {
|
||||
t = ts.storage(table, 'tablesorter-pager') || {}; // fixes #387
|
||||
p.page = isNaN(t.page) ? p.page : t.page;
|
||||
p.size = ( isNaN(t.size) ? p.size : t.size ) || 10;
|
||||
$.data(table, 'pagerLastSize', p.size);
|
||||
}
|
||||
|
||||
// clear initialized flag
|
||||
p.initialized = false;
|
||||
// before initialization event
|
||||
@ -193,22 +201,15 @@ tsp = ts.pager = {
|
||||
c.$table
|
||||
.unbind('filterStart filterEnd sortEnd disable enable destroy update pageSize '.split(' ').join('.pager '))
|
||||
.bind('filterStart.pager', function(e, filters) {
|
||||
$.data(table, 'pagerUpdateTriggered', false);
|
||||
p.currentFilters = filters;
|
||||
})
|
||||
// update pager after filter widget completes
|
||||
.bind('filterEnd.pager sortEnd.pager', function(e) {
|
||||
//Prevent infinite event loops from occuring by setting this in all moveToPage calls and catching it here.
|
||||
if ($.data(table, 'pagerUpdateTriggered')) {
|
||||
$.data(table, 'pagerUpdateTriggered', false);
|
||||
return;
|
||||
}
|
||||
//only run the server side sorting if it has been enabled
|
||||
if (e.type === "filterEnd" || (e.type === "sortEnd" && c.serverSideSorting)) {
|
||||
.bind('filterEnd.pager sortEnd.pager', function() {
|
||||
if (p.initialized) {
|
||||
tsp.moveToPage(table, p, false);
|
||||
tsp.updatePageDisplay(table, c, false);
|
||||
tsp.fixHeight(table, c);
|
||||
}
|
||||
tsp.updatePageDisplay(table, c, false);
|
||||
tsp.fixHeight(table, c);
|
||||
})
|
||||
.bind('disable.pager', function(e){
|
||||
e.stopPropagation();
|
||||
@ -246,7 +247,8 @@ tsp = ts.pager = {
|
||||
fxn = [ 'moveToFirstPage', 'moveToPrevPage', 'moveToNextPage', 'moveToLastPage' ];
|
||||
p.$container.find(ctrls.join(','))
|
||||
.unbind('click.pager')
|
||||
.bind('click.pager', function(){
|
||||
.bind('click.pager', function(e){
|
||||
e.stopPropagation();
|
||||
var i,
|
||||
$c = $(this),
|
||||
l = ctrls.length;
|
||||
@ -258,7 +260,6 @@ tsp = ts.pager = {
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
if ( p.$goto.length ) {
|
||||
@ -300,7 +301,7 @@ tsp = ts.pager = {
|
||||
},
|
||||
|
||||
updatePageDisplay: function(table, c, flag) {
|
||||
var i, pg, s, t, out,
|
||||
var i, pg, s, out,
|
||||
wo = c.widgetOptions,
|
||||
p = c.pager,
|
||||
f = c.$table.hasClass('hasFilters') && !wo.pager_ajaxUrl,
|
||||
@ -427,6 +428,9 @@ tsp = ts.pager = {
|
||||
$t.find('thead tr.' + wo.pager_css.errorRow).remove(); // Clean up any previous error.
|
||||
|
||||
if ( exception ) {
|
||||
if (c.debug) {
|
||||
ts.log('Ajax Error', xhr, exception);
|
||||
}
|
||||
$err = $('<tr class="' + wo.pager_css.errorRow + '"><td style="text-align:center;" colspan="' +
|
||||
hl + '">' + exception.message + ' (' + xhr.status + ')</td></tr>')
|
||||
.click(function(){
|
||||
@ -435,7 +439,6 @@ tsp = ts.pager = {
|
||||
// add error row to thead instead of tbody, or clicking on the header will result in a parser error
|
||||
.appendTo( $t.find('thead:first') );
|
||||
c.$tbodies.eq(0).empty();
|
||||
if (c.debug) { ts.log({ 'exception' : exception, 'jqxhr' : xhr }); }
|
||||
} else {
|
||||
// process ajax object
|
||||
if (!$.isArray(result)) {
|
||||
@ -456,17 +459,15 @@ tsp = ts.pager = {
|
||||
if (d instanceof jQuery) {
|
||||
// append jQuery object
|
||||
c.$tbodies.eq(0).empty().append(d);
|
||||
} else if (d.length) {
|
||||
} else if (l) {
|
||||
// build table from array
|
||||
if ( l > 0 ) {
|
||||
for ( i = 0; i < l; i++ ) {
|
||||
tds += '<tr>';
|
||||
for ( j = 0; j < d[i].length; j++ ) {
|
||||
// build tbody cells
|
||||
tds += '<td>' + d[i][j] + '</td>';
|
||||
}
|
||||
tds += '</tr>';
|
||||
for ( i = 0; i < l; i++ ) {
|
||||
tds += '<tr>';
|
||||
for ( j = 0; j < d[i].length; j++ ) {
|
||||
// build tbody cells
|
||||
tds += '<td>' + d[i][j] + '</td>';
|
||||
}
|
||||
tds += '</tr>';
|
||||
}
|
||||
// add rows to first tbody
|
||||
c.$tbodies.eq(0).html( tds );
|
||||
@ -500,9 +501,15 @@ tsp = ts.pager = {
|
||||
if (c.showProcessing) {
|
||||
ts.isProcessing(table); // remove loading icon
|
||||
}
|
||||
p.totalPages = Math.ceil( p.totalRows / ( p.size || 10 ) );
|
||||
// make sure last pager settings are saved, prevents multiple server side calls with
|
||||
// the same parameters
|
||||
p.last.totalPages = p.totalPages = Math.ceil( p.totalRows / ( p.size || 10 ) );
|
||||
p.last.currentFilters = p.currentFilters;
|
||||
p.last.sortList = (c.sortList || []).join(',');
|
||||
tsp.updatePageDisplay(table, c);
|
||||
tsp.fixHeight(table, c);
|
||||
// apply widgets after table has rendered
|
||||
$t.trigger('applyWidgets');
|
||||
if (p.initialized) {
|
||||
$t.trigger('pagerChange', c);
|
||||
$t.trigger('updateComplete');
|
||||
@ -537,6 +544,9 @@ tsp = ts.pager = {
|
||||
p.oldAjaxSuccess(data);
|
||||
}
|
||||
};
|
||||
if (c.debug) {
|
||||
ts.log('ajax initialized', wo.pager_ajaxObject);
|
||||
}
|
||||
$.ajax(wo.pager_ajaxObject);
|
||||
}
|
||||
},
|
||||
@ -549,7 +559,7 @@ tsp = ts.pager = {
|
||||
.replace(/\{page([\-+]\d+)?\}/, function(s,n){ return p.page + (n ? parseInt(n, 10) : 0); })
|
||||
.replace(/\{size\}/g, p.size) : '',
|
||||
sl = c.sortList,
|
||||
fl = p.currentFilters || [],
|
||||
fl = p.currentFilters || $(table).data('lastSearch') || [],
|
||||
sortCol = url.match(/\{\s*sort(?:List)?\s*:\s*(\w*)\s*\}/),
|
||||
filterCol = url.match(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/),
|
||||
arry = [];
|
||||
@ -571,10 +581,14 @@ tsp = ts.pager = {
|
||||
});
|
||||
// if the arry is empty, just add the fcol parameter... "&{filterList:fcol}" becomes "&fcol"
|
||||
url = url.replace(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/g, arry.length ? arry.join('&') : filterCol );
|
||||
p.currentFilters = fl;
|
||||
}
|
||||
if ( $.isFunction(wo.pager_customAjaxUrl) ) {
|
||||
url = wo.pager_customAjaxUrl(table, url);
|
||||
}
|
||||
if (c.debug) {
|
||||
ts.log('Pager ajax url: ' + url);
|
||||
}
|
||||
return url;
|
||||
},
|
||||
|
||||
@ -613,6 +627,7 @@ tsp = ts.pager = {
|
||||
|
||||
wo.pager_startPage = p.page;
|
||||
wo.pager_size = p.size;
|
||||
c.$table.trigger('applyWidgets');
|
||||
|
||||
},
|
||||
|
||||
@ -630,10 +645,13 @@ tsp = ts.pager = {
|
||||
p.totalPages = 1;
|
||||
c.$table.addClass('pagerDisabled').find('tr.pagerSavedHeightSpacer').remove();
|
||||
tsp.renderTable(table, c.rowsCopy);
|
||||
if (c.debug) {
|
||||
ts.log('pager disabled');
|
||||
}
|
||||
}
|
||||
// disable size selector
|
||||
p.$size.add(p.$goto).each(function(){
|
||||
$(this).addClass(wo.pager_css.disabled).attr('disabled', 'disabled');
|
||||
$(this).addClass(wo.pager_css.disabled)[0].disabled = true;
|
||||
});
|
||||
c.$table.trigger('applyWidgets');
|
||||
},
|
||||
@ -645,13 +663,20 @@ tsp = ts.pager = {
|
||||
pg = Math.min( p.totalPages, p.filteredPages );
|
||||
if ( p.page < 0 ) { p.page = 0; }
|
||||
if ( p.page > ( pg - 1 ) && pg !== 0 ) { p.page = pg - 1; }
|
||||
// don't allow rendering multiple times on the same page/size/totalpages/filters
|
||||
if (l.page === p.page && l.size === p.size && l.total === p.totalPages && l.filters === p.currentFilters ) { return; }
|
||||
// don't allow rendering multiple times on the same page/size/totalpages/filters/sorts
|
||||
if ( l.page === p.page && l.size === p.size && l.totalPages === p.totalPages &&
|
||||
(l.currentFilters || []).join(',') === (p.currentFilters || []).join(',') &&
|
||||
l.sortList === (c.sortList || []).join(',') ) { return; }
|
||||
if (c.debug) {
|
||||
ts.log('Pager changing to page ' + p.page);
|
||||
}
|
||||
p.last = {
|
||||
page : p.page,
|
||||
size : p.size,
|
||||
// fixes #408; modify sortList otherwise it auto-updates
|
||||
sortList : (c.sortList || []).join(','),
|
||||
totalPages : p.totalPages,
|
||||
currentFilters : p.currentFilters
|
||||
currentFilters : p.currentFilters || []
|
||||
};
|
||||
if (p.ajax) {
|
||||
tsp.getAjax(table, c);
|
||||
@ -659,7 +684,6 @@ tsp = ts.pager = {
|
||||
tsp.renderTable(table, c.rowsCopy);
|
||||
}
|
||||
$.data(table, 'pagerLastPage', p.page);
|
||||
$.data(table, 'pagerUpdateTriggered', true);
|
||||
if (p.initialized && flag !== false) {
|
||||
c.$table.trigger('pageMoved', c);
|
||||
c.$table.trigger('applyWidgets');
|
||||
@ -668,11 +692,11 @@ tsp = ts.pager = {
|
||||
|
||||
setPageSize: function(table, size, c) {
|
||||
var p = c.pager;
|
||||
p.size = size;
|
||||
p.$size.val(size);
|
||||
p.size = size || p.size || 10;
|
||||
p.$size.val(p.size);
|
||||
$.data(table, 'pagerLastPage', p.page);
|
||||
$.data(table, 'pagerLastSize', p.size);
|
||||
p.totalPages = Math.ceil( p.totalRows / ( p.size || 10 ) );
|
||||
p.totalPages = Math.ceil( p.totalRows / p.size );
|
||||
tsp.moveToPage(table, p);
|
||||
},
|
||||
|
||||
@ -715,30 +739,33 @@ tsp = ts.pager = {
|
||||
},
|
||||
|
||||
enablePager: function(table, c, triggered){
|
||||
var p = c.pager,
|
||||
wo = c.widgetOptions;
|
||||
var p = c.pager;
|
||||
p.isDisabled = false;
|
||||
p.page = $.data(table, 'pagerLastPage') || p.page || 0;
|
||||
p.size = $.data(table, 'pagerLastSize') || parseInt(p.$size.find('option[selected]').val(), 10) || p.size;
|
||||
p.size = $.data(table, 'pagerLastSize') || parseInt(p.$size.find('option[selected]').val(), 10) || p.size || 10;
|
||||
p.$size.val(p.size); // set page size
|
||||
p.totalPages = Math.ceil( Math.min( p.totalPages, p.filteredPages ) / ( p.size || 10 ) );
|
||||
p.totalPages = Math.ceil( Math.min( p.totalPages, p.filteredPages ) / p.size );
|
||||
c.$table.removeClass('pagerDisabled');
|
||||
if ( triggered ) {
|
||||
c.$table.trigger('update');
|
||||
tsp.setPageSize(table, p.size, c);
|
||||
tsp.hideRowsSetup(table, c);
|
||||
tsp.fixHeight(table, c);
|
||||
if (c.debug) {
|
||||
ts.log('pager enabled');
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
appender: function(table, rows) {
|
||||
var c = table.config,
|
||||
wo = c.widgetOptions,
|
||||
p = c.pager;
|
||||
if ( !p.ajax ) {
|
||||
c.rowsCopy = rows;
|
||||
p.totalRows = c.widgetOptions.pager_countChildRows ? c.$tbodies.eq(0).children().length : rows.length;
|
||||
p.size = $.data(table, 'pagerLastSize') || p.size;
|
||||
p.totalPages = Math.ceil( p.totalRows / ( p.size || 10 ) );
|
||||
p.size = $.data(table, 'pagerLastSize') || p.size || wo.pager_size || 10;
|
||||
p.totalPages = Math.ceil( p.totalRows / p.size );
|
||||
tsp.moveToPage(table, p);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user