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

Default filters on load #321

Closed
TheSin- opened this issue Jun 3, 2013 · 9 comments
Closed

Default filters on load #321

TheSin- opened this issue Jun 3, 2013 · 9 comments

Comments

@TheSin-
Copy link
Collaborator

TheSin- commented Jun 3, 2013

I'm not sure if there is a place for this and I just missed it, but I'd like to be able to have some default filter values on load.

like for a Datepicker, it's has a from and to field but they don't actually show up in the boxes nor does it's filter on load.

If there an example of something that has a default value and filters on load?

@Mottie
Copy link
Owner

Mottie commented Jun 3, 2013

Ahh, yes, I thought I had this on my to do list... wasn't there another issue addressing this?

@TheSin-
Copy link
Collaborator Author

TheSin- commented Jun 3, 2013

not that I could find, nor could I find an example setting default values for any filter really, I'm in the code now looking, specifically at uiDatepicker, so far I see mention of minDate and I assume maxDate someplace but I don't see the options to set those. val for from is being set in closeFrom, can't see where the To val() gets set yet. And closeFrom in bound to stickyHeadersInit, so it "should" be getting set but isn't, now this is just one of the filters I haven't looked at others yet but having a default filter sure woul deb nice so I'm going to keep searching.

EDIT: Actually closeForm is bound to onClose in stickyHeadersInit, so it isn't run on load at all.

@TheSin-
Copy link
Collaborator Author

TheSin- commented Jun 3, 2013

okay adding at line 470

                $cell.find('.dateFrom').datepicker('setDate', o.from);
                $cell.find('.dateTo').datepicker('setDate', o.to);

and at line 478

                        $shcell.find('.dateFrom').datepicker('setDate', o.from);
                        $shcell.find('.dateTo').datepicker('setDate', o.to);

but this just sets the inputs, it doesn't start the filter event.

EDIT: trying to work with adding

$cell.closest('table').trigger('search', false);

but no avail

EDIT2: tried with, but the alert tells me this is still too soon??, not too soon the val is there :\ Why won't search trigger?

                // on load run filters in case there are default values
                $cell.closest('table').bind('tablesorter-initialized', function(){
                        if (o.to || o.from) {
                                alert($(this).attr('id') + ' should be filtering now');
                                $(this).trigger('search', false);
                        }
                });

EDIT3: Add a bind to search, and it is searching, so maybe still to early? What is after tablesorter-initialized?

@TheSin-
Copy link
Collaborator Author

TheSin- commented Jun 3, 2013

Just for completeness, I also updated

                // on reset
                $cell.closest('table').bind('filterReset', function(){
                        $cell.find('.dateFrom, .dateTo').val('');
                        if ($shcell.length) {
                                $shcell.find('.dateFrom, .dateTo').val('');
                        }
                });

to

                // on reset
                $cell.closest('table').bind('filterReset', function(){
                        $cell.find('.dateFrom, .dateTo').val('');
                        $cell.find('.dateFrom').datepicker('setDate', o.from);
                        $cell.find('.dateTo').datepicker('setDate', o.to);
                        if ($shcell.length) {
                                $shcell.find('.dateFrom, .dateTo').val('');
                                $shcell.find('.dateFrom').datepicker('setDate', o.from);
                                $shcell.find('.dateTo').datepicker('setDate', o.to);
                        }
                });

@TheSin-
Copy link
Collaborator Author

TheSin- commented Jun 3, 2013

attempted using $(window).load which is MUCH later then tablesorter-initialized, but still no sorting :\

if I click in the filter and just click off with out changing a thing it works, it triggers the search and uses the values.

but this doesn't work

                // on load run filters in case there are default values
                $(window).load(function() {
                        if (o.to || o.from) 
                                $cell.closest('table').trigger('search', false);
                });

Oh wait I'm not setting the hidden input I bet that is the issue :\

@TheSin-
Copy link
Collaborator Author

TheSin- commented Jun 3, 2013

Finally working, Let me know what you think of it Rob when you get a chance

                // on load run filters in case there are default values
                $cell.closest('table').bind('tablesorter-initialized', function(){
                        if (o.to || o.from)
                                $cell.closest('table').find('.hasDatepicker').focus().blur(function() {
                                        // Bug in jquery-ui, doesn't hide on blur but should
                                        $(this).datepicker('hide');
                                });
                });

@Mottie
Copy link
Owner

Mottie commented Jun 3, 2013

Working on it... but I'm also trying to include all the other filters, as well as the filter formatter.

@TheSin-
Copy link
Collaborator Author

TheSin- commented Jun 3, 2013

yeah this is was just quick and dirty as a proof of concept, I like to make sure I have the right idea before i get too deep ;) Hope my work approve helps at least ;)

@Mottie
Copy link
Owner

Mottie commented Jun 5, 2013

I just pushed v2.10.8 which fixes the filter_formatter functions to now properly set the default values.

I've also included a new option filter_defaultAttrib which contains the data-attribute name to find the filter's default value; it is data-value by default. So in the custom filter demo I added this to the Age column:

<th data-value="<30">Age</th>

Also, these data attributes will over-ride the default values set in the filter_formatter.

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