Sort2Hash widget added

See http://stackoverflow.com/q/31377639/145346
This commit is contained in:
Mottie 2015-07-13 12:41:18 -05:00
parent 2833b9b639
commit 2967e6196c
4 changed files with 259 additions and 1 deletions

View File

@ -0,0 +1,173 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery plugin: Tablesorter 2.0 - Sort2Hash Widget (Beta)</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">
<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-sort2Hash.js"></script>
<script id="js">$(function() {
$( 'table' ).tablesorter({
theme: 'blue',
widgets: [ 'zebra', 'sort2Hash' ],
widgetOptions : {
// hash prefix
sort2Hash_hash : '#',
// don't '#' or '=' here
sort2Hash_separator : ',',
// this option > table ID > table index on page
sort2Hash_tableId : null
}
});
});</script>
</head>
<body>
<div id="banner">
<h1>table<em>sorter</em></h1>
<h2>Sort2Hash Widget (Beta)</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><a href="#">Notes</a></h3>
<div>
<ul>
<li>Added <span class="version">v2.22.4</span>. Instead of using the <a href="example-widget-savesort.html">saveSort</a> widget, this widget updates the hash tag to allow saving &amp; sharing a sort applied to a tablesorter table.</li>
<li>Sort the tables in the demo below. Notice the changes made to the location hash, then reload the page to have the hash applied to the tables.</li>
<li>This widget requires jQuery version 1.7+.</li>
<li>This widget does NOT work with tablesorter v2.0.5.</li>
</ul>
</div>
<h3><a href="#">Options</a></h3>
<div>
<h3>Sort2Hash widget default options (added inside of tablesorter <code>widgetOptions</code>)</h3>
<div class="tip">
<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="tablesorter-blue options">
<thead>
<tr><th>Option</th><th>Default</th><th>Description</th></tr>
</thead>
<tbody>
<tr id="sort2hash_hash">
<td><span class="permalink">sort2hash_hash</span></td>
<td><code>'#'</code></td>
<td>
The hash should always be there. This option was added to allow setting extra hash parameters and/or hashbang or whatever.
</td>
</tr>
<tr id="sort2hash_separator">
<td><a href="#" class="permalink">sort2Hash_separator</a></td>
<td><code>','</code></td>
<td>
Change the hash separator using this option. There are some limitations.
<div class="collapsible">
<br>
In the location hash, the sort parameters are added as <code>&tableID=column,direction, ... ,column,direction</code> (no spaces). This option allows changing the column-direction separator, a comma by default, into the chosen separator.
<p><span class="label alert">*NOTE*</span> Do not set this option to use a hash (<code>#</code>), ampersand (<code>&</code>) or equal sign (<code>=</code>) as it will interfere with how the hash parameters are set up.</p>
</div>
</td>
</tr>
<tr id="sort2hash_tableid">
<td><a href="#" class="permalink">sort2Hash_tableId</a></td>
<td><code>null</code></td>
<td>
Set an ID here to override the table id attribute.
<div class="collapsible">
<br>
In the location hash, the sort parameters are added as <code>&tableID=column,direction, ... ,column,direction</code> (no spaces). The <code>tableID</code> is set by this option.
<p>This option setting is prioritized over the actual table ID attribute. If neither are set, the <code>tableID</code> will be set as the table's zero-based index on the page.</p>
<pre class="prettyprint">sort2Hash_tableID &gt; table.id attribute &gt; table index</pre>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<h1>Demo</h1>
<div id="demo"><table>
<thead>
<tr>
<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>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>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>Clark</td><td>Kent</td><td>18</td><td>$15.89</td><td>44%</td><td>Jan 12, 2003 11:14 AM</td></tr>
<tr><td>Bruce</td><td>Almighty</td><td>45</td><td>$153.19</td><td>44%</td><td>Jan 18, 2001 9:12 AM</td></tr>
<tr><td>Bruce</td><td>Evans</td><td>22</td><td>$13.19</td><td>11%</td><td>Jan 18, 2007 9:12 AM</td></tr>
</tbody>
</table>
<table id="second">
<thead>
<tr>
<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>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>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>Clark</td><td>Kent</td><td>18</td><td>$15.89</td><td>44%</td><td>Jan 12, 2003 11:14 AM</td></tr>
<tr><td>Bruce</td><td>Almighty</td><td>45</td><td>$153.19</td><td>44%</td><td>Jan 18, 2001 9:12 AM</td></tr>
<tr><td>Bruce</td><td>Evans</td><td>22</td><td>$13.19</td><td>11%</td><td>Jan 18, 2007 9:12 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

@ -519,6 +519,7 @@
<li><span class="results">&dagger;</span> <a href="example-widget-resizable.html">Resizable columns widget</a> (v2.0.23.1; <span class="version updated">v2.22.0</span>)</li>
<li><span class="results">&dagger;</span> <a href="example-widget-savesort.html">Save sort widget</a> (v2.0.27)</li>
<li><a href="example-widget-scroller.html">Scroller widget</a> (<span class="version">v2.9</span>; <span class="version updated">v2.22.0</span>).</li>
<li><span class="label label-info">Beta</span> <a href="example-widget-sort-to-hash.html">Sort-to-hash widget</a> (<span class="version">v2.22.4</span>)</li>
<li><span class="label label-info">Beta</span> <a href="example-widget-sort-tbodies.html">Sort tbodies widget</a> (<span class="version">v2.22.2</span>).</li>
<li><a href="example-widget-static-row.html">Static row widget</a> (<span class="version">v2.16</span>; <span class="version updated">v2.19.1</span>).</li>

View File

@ -138,6 +138,11 @@
ui.newPanel.find('table').trigger('applyWidgets');
}
});
console.log( hash );
// hash is not a jQuery selector
if ( /[=,]/.test(hash) ) {
return false;
}
// open parent accordion of nested accordion
if ( $this.find(hash).length && !$this.children(hash).length ) {
// div should have an id of ui-accordion-#-panel-#
@ -170,7 +175,7 @@
function showProperty(){
var prop, $t, wo, stickyHt,
h = window.location.hash;
if (h) {
if (h && !/[=,]/.test(h)) {
prop = $(h);
if (prop.length && !/h3|a|table/i.test(prop[0].nodeName)) {
prop.find('.collapsible').show();

View File

@ -0,0 +1,79 @@
/*! Widget: sort2Hash */
;( function( $ ) {
'use strict';
var ts = $.tablesorter || {},
s2h = {
init : function( c, wo ) {
var arry, indx, len, column, direction,
sort = s2h.getSort( c, wo );
if (sort) {
arry = sort.split( wo.sort2Hash_separator );
len = arry.length;
sort = [];
for ( indx = 0; indx < len; indx++ ) {
column = arry[ indx++ ];
direction = arry[ indx ];
// ignore unpaired values
if ( typeof direction !== 'undefined' ) {
sort.push( [ column, direction ] );
}
}
if ( sort.length ) {
c.sortList = sort;
}
}
c.$table.on( 'sortEnd.sort2hash', function() {
s2h.setHash( c, wo );
});
},
getTableId : function( c, wo ) {
// option > table id > table index on page
return wo.sort2Hash_tableId ||
c.table.id ||
'table' + $( 'table' ).index( c.$table );
},
getSort : function( c, wo, clean ) {
// modified original code from http://www.netlobo.com/url_query_string_javascript.html
var name = s2h.getTableId( c, wo ).replace( /[\[]/, '\\[' ).replace( /[\]]/, '\\]' ),
sort = ( new RegExp( '[\\#&]' + name + '=([^&]*)' ) ).exec( window.location.hash );
if ( sort === null ) {
return '';
} else {
if ( clean ) {
window.location.hash = window.location.hash.replace( '&' + name + '=' + sort[ 1 ], '' );
}
return sort[ 1 ];
}
},
setHash : function( c, wo ) {
var hash, indx,
arry = [],
tableId = s2h.getTableId( c, wo ) + '=',
sort = c.sortList || [],
len = sort.length;
if ( len ) {
s2h.getSort( c, wo, true ); // remove hash
window.location.hash += ( window.location.hash.length ? '' : wo.sort2Hash_hash ) +
'&' + tableId +
// flatten array, then join with separator
[].concat.apply( [], sort ).join( wo.sort2Hash_separator );
}
}
};
ts.addWidget({
id: 'sort2Hash',
options: {
sort2Hash_hash : '#', // hash prefix
sort2Hash_separator : '-', // don't '#' or '=' here
sort2Hash_tableId : null // this option > table ID > table index on page
},
init: function(table, thisWidget, c, wo) {
s2h.init( c, wo );
},
remove: function(table, c) {
c.$table.off( 'sortEnd.sort2hash' );
}
});
})(jQuery);