Search code examples
jquerywidgettablesorter

Need to sort a table and get the url and share with others which shows the sorted result


I have a table which I am sorting on multiple columns (using shift+click) using jquery.tablesorter.combined.js and using saveSort widget to save the sort.

is there any way i can sort this in server side and get the url so that I can copy & paste the url and share this result with others via URL ? Loading this URL should give me the sorted table as it is.

Please let me know

Thanks in advance


Solution

  • I ended up making a widget to add the current sort to the hash. It has not be thoroughly tested and does not appear to work in this demo because of the inability to set the hash on the inner frame. It does work in a full page demo.

    /*! 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 ];
                    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 ) {
            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);
    
    $(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
            }
        });
    
    });
    

    I'll include this widget in the next update of my fork of tablesorter.