Toggle: Add widget to enable/disable sort & filter

This commit is contained in:
Rob Garrison 2015-11-08 10:45:25 -06:00
parent 57779698f5
commit 60ea78eea6
3 changed files with 304 additions and 0 deletions

View File

@ -0,0 +1,220 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery plugin: Tablesorter 2.0 - Toggle Widget</title>
<!-- jQuery -->
<script src="js/jquery-latest.min.js"></script>
<!-- Demo stuff -->
<link class="ui-theme" rel="stylesheet" href="css/jquery-ui.min.css">
<script src="js/jquery-ui.min.js"></script>
<link rel="stylesheet" href="css/jq.css">
<link href="css/prettify.css" rel="stylesheet">
<link href="css/tipsy.css" rel="stylesheet">
<script src="js/prettify.js"></script>
<script src="js/docs.js"></script>
<script src="js/jquery.tipsy.min.js"></script>
<!-- Tablesorter: required -->
<link rel="stylesheet" href="../css/theme.blue.css">
<script src="../js/jquery.tablesorter.js"></script>
<script src="../js/widgets/widget-filter.js"></script>
<script src="../js/widgets/widget-headerTitles.js"></script>
<script src="../js/widgets/widget-toggle.js"></script>
<script id="js">$( function() {
var $table = $( '#table' ),
isEnabled = true, // tablesorter enabled
hideFilterRow = false, // hide filter row setting
// used by headerTitles widget
updateTooltips = function( $cell, txt ) {
if ( $cell.is( ':hover' ) ) {
$cell
.attr({ title : txt, 'original-title' : txt })
// hide, then show the tooltip to force updating & realignment
.tipsy( 'hide' )
.tipsy( 'show' );
}
return txt;
};
$table.tablesorter({
theme : 'blue',
widthFixed : true,
widgets : [ 'zebra', 'filter', 'headerTitles', 'toggle-ts' ],
widgetOptions : {
filter_reset : '.reset',
filter_external : '.search',
filter_columnFilters : true,
headerTitle_useAria : true,
headerTitle_tooltip : 'tooltip',
headerTitle_callback : updateTooltips,
// toggle-ts widget
toggleTS_hideFilterRow : hideFilterRow,
toggleTS_callback : function( c, isEnabled ) {
// update tooltips
c.$table.trigger( 'refreshHeaderTitle' );
}
}
});
// enable/disable tablesorter button
$( '.toggle' ).click( function() {
isEnabled = !isEnabled;
$( this ).text( isEnabled ? 'Disable' : 'Enable' );
$table.trigger( isEnabled ? 'enable' : 'disable' );
// If using the pager:
// $table.trigger( ( isEnabled ? 'enable' : 'disable' ) + 'Pager' );
// To reset sort or filter, include the following
// if ( !isEnabled ) {
// $table.trigger( 'sortReset' ).trigger( 'filterReset' );
// }
});
// demo: toggle filter row option
$( '.filterRow' ).click( function() {
hideFilterRow = !hideFilterRow;
$table[ 0 ].config.widgetOptions.toggleTS_hideFilterRow = hideFilterRow;
$( this ).text( '' + hideFilterRow );
});
// add tooltip
$( '.tooltip' ).tipsy({ gravity: 's' });
});</script>
</head>
<body>
<div id="banner">
<h1>table<em>sorter</em></h1>
<h2>Toggle Sort &amp; Filter Widget</h2>
<h3>Flexible client-side table sorting</h3>
<a href="index.html">Back to documentation</a>
</div>
<div id="main">
<p></p>
<br>
<div id="root" class="accordion">
<h3 id="notes"><a href="#">Notes</a></h3>
<div>
<ul>
<li>This widget allows the enabling or disabling of tablesorter sorting &amp; filtering.</li>
<li>The widget does not reset an applied sort or filters. If you want to include this when disabling, use the following code:
<ul>
<li><a href="index.html#sortreset-method">Sort reset</a>: <code class="prettyprint lang-js">$('table').trigger('sortReset');</code></li>
<li><a href="example-widget-filter.html#method-reset">Filter reset</a>: <code class="prettyprint lang-js">$('table').trigger('filterReset');</code></li>
</ul>
<br>
</li>
<li><span class="label alert">*ALERT*</span> This widget requires tablesorter v2.22.4+ and jQuery 1.7+</li>
</ul>
</div>
<h3><a href="#">Options</a></h3>
<div>
<div>
<span class="label label-info">TIP!</span> Click on the link in the option column to reveal full details (or <a href="#" class="toggleAll">toggle</a>|<a href="#" class="showAll">show</a>|<a href="#" class="hideAll">hide</a> all) or double click to update the browser location.
</div>
<table class="options tablesorter-blue" data-sortlist="[[0,0]]">
<thead>
<tr><th>Option</th><th class="defaults">Default</th><th class="sorter-false">Description</th></tr>
</thead>
<tbody>
<tr id="togglets_hidefilterrow">
<td><span class="permalink">toggleTS_hideFilterRow</span></td>
<td>false</td>
<td>If <code>true</code>, hide filter row; when <code>false</code>, associated filters are disabled.</td>
</tr>
<tr id="togglets_callback">
<td><a href="#" class="permalink">toggleTS_callback</a></td>
<td>null</td>
<td>This function is called after tablesorter has been enabled or disabled.
<div class="collapsible">
<p>Use it as follows:</p>
<pre class="prettyprint lang-js">$( function() {
$( 'table' ).tablesorter({
widget : [ 'toggle-ts' ],
widgetOptions : {
toggleTS_callback : function( c, isEnabled ) {
c.$table.toggleClass( 'isDisabled', !isEnabled );
}
}
});
});</pre>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p></p>
<h1>Demo</h1>
<div id="demo"><p>
Tablesorter & Filter: <button type="button" class="toggle">Disable</button>
Hide Filter Row: <button type="button" class="filterRow">false</button>
</p>
Filter any column: <input class="search" type="search" data-column="any">
<p>Filter <button type="button" class="reset">Reset</button> (this works even if tablesorter & filter are disabled)</p>
<table id="table" class="tablesorter">
<thead>
<tr>
<th>Rank</th>
<th data-value="e">First Name</th>
<th>Last Name</th>
<th>Age</th>
<th class="filter-false">Total (filter disabled)</th>
<th class="sorter-false">Discount (sort disabled)</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<tr><td>1</td><td>Philip Aaron Wong</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 Dexter</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>
</body>
</html>

