Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter save widget #178

Closed
Benni-chan opened this issue Nov 21, 2012 · 2 comments
Closed

filter save widget #178

Benni-chan opened this issue Nov 21, 2012 · 2 comments

Comments

@Benni-chan
Copy link

Hi,
since there was no way to save a filter (like the savesort widget), i wrote my own little widget. maybe it is of some use to you.

$.tablesorter.addWidget({
    id: 'FilterSave',
    format: function(table, init){
        var sl, time, c = table.config,
            wo = c.widgetOptions,
            ss = wo.FilterSave !== false; // make FilterSave active/inactive; default to true

        var count_filters = $(table).find('input.tablesorter-filter').length;
        var filter = new Array();
        for (var i=0; i<count_filters;i++)
        {
            filter[i]=$(table).find('input.tablesorter-filter').eq(i).val();
        }

        if (c.debug){
            time = new Date();
        }
        if ($(table).hasClass('hasSaveFilter')){
            if (ss && table.hasInitialized && $.tablesorter.storage){
                $.tablesorter.storage( table, 'tablesorter-savefilter', filter );
                // console.log("saved"+filter)
                if (c.debug){
                    $.tablesorter.benchmark('FilterSave widget: Saving last filter: ' + filter, time);
                }
            }
        } else if (table.hasInitialized){
            // set table sort on initial run of the widget
            $(table).addClass('hasSaveFilter');
            filter = '';
            // get data
            if ($.tablesorter.storage){
                fl = $.tablesorter.storage( table, 'tablesorter-savefilter' );
                filter = (fl && $.isArray(fl)) ? fl : '';
                if (c.debug){
                    $.tablesorter.benchmark('FilterSave: Last filter loaded: "' + filter + '"', time);
                }
            }
            // init is true when widget init is run, this will run this widget before all other widgets have initialized
            // this method allows using this widget in the original tablesorter plugin; but then it will run all widgets twice.
            if (filter && filter.length > 0)
            {
                $(table).trigger('search', [filter]);
            }
        }
    },
    remove: function(table, c, wo){
        // clear storage
        $.tablesorter.storage( table, 'tablesorter-savefilter', '' );
    }
});
@Mottie
Copy link
Owner

Mottie commented Nov 24, 2012

Thanks @Benni-chan!

I'll work on including this in version 3 :)

@Mottie Mottie mentioned this issue Nov 24, 2012
22 tasks
@Mottie
Copy link
Owner

Mottie commented Nov 24, 2013

Added filter_saveFilters widget option in version 2.14.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants