Add 'Default Filters' and 'Clear Filters' buttons to new grid filters form.

This commit is contained in:
Lance Edgar 2015-08-14 18:53:39 -05:00
parent d2b065a8fc
commit d57f5169b0
4 changed files with 39 additions and 1 deletions

View file

@ -155,9 +155,14 @@ class Grid(object):
settings['filter.{0}.verb'.format(filtr.key)] = filtr.default_verb
settings['filter.{0}.value'.format(filtr.key)] = filtr.default_value
# If request contains instruction to reset to default filters, then we
# can skip the rest of the request/session checks.
if self.request.GET.get('reset-to-default-filters') == 'true':
pass
# If request has filter settings, grab those, then grab sort/pager
# settings from request or session.
if self.request_has_settings('filter'):
elif self.request_has_settings('filter'):
self.update_filter_settings(settings, 'request')
if self.request_has_settings('sort'):
self.update_sort_settings(settings, 'request')

View file

@ -19,11 +19,15 @@
this.filters = this.element.find('.newfilters');
this.add_filter = this.filters.find('#add-filter');
this.apply_filters = this.filters.find('#apply-filters');
this.default_filters = this.filters.find('#default-filters');
this.clear_filters = this.filters.find('#clear-filters');
this.grid = this.element.find('.newgrid');
// Enhance filters etc.
this.filters.find('.filter').gridfilter();
this.apply_filters.button('option', 'icons', {primary: 'ui-icon-search'});
this.default_filters.button('option', 'icons', {primary: 'ui-icon-home'});
this.clear_filters.button('option', 'icons', {primary: 'ui-icon-trash'});
if (! this.filters.find('.active:checked').length) {
this.apply_filters.button('disable');
}
@ -79,6 +83,27 @@
return false;
});
// When user clicks Default Filters button, refresh page with
// instructions for the server to reset filters to default settings.
this.default_filters.click(function() {
var form = that.filters.find('form');
form.off('submit');
form.append('<input type="hidden" name="reset-to-default-filters" value="true" />');
that.element.mask("Refreshing data...");
form.submit();
});
// When user clicks Clear Filters button, deactivate all filters
// and refresh the grid.
this.clear_filters.click(function() {
that.filters.find('.filter').each(function() {
if ($(this).gridfilter('active')) {
$(this).gridfilter('active', false);
}
});
that.filters.find('form').submit();
});
// Refresh data when user clicks a sortable column header.
this.element.on('click', 'thead th.sortable a', function() {
var th = $(this).parent();

View file

@ -25,6 +25,8 @@
<option value="${filtr.key}"${' disabled="disabled"' if filtr.active else ''|n}>${filtr.label}</option>
% endfor
</select>
${form.tag('button', type='button', id='default-filters', c="Default Filters")}
${form.tag('button', type='button', id='clear-filters', c="Clear Filters")}
</div>
${form.end()}

View file

@ -71,6 +71,12 @@ class MasterView(View):
returns the full page.
"""
grid = self.make_grid()
# If user just refreshed the page with a reset instruction, issue a
# redirect in order to clear out the query string.
if self.request.GET.get('reset-to-default-filters') == 'true':
return self.redirect(self.request.current_route_url(_query=None))
if self.request.params.get('partial'):
self.request.response.content_type = b'text/html'
self.request.response.text = grid.render_grid()