diff --git a/docs/example-widget-sort-to-hash.html b/docs/example-widget-sort-to-hash.html new file mode 100644 index 00000000..e1a9c189 --- /dev/null +++ b/docs/example-widget-sort-to-hash.html @@ -0,0 +1,173 @@ + + + + + jQuery plugin: Tablesorter 2.0 - Sort2Hash Widget (Beta) + + + + + + + + + + + + + + + + + + + + + +
+ +

+
+ +
+ +

Notes

+
+
    +
  • Added v2.22.4. Instead of using the saveSort widget, this widget updates the hash tag to allow saving & sharing a sort applied to a tablesorter table.
  • +
  • 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.
  • +
  • This widget requires jQuery version 1.7+.
  • +
  • This widget does NOT work with tablesorter v2.0.5.
  • +
+
+ +

Options

+
+

Sort2Hash widget default options (added inside of tablesorter widgetOptions)

+
+ TIP! Click on the link in the option column to reveal full details (or toggle|show|hide all) or double click to update the browser location. +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
OptionDefaultDescription
sort2hash_hash'#' + The hash should always be there. This option was added to allow setting extra hash parameters and/or hashbang or whatever. +
',' + Change the hash separator using this option. There are some limitations. +
+
+ In the location hash, the sort parameters are added as &tableID=column,direction, ... ,column,direction (no spaces). This option allows changing the column-direction separator, a comma by default, into the chosen separator. +

*NOTE* Do not set this option to use a hash (#), ampersand (&) or equal sign (=) as it will interfere with how the hash parameters are set up.

+
+
null + Set an ID here to override the table id attribute. +
+
+ In the location hash, the sort parameters are added as &tableID=column,direction, ... ,column,direction (no spaces). The tableID is set by this option. +

This option setting is prioritized over the actual table ID attribute. If neither are set, the tableID will be set as the table's zero-based index on the page.

+
sort2Hash_tableID > table.id attribute > table index
+
+
+
+ +
+ +

Demo

+
+ + + + + + + + + + + + + + + + + +
First NameLast NameAgeTotalDiscountDate
PeterParker28$9.9920%Jul 6, 2006 8:14 AM
JohnHood33$19.9925%Dec 10, 2002 5:14 AM
ClarkKent18$15.8944%Jan 12, 2003 11:14 AM
BruceAlmighty45$153.1944%Jan 18, 2001 9:12 AM
BruceEvans22$13.1911%Jan 18, 2007 9:12 AM
+ + + + + + + + + + + + + + + + + + + +
First NameLast NameAgeTotalDiscountDate
PeterParker28$9.9920%Jul 6, 2006 8:14 AM
JohnHood33$19.9925%Dec 10, 2002 5:14 AM
ClarkKent18$15.8944%Jan 12, 2003 11:14 AM
BruceAlmighty45$153.1944%Jan 18, 2001 9:12 AM
BruceEvans22$13.1911%Jan 18, 2007 9:12 AM
+
+ +

Javascript

+
+

+	
+ +

HTML

+
+

+	
+ +
+ + + diff --git a/docs/index.html b/docs/index.html index aa4591b1..1464f27b 100644 --- a/docs/index.html +++ b/docs/index.html @@ -519,6 +519,7 @@
  • Resizable columns widget (v2.0.23.1; v2.22.0)
  • Save sort widget (v2.0.27)
  • Scroller widget (v2.9; v2.22.0).
  • +
  • Beta Sort-to-hash widget (v2.22.4)
  • Beta Sort tbodies widget (v2.22.2).
  • Static row widget (v2.16; v2.19.1).
  • diff --git a/docs/js/docs.js b/docs/js/docs.js index 6a60185e..5dcebc9f 100644 --- a/docs/js/docs.js +++ b/docs/js/docs.js @@ -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(); diff --git a/js/widgets/widget-sort2Hash.js b/js/widgets/widget-sort2Hash.js new file mode 100644 index 00000000..003f483a --- /dev/null +++ b/js/widgets/widget-sort2Hash.js @@ -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);