Skip to content

Commit

Permalink
Add filter_saveFilters option. See #388
Browse files Browse the repository at this point in the history
  • Loading branch information
Mottie committed Nov 16, 2013
1 parent 3fede9f commit af0a142
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
9 changes: 7 additions & 2 deletions docs/example-widget-filter.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
// jQuery selector string of an element used to reset the filters
filter_reset : 'button.reset',

// Use the $.tablesorter.storage utility to save the most recent filters (default setting is false)
filter_saveFilters : true,

// Delay in milliseconds before the filter widget starts searching; This option prevents searching for
// every character while typing and should make searching large tables faster.
filter_searchDelay : 300,
Expand Down Expand Up @@ -195,6 +198,7 @@ <h3>Flexible client-side table sorting</h3>
<h3><a href="#">Notes</a></h3>
<div>
<ul>
<li>Added &amp; set <code>filter_saveFilters</code> to <code>true</code> (default is <code>false</code>) in this demo (<span class="version">v2.14</span>).</li>
<li>Hover over the grey bar below the table header to open the filter row. Disable this by setting <code>filter_hideFilters</code> option to <code>false</code>.</li>
<li>This widget uses jQuery's <code>.nextUntil()</code> function which is only available is jQuery version 1.4+.</li>
<li>This widget does work with tablesorter v2.0.5.</li>
Expand Down Expand Up @@ -239,6 +243,7 @@ <h3>Filter widget defaults (added inside of tablesorter <code>widgetOptions</cod
<li><code>filter_liveSearch : true</code> - if true, search column content while the user types (with a delay). If false, the user must press enter to start the search. If set to a number, when the length of the input text reaches this minimum length, a search will initiate.</li>
<li><code>filter_onlyAvail : 'filter-onlyAvail'</code> - a header with a select dropdown &amp; this class name will only show available (visible) options within that drop down.</li>
<li><code>filter_reset : null</code> - jQuery selector string of an element used to reset the filters.</li>
<li><code>filter_saveFilters : false</code> - Uses the <code>$.tablesorter.storage</code> utility to save the most recent filters.</li>
<li><code>filter_searchDelay : 300</code> - typing delay in milliseconds before starting a search.</li>
<li><code>filter_serversideFiltering : false</code> - if true, filter will be done server-side. The client-side filtering will be disabled, but the ui and events will still be used.</li>
<li><code>filter_startsWith : false</code> - if true, filter start from the beginning of the cell contents.</li>
Expand Down Expand Up @@ -339,8 +344,8 @@ <h1>Demo</h1>
<hr>

<div id="demo">
<button type="button" data-filter-column="5" data-filter-text="2?%">Saved Search</button> (search the Discount column for &quot;2?%&quot;)<br>
<button type="button" class="reset">Reset Search</button> <!-- targeted by the "filter_reset" option -->
Search <button type="button" data-filter-column="5" data-filter-text="2?%">2?%</button> in the Discount column<br>
<button type="button" class="reset">Reset</button> <!-- targeted by the "filter_reset" option -->

<table class="tablesorter">
<thead>
Expand Down
19 changes: 19 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,8 @@ <h1>Configuration</h1>
filter_onlyAvail : 'filter-onlyAvail',
// jQuery selector string of an element used to reset the filters.
filter_reset : null,
// Use the $.tablesorter.storage utility to save the most recent filters
filter_saveFilters : false,
// typing delay in milliseconds before starting a search.
filter_searchDelay : 300,
// if true, filter start from the beginning of the cell contents.
Expand Down Expand Up @@ -2088,6 +2090,23 @@ <h4>Comparison example</h4>
<td><a href="example-widget-filter-custom.html">Example</a></td>
</tr>

<tr id="widget-filter-savefilters">
<td><a href="#" class="permalink">filter_saveFilters</a></td>
<td>Boolean</td>
<td>false</td>
<td>
Filter widget: If the storage utility is available (included with <code>jquery.tablesorter.widgets.js</code>, the last applied filter is saved to storage (<span class="version">v2.14</span>).
<div class="collapsible">
<br>
Filters saved to local storage (or cookies) will over-ride any default filters within the header data-attribute (set by the <a href="#widget-filter-defaultattrib"><code>filter_defaultAttrib</code> option</a> and be available to the pager before any ajax calls are made.<br>
<br>
To bypass this behavior, clear out the saved filters as follows:
<pre class="prettyprint lang-js">$.tablesorter.storage( $('table'), 'tablesorter-filters', '' );</pre>
</div>
</td>
<td><a href="example-widget-filter-custom.html">Example</a></td>
</tr>

<tr id="widget-filter-searchdelay">
<td><a href="#" class="permalink">filter_searchDelay</a></td>
<td>Numeric</td>
Expand Down
18 changes: 15 additions & 3 deletions js/jquery.tablesorter.widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ ts.addWidget({
filter_liveSearch : true, // if true, search column content while the user types (with a delay)
filter_onlyAvail : 'filter-onlyAvail', // a header with a select dropdown & this class name will only show available (visible) options within the drop down
filter_reset : null, // jQuery selector string of an element used to reset the filters
filter_saveFilters : false, // Use the $.tablesorter.storage utility to save the most recent filters
filter_searchDelay : 300, // typing delay in milliseconds before starting a search
filter_startsWith : false, // if true, filter start from the beginning of the cell contents
filter_useParsedData : false, // filter all data using parsed content
Expand Down Expand Up @@ -624,18 +625,26 @@ ts.filter = {
if (filters.length) {
ts.setFilters(table, filters, true);
}
ts.filter.checkFilters(table, filters);
});
// filter widget initialized
wo.filter_Initialized = true;
c.$table.trigger('filterInit');
ts.filter.checkFilters(table);
},
setDefaults: function(table, c, wo) {
var indx,
filters = [],
columns = c.columns;
for (indx = 0; indx < columns; indx++) {
filters[indx] = c.$headers.filter('[data-column="' + indx + '"]:last').attr(wo.filter_defaultAttrib) || filters[indx];
if (wo.filter_saveFilters && ts.storage) {
filters = ts.storage( table, 'tablesorter-filters' ) || [];
// make sure we're not just saving an empty array
if (filters.join('') === '') { filters = []; }
}
// if not filters saved, then check default settings
if (!filters.length) {
for (indx = 0; indx < columns; indx++) {
filters[indx] = c.$headers.filter('[data-column="' + indx + '"]:last').attr(wo.filter_defaultAttrib) || filters[indx];
}
}
$(table).data('lastSearch', filters);
return filters;
Expand Down Expand Up @@ -915,6 +924,9 @@ ts.filter = {
c.lastCombinedFilter = combinedFilters; // save last search
c.lastSearch = filters;
c.$table.data('lastSearch', filters);
if (wo.filter_saveFilters && ts.storage) {
ts.storage( table, 'tablesorter-filters', filters );
}
if (c.debug) {
ts.benchmark("Completed filter widget search", time);
}
Expand Down

0 comments on commit af0a142

Please sign in to comment.