From fc9a64e693c4679ac6fd13662b2230c330d78c6c Mon Sep 17 00:00:00 2001 From: Mottie Date: Sun, 12 May 2013 17:32:39 -0500 Subject: [PATCH] added filter_onlyAvail option; fixes #292 --- docs/example-widget-filter-custom.html | 64 +++++++++++++++++--------- docs/example-widget-filter.html | 28 +++++------ docs/index.html | 16 +++++++ js/jquery.tablesorter.widgets.js | 22 ++++++--- 4 files changed, 88 insertions(+), 42 deletions(-) diff --git a/docs/example-widget-filter-custom.html b/docs/example-widget-filter-custom.html index 3dc8670c..bdfd638f 100644 --- a/docs/example-widget-filter-custom.html +++ b/docs/example-widget-filter-custom.html @@ -165,24 +165,25 @@ -

Default Select ("First Name" column)

+

Default Select ("First Name" column)

-

Custom Filter Function ("Last Name" column)

+

Custom Filter Function ("Last Name" column)

-

Custom Select ("City" or "Total" column)

+

Custom Select ("City" or "Total" column)

+
+ +

Default Select showing only available options ("Discount" column)

+
+
@@ -253,7 +271,7 @@

Demo

- false (toggle "filter-match" class on First Name column)
+ false (toggle "filter-match" class on First Name column)
@@ -266,7 +284,7 @@ - + diff --git a/docs/example-widget-filter.html b/docs/example-widget-filter.html index d86b6f6d..83635edf 100644 --- a/docs/example-widget-filter.html +++ b/docs/example-widget-filter.html @@ -182,16 +182,16 @@ $(function(){
City Age TotalDiscountDiscount Date
- - - - - + + + + + - - - - + + + +
Type (1)DescriptionExample
textAny text entered in the filter will match text found within the columnabc (finds "abc", "abcd", "abcde", etc)
"To exactly match the search query, add a quote, apostrophe or equal sign to the beginning and/or end of the queryabc" or abc= (exactly match "abc")
?Wildcard for a single, non-space character.J?n (finds "Jan" and "Jun", but not "Joan")
*Wildcard for zero or more non-space characters.B*k (matches "Black" and "Book")
/\d/Add any regex to the query to use in the query/b[aeiou]g/i (finds "bag", "beg", "BIG", "Bug", etc)
textAny text entered in the filter will match text found within the columnabc (finds "abc", "abcd", "abcde", etc)
"To exactly match the search query, add a quote, apostrophe or equal sign to the beginning and/or end of the queryabc" or abc= (exactly match "abc")
?Wildcard for a single, non-space character.J?n (finds "Jan" and "Jun", but not "Joan")
*Wildcard for zero or more non-space characters.B*k (matches "Black" and "Book")
/\d/Add any regex to the query to use in the query/b[aeiou]g/i (finds "bag", "beg", "BIG", "Bug", etc)
< <= >= >Find alphabetical or numerical values less than or greater than or equal to the filtered query (2)>= 10 (find values greater than or equal to 10)
!Not operator. Filter the column with content that do not match the query.!fe (hide rows with "female" in that column, but shows rows with "male")
 &&  or  AND Logical "and". Filter the column for content that matches text from either side of the operator.box && bat (matches a column cell that contains both "box" and "bat")
| or  OR Logical "or" (Vertical bar). Filter the column for content that matches text from either side of the bar.box|bat (matches a column cell with either "box" or "bat")
 -  or  to Find a range of values. Make sure there is a space before and after the dash (or the word "to") (2).10 - 30 or 10 to 30 (match values between 10 and 30)
!Not operator. Filter the column with content that do not match the query.!fe (hide rows with "female" in that column, but shows rows with "male")
 &&  or  AND Logical "and". Filter the column for content that matches text from either side of the operator.box && bat (matches a column cell that contains both "box" and "bat")
| or  OR Logical "or" (Vertical bar). Filter the column for content that matches text from either side of the bar.box|bat (matches a column cell with either "box" or "bat")
 -  or  to Find a range of values. Make sure there is a space before and after the dash (or the word "to") (2).10 - 30 or 10 to 30 (match values between 10 and 30)
(1) You cannot combine these operators with each other (except for the wildcards).
@@ -213,9 +213,10 @@ $(function(){
  • filter_hideFilters : false - if true, filters are hidden initially, but can be revealed by clicking on the filter icon.
  • filter_ignoreCase : true - if true, make all searches case-insensitive.
  • filter_liveSearch : true - 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.
  • +
  • filter_onlyAvail : 'filter-onlyAvail' - a header with a select dropdown & this class name will only show available (visible) options within that drop down.
  • filter_reset : null - jQuery selector string of an element used to reset the filters.
  • filter_searchDelay : 300 - typing delay in milliseconds before starting a search.
  • -
  • filter_serversideFiltering : false - if true, filter will be done server-side. The client-side filtering will be disabled, but the ui and events will still be used..
  • +
  • filter_serversideFiltering : false - if true, filter will be done server-side. The client-side filtering will be disabled, but the ui and events will still be used.
  • filter_startsWith : false - if true, filter start from the beginning of the cell contents.
  • filter_useParsedData : false - filter all data using parsed content.
  • @@ -227,8 +228,9 @@ $(function(){
    @@ -243,7 +245,7 @@ $(function(){

    search

    With this method, you can pass an array of filter values: -
    // apply "2?%" filter to the fifth column (zero-based index)
    +			
    // apply "2?%" filter to the fifth column (zero-based index)
     var columns = [];
     columns[5] = '2?%';
     // or define the array this way var columns = [ '', '', '', '', '', '2?%' ]
    @@ -293,7 +295,7 @@ $.tablesorter.setFilters( $('table'), [ '', '', '', '', '', '2?%' ], true );
     
     	
    - (search the Discount column for "2?%")
    + (search the Discount column for "2?%")
    diff --git a/docs/index.html b/docs/index.html index 6683102c..26d97e10 100644 --- a/docs/index.html +++ b/docs/index.html @@ -1307,6 +1307,8 @@ $(function(){ filter_ignoreCase : true, // if true, search column content while the user types (with a delay) filter_liveSearch : true, + // a selector in a header with this class name will only show selected options in the drop down + filter_onlyAvail : 'filter-onlyAvail', // jQuery selector string of an element used to reset the filters. filter_reset : null, // typing delay in milliseconds before starting a search. @@ -1837,6 +1839,20 @@ $(function(){ + + + + + + + + diff --git a/js/jquery.tablesorter.widgets.js b/js/jquery.tablesorter.widgets.js index 48217e9a..774c7f93 100644 --- a/js/jquery.tablesorter.widgets.js +++ b/js/jquery.tablesorter.widgets.js @@ -332,6 +332,7 @@ ts.addWidget({ filter_hideFilters : false, // collapse filter row when mouse leaves the area filter_ignoreCase : true, // if true, make all searches case-insensitive 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_searchDelay : 300, // typing delay in milliseconds before starting a search filter_startsWith : false, // if true, filter start from the beginning of the cell contents @@ -522,14 +523,16 @@ ts.addWidget({ $t.trigger('applyWidgets'); // make sure zebra widget is applied $t.trigger('filterEnd'); }, - buildSelect = function(i, updating){ - var o, arry = []; + buildSelect = function(i, updating, onlyavail){ + var o, t, arry = [], currentVal; i = parseInt(i, 10); o = ''; for (k = 0; k < b.length; k++ ){ l = c.cache[k].row.length; // loop through the rows for (j = 0; j < l; j++){ + // check if has class filtered + if (onlyavail && c.cache[k].row[j][0].className.match(wo.filter_filteredRow)) { continue; } // get non-normalized cell content if (wo.filter_useParsedData){ arry.push( '' + c.cache[k].normalized[j][i] ); @@ -550,10 +553,14 @@ ts.addWidget({ }); arry = (ts.sortText) ? arry.sort(function(a, b){ return ts.sortText(table, a, b, i); }) : arry.sort(true); + // Get curent filter value + currentVal = $t.find('thead').find('select.' + css + '[data-column="' + i + '"]').val(); + // build option list for (k = 0; k < arry.length; k++){ + t = arry[k].replace(/\"/g, """); // replace quotes - fixes #242 & ignore empty strings - see http://stackoverflow.com/q/14990971/145346 - o += arry[k] !== '' ? '' : ''; + o += arry[k] !== '' ? '' : ''; } $t.find('thead').find('select.' + css + '[data-column="' + i + '"]')[ updating ? 'html' : 'append' ](o); }, @@ -565,7 +572,7 @@ ts.addWidget({ if ((t.hasClass('filter-select') || wo.filter_functions && wo.filter_functions[i] === true) && !t.hasClass('filter-false')){ if (!wo.filter_functions) { wo.filter_functions = {}; } wo.filter_functions[i] = true; // make sure this select gets processed by filter_functions - buildSelect(i, updating); + buildSelect(i, updating, t.hasClass(wo.filter_onlyAvail)); } } }; @@ -622,8 +629,8 @@ ts.addWidget({ } } $t - .bind('addRows updateCell update updateRows updateComplete appendCache filterReset search '.split(' ').join('.tsfilter '), function(e, filter){ - if (!/(search|filterReset)/.test(e.type)){ + .bind('addRows updateCell update updateRows updateComplete appendCache filterReset filterEnd search '.split(' ').join('.tsfilter '), function(e, filter){ + if (!/(search|filterReset|filterEnd)/.test(e.type)){ e.stopPropagation(); buildDefault(true); } @@ -633,6 +640,9 @@ ts.addWidget({ // send false argument to force a new search; otherwise if the filter hasn't changed, it will return filter = e.type === 'search' ? filter : e.type === 'updateComplete' ? $t.data('lastSearch') : ''; checkFilters(filter); + if (e.type === 'filterEnd') { + buildDefault(true); + } return false; }) .find('input.' + css).bind('keyup search', function(e, filter){
    filter_onlyAvailString'filter-onlyAvail' + Filter widget: If a header contains a select dropdown and this class name, only the available (visible) options in the column will show (v2.10.1). +
    +
    + This option is useful after one or more columns have been filtered, then the column select filter with this class applied will only show the contents of the column within the dropdown that are currently visible. See the custom filter widget demo "Discount" column for an example (sort the "First Name" column first). +
    +
    Example
    filter_reset String