From 39a1772b257f3179d159a02e66cd704754c8bade Mon Sep 17 00:00:00 2001 From: Rob Garrison Date: Tue, 11 Apr 2017 14:55:27 -0500 Subject: [PATCH] Filter: Add equalFilters function --- js/widgets/widget-filter.js | 20 +++++++++++++++++--- testing/testing-widgets.js | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/js/widgets/widget-filter.js b/js/widgets/widget-filter.js index a4d201a6..5d39b511 100644 --- a/js/widgets/widget-filter.js +++ b/js/widgets/widget-filter.js @@ -885,6 +885,19 @@ tsf.checkFilters( table, filter, skipFirst ); } }, + equalFilters: function (c, filter1, filter2) { + var indx, + f1 = [], + f2 = [], + len = c.columns + 1; // add one to include anyMatch filter + filter1 = $.isArray(filter1) ? filter1 : []; + filter2 = $.isArray(filter2) ? filter2 : []; + for (indx = 0; indx < len; indx++) { + f1[indx] = filter1[indx] || ''; + f2[indx] = filter2[indx] || ''; + } + return f1.join(',') === f2.join(','); + }, checkFilters: function( table, filter, skipFirst ) { var c = table.config, wo = c.widgetOptions, @@ -917,7 +930,7 @@ } // return if the last search is the same; but filter === false when updating the search // see example-widget-filter.html filter toggle buttons - if ( c.lastSearch.join(',') === currentFilters.join(',') && filter !== false ) { + if ( tsf.equalFilters(c, c.lastSearch, currentFilters) && filter !== false ) { return; } else if ( filter === false ) { // force filter refresh @@ -1266,7 +1279,7 @@ }, findRows: function( table, filters, currentFilters ) { if ( - table.config.lastSearch.join(',') === ( currentFilters || [] ).join(',') || + tsf.equalFilters(table.config, table.config.lastSearch, currentFilters) || !table.config.widgetOptions.filter_initialized ) { return; @@ -1811,7 +1824,8 @@ if ( ( getRaw !== true && wo && !wo.filter_columnFilters ) || // setFilters called, but last search is exactly the same as the current // fixes issue #733 & #903 where calling update causes the input values to reset - ( $.isArray(setFilters) && setFilters.join(',') === c.lastSearch.join(',') ) ) { + ( $.isArray(setFilters) && tsf.equalFilters(c, setFilters, c.lastSearch) ) + ) { return $( table ).data( 'lastSearch' ) || []; } if ( c ) { diff --git a/testing/testing-widgets.js b/testing/testing-widgets.js index 2d060a87..2ec9f4b2 100644 --- a/testing/testing-widgets.js +++ b/testing/testing-widgets.js @@ -192,6 +192,25 @@ jQuery(function($){ assert.deepEqual( processFilters( filters, false ), results ); }); + QUnit.test( 'Filter comparison', function(assert) { + assert.expect(10); + var undef, + c = { columns: 10 }, // psuedo table.config + compare = this.ts.filter.equalFilters; + + assert.equal( compare( c, [], [] ), true, 'two empty arrays' ); + assert.equal( compare( c, [], '' ), true, 'empty array + empty string' ); + assert.equal( compare( c, '', [] ), true, 'empty string + empty array' ); + assert.equal( compare( c, ['', '', ''], [] ), true, 'empty array len 3 vs len 0' ); + assert.equal( compare( c, ['1', undef, ''], ['1'] ), true, 'equal but diff len' ); + assert.equal( compare( c, [undef, '1', ''], [undef, '1'] ), true, 'equal but diff len' ); + assert.equal( compare( c, [] ), true, 'undefined second filter' ); + assert.equal( compare( c, ['', undef] ), true, 'undefined second filter' ); + + assert.equal( compare( c, ['1', '', ''], ['', '1', ''] ), false, 'same value diff position' ); + assert.equal( compare( c, [undef, '1', ''], ['', undef, '1'] ), false, 'same value diff position' ); + }); + QUnit.test( 'Filter searches', function(assert) { var ts = this.ts, c = this.c,