View File

@ -504,6 +504,8 @@
<li><span class="results">&dagger;</span> <a href="example-widget-sticky-header.html">Sticky header widget</a> (v2.0.21.1; <span class="version updated">v2.24.0</span>).</li>
<li><a href="example-widget-css-sticky-header.html">Sticky header (css3) widget</a> (<span class="version">v2.14.2</span>; <span class="version updated">v2.19.1</span>).</li>
<li><span class="label label-info">Beta</span> <a href="example-widget-toggle-tablesorter.html">Toggle Sort & Filter Widget</a> (<span class="version">v2.24.4</span>).</li>
<li><span class="results">&dagger;</span> UITheme widget (<span class="version updated">v2.17.4</span>; <span class="version updated">v2.19.0</span>):
<ul>
<li><a href="example-widget-ui-theme.html">jQuery UI theme</a> (v2.0.9).</li>
@ -5788,6 +5790,7 @@ $('table').trigger( 'search', [['', '', '', '', 'orange']] ); // find orange in
</td>
<td>
<a href="example-widget-header-titles.html">1</a>
<a href="example-widget-toggle-tablesorter.html">2</a>
</td>
</tr>
<tr id="variable-instanceMethods">

View File

@ -0,0 +1,81 @@
/*! tablesorter enable/disable sort & filter (BETA) - 11/8/2015 (v2.24.4)
* Requires tablesorter v2.24.4+ & jQuery 1.7+
* by Rob Garrison
*/
;( function( $ ) {
'use strict';
var ts = $.tablesorter,
tst = ts.toggleTS = {
init : function( c, wo ) {
wo.toggleTS_isEnabled = true; // enabled
wo.toggleTS_areDisabled = {
headers : [],
filters : []
};
c.$table.on('enable.toggleTS disable.toggleTS', function( event ) {
tst.toggle( this.config, this.config.widgetOptions, event.type === 'enable' );
});
},
toggle : function( c, wo, isEnabled ) {
if ( wo.toggleTS_isEnabled !== isEnabled ) {
wo.toggleTS_isEnabled = isEnabled;
var indx, $el,
len = c.$headers.length;
// table headers
for ( indx = 0; indx < len; indx++ ) {
$el = c.$headers.eq( indx );
// function added in v2.24.4
ts.setColumnSort( c, $el, !isEnabled );
// function added in v2.24.4; passing "isEnabled" allows removal of "next sort" labels
ts.setColumnAriaLabel( c, $el, isEnabled );
}
if ( wo.toggleTS_hideFilterRow ) {
c.$table.find( '.' + ts.css.filterRow ).toggle( isEnabled );
} else if ( ts.hasWidget( c.$table, 'filter' ) ) {
// c.$filters points to filter CELL
len = c.$filters.length;
for ( indx = 0; indx < len; indx++ ) {
if ( isEnabled && !wo.toggleTS_areDisabled.filters[ indx ] ) {
c.$filters.eq( indx ).find( 'input, select' )
.removeClass( ts.css.filterDisabled )
.prop( 'disabled', false );
} else if ( !isEnabled ) {
$el = c.$filters.eq( indx ).find( 'input, select' );
if ( $el.hasClass( ts.css.filterDisabled ) ) {
wo.toggleTS_areDisabled.filters[ indx ] = true;
}
$el
.addClass( ts.css.filterDisabled )
.prop( 'disabled', true );
}
}
}
// include external filters
wo.filter_$externalFilters
.toggleClass( ts.css.filterDisabled, isEnabled )
.prop( 'disabled', !isEnabled );
}
if ( typeof wo.toggleTS_callback === 'function' ) {
wo.toggleTS_callback( c, isEnabled );
}
}
};
ts.addWidget({
id: 'toggle-ts',
options: {
toggleTS_hideFilterRow : false,
toggleTS_callback : null
},
init : function( table, thisWidget, c, wo ) {
tst.init( c, wo );
},
remove : function( table, c ) {
c.$table.off( 'enable.toggleTS disable.toggleTS' );
}
});
})( jQuery );