mirror of
https://github.com/Mottie/tablesorter.git
synced 2025-01-12 15:24:21 +00:00
currentSort: add currentSort widget. Fixes #1208
This commit is contained in:
parent
5e67437604
commit
710929579e
2
dist/js/widgets/widget-currentSort.min.js
vendored
Normal file
2
dist/js/widgets/widget-currentSort.min.js
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
/*! Widget: currentSort - 7/26/2016 (v2.27.0) */
|
||||||
|
!function(a){"use strict";var b=a.tablesorter;b.currentSortLanguage={0:"asc",1:"desc",2:"unsorted"},b.currentSort={init:function(a){a.$table.on("sortEnd.tscurrentSort",function(){b.currentSort.update(this.config)})},update:function(a){if(a){var c,d=a.widgetOptions,e=b.currentSortLanguage,f=e[2],g=Array.apply(null,Array(a.columns)).map(String.prototype.valueOf,f),h=a.sortList,i=h.length;for(c=0;i>c;c++)g[h[c][0]]=e[h[c][1]];a.currentSort=g,"function"==typeof d.currentSort_callback&&d.currentSort_callback(a,g)}}},b.addWidget({id:"currentSort",options:{currentSort_callback:null},init:function(a,c,d,e){b.currentSort.init(d,e)},remove:function(a,b){b.$table.off("sortEnd.tscurrentSort")}})}(jQuery);
|
117
docs/example-widget-current-sort.html
Normal file
117
docs/example-widget-current-sort.html
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>jQuery tablesorter 2.0 - currentSort widget</title>
|
||||||
|
|
||||||
|
<!-- jQuery -->
|
||||||
|
<script src="js/jquery-latest.min.js"></script>
|
||||||
|
|
||||||
|
<!-- Demo stuff -->
|
||||||
|
<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/widgets/widget-currentSort.js"></script>
|
||||||
|
<style>
|
||||||
|
#display li:last-child { color: #008080; }
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<script id="js">$(function(){
|
||||||
|
|
||||||
|
$.tablesorter.currentSortLanguage = {
|
||||||
|
0: 'asc',
|
||||||
|
1: 'desc',
|
||||||
|
2: 'unsorted'
|
||||||
|
};
|
||||||
|
|
||||||
|
$('table').tablesorter({
|
||||||
|
theme: 'blue',
|
||||||
|
widgets : ['zebra', 'currentSort'],
|
||||||
|
widgetOptions: {
|
||||||
|
currentSort_callback: function(config, values) {
|
||||||
|
// values also contained in config.currentSort
|
||||||
|
// update display
|
||||||
|
var $display = $('#display');
|
||||||
|
$display
|
||||||
|
.append('<li>[ "' + values.join('", "') + '" ]</li>')
|
||||||
|
.find('li:first').remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
}); </script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<div id="banner">
|
||||||
|
<h1>table<em>sorter</em></h1>
|
||||||
|
<h2>currentSort Widget</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>Added <span class="verison">v2.27.0</span>.
|
||||||
|
<ul>
|
||||||
|
<li>This widget has only one option - <code>currentSort_callback</code> - which provides the following parameters.
|
||||||
|
<ul>
|
||||||
|
<li><code>config</code> - The <code>table.config</code> object.</li>
|
||||||
|
<li><code>values</code> - An array of the current sort using the <code>$.tablesorter.currentSortLanguage</code> settings.</li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
|
<li>Change the language of the output for this widget by modifying the <code>$.tablesorter.currentSortLanguage</code> object.</li>
|
||||||
|
<li>The current sort array is also saved to <code>config.currentSort</code>.</li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<h1>Current Sort</h1>
|
||||||
|
<ul id="display">
|
||||||
|
<li>The last three sort arrays will appear here.</li>
|
||||||
|
<li> </li>
|
||||||
|
<li> </li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<h1>Demo</h1>
|
||||||
|
<table class="tablesorter">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>AlphaNumeric Sort</th>
|
||||||
|
<th>Currency</th>
|
||||||
|
<th>Alphabetical</th>
|
||||||
|
<th>Sites</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr><td>abc 123</td><td>£10,40</td><td>Koala</td><td>http://www.google.com</td></tr>
|
||||||
|
<tr><td>abc 1</td><td>£234,10</td><td>Ox</td><td>http://www.yahoo.com</td></tr>
|
||||||
|
<tr><td>abc 9</td><td>£10,33</td><td>Girafee</td><td>http://www.facebook.com</td></tr>
|
||||||
|
<tr><td>zyx 24</td><td>£10</td><td>Bison</td><td>http://www.whitehouse.gov/</td></tr>
|
||||||
|
<tr><td>abc 11</td><td>£3,20</td><td>Chimp</td><td>http://www.ucla.edu/</td></tr>
|
||||||
|
<tr><td>abc 2</td><td>£56,10</td><td>Elephant</td><td>http://www.wikipedia.org/</td></tr>
|
||||||
|
<tr><td>abc 9</td><td>£3,20</td><td>Lion</td><td>http://www.nytimes.com/</td></tr>
|
||||||
|
<tr><td>ABC 10</td><td>£87,00</td><td>Zebra</td><td>https://github.com</td></tr>
|
||||||
|
<tr><td>zyx 1</td><td>£99,90</td><td>Koala</td><td>http://www.mit.edu/</td></tr>
|
||||||
|
<tr><td>zyx 12</td><td>£234,10</td><td>Llama</td><td>http://www.nasa.gov/</td></tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<h1>Javascript</h1>
|
||||||
|
<div id="javascript">
|
||||||
|
<pre class="prettyprint lang-javascript"></pre>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -458,6 +458,7 @@
|
|||||||
<li><span class="results">†</span> <a href="example-widget-columns.html">Columns highlight widget</a> (v2.0.17).</li>
|
<li><span class="results">†</span> <a href="example-widget-columns.html">Columns highlight widget</a> (v2.0.17).</li>
|
||||||
<li><a href="example-widget-column-selector.html">Column selector widget</a> (<span class="version">v2.15</span>; <span class="version updated">v2.26.6</span>).</li>
|
<li><a href="example-widget-column-selector.html">Column selector widget</a> (<span class="version">v2.15</span>; <span class="version updated">v2.26.6</span>).</li>
|
||||||
<li><a href="example-widget-editable.html">Content editable widget</a> (v2.9; <span class="version updated">v2.25.5</span>).</li>
|
<li><a href="example-widget-editable.html">Content editable widget</a> (v2.9; <span class="version updated">v2.25.5</span>).</li>
|
||||||
|
<li><a href="example-widget-current-sort.html">Current Sort Widget</a> (<span class="version">v2.27.0</span>).</li>
|
||||||
<li><span class="label label-info">Beta</span> <a href="example-dragtable.html">Dragtable mod</a> - (jQuery UI widget for column reordering [<a class="external" href="http://stackoverflow.com/a/27770224/145346">ref</a>]; <span class="version">v2.24.0</span>).</li>
|
<li><span class="label label-info">Beta</span> <a href="example-dragtable.html">Dragtable mod</a> - (jQuery UI widget for column reordering [<a class="external" href="http://stackoverflow.com/a/27770224/145346">ref</a>]; <span class="version">v2.24.0</span>).</li>
|
||||||
<li><span class="results">†</span> Filter widget (<span class="version updated">v2.27.0</span>):
|
<li><span class="results">†</span> Filter widget (<span class="version updated">v2.27.0</span>):
|
||||||
<ul>
|
<ul>
|
||||||
|
60
js/widgets/widget-currentSort.js
Normal file
60
js/widgets/widget-currentSort.js
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
/*! Widget: currentSort - 7/26/2016 (v2.27.0) *//*
|
||||||
|
* Requires tablesorter v2.8+ and jQuery 1.7+
|
||||||
|
* by Rob Garrison
|
||||||
|
*/
|
||||||
|
;( function( $ ) {
|
||||||
|
'use strict';
|
||||||
|
var ts = $.tablesorter;
|
||||||
|
|
||||||
|
ts.currentSortLanguage = {
|
||||||
|
0: 'asc',
|
||||||
|
1: 'desc',
|
||||||
|
2: 'unsorted'
|
||||||
|
};
|
||||||
|
|
||||||
|
ts.currentSort = {
|
||||||
|
init : function( c ) {
|
||||||
|
c.$table.on( 'sortEnd.tscurrentSort', function() {
|
||||||
|
ts.currentSort.update( this.config );
|
||||||
|
});
|
||||||
|
},
|
||||||
|
update: function( c ) {
|
||||||
|
if ( c ) {
|
||||||
|
var indx,
|
||||||
|
wo = c.widgetOptions,
|
||||||
|
lang = ts.currentSortLanguage,
|
||||||
|
unsort = lang[ 2 ],
|
||||||
|
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill
|
||||||
|
// order = new Array(c.columns).fill(unsort),
|
||||||
|
// the above ES6 will not work in all browsers, so
|
||||||
|
// we're stuck with this messy code to fill the array:
|
||||||
|
order = Array
|
||||||
|
.apply( null, Array( c.columns ) )
|
||||||
|
.map( String.prototype.valueOf, unsort ),
|
||||||
|
sortList = c.sortList,
|
||||||
|
len = sortList.length;
|
||||||
|
for ( indx = 0; indx < len; indx++ ) {
|
||||||
|
order[ sortList[ indx ][ 0 ] ] = lang[ sortList[ indx ][ 1 ] ];
|
||||||
|
}
|
||||||
|
c.currentSort = order;
|
||||||
|
if ( typeof wo.currentSort_callback === 'function' ) {
|
||||||
|
wo.currentSort_callback(c, order);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
ts.addWidget({
|
||||||
|
id: 'currentSort',
|
||||||
|
options: {
|
||||||
|
currentSort_callback : null
|
||||||
|
},
|
||||||
|
init : function( table, thisWidget, c, wo ) {
|
||||||
|
ts.currentSort.init( c, wo );
|
||||||
|
},
|
||||||
|
remove : function( table, c ) {
|
||||||
|
c.$table.off( 'sortEnd.tscurrentSort' );
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
})( jQuery );
|
Loading…
Reference in New Issue
Block a user