From b3a7b82a6740f4a9ef96394564be3d70696a162a Mon Sep 17 00:00:00 2001 From: Mottie Date: Tue, 20 Jan 2015 10:51:17 -0600 Subject: [PATCH] ColumnSelector: columns array parameter added to refreshColumnSelector method. Fixes #798 --- docs/example-widget-column-selector.html | 16 ++++- js/widgets/widget-columnSelector.js | 80 +++++++++++++++--------- 2 files changed, 64 insertions(+), 32 deletions(-) diff --git a/docs/example-widget-column-selector.html b/docs/example-widget-column-selector.html index 4f12fb46..4ec3b312 100644 --- a/docs/example-widget-column-selector.html +++ b/docs/example-widget-column-selector.html @@ -182,6 +182,7 @@

Notes

- * Prior to v2.15.6, setting these methods to "false" would remove it from the selector. Now, they must be set to "disable" to remove the column from the selector, this was done to make it more consistent with the columnSelector_columns setting. + * *NOTE* Prior to v2.15.6, setting these methods to "false" would remove it from the selector. Now, they must be set to "disable" to remove the column from the selector, this was done to make it more consistent with the columnSelector_columns setting.
diff --git a/js/widgets/widget-columnSelector.js b/js/widgets/widget-columnSelector.js index aee6ddfb..acabc774 100644 --- a/js/widgets/widget-columnSelector.js +++ b/js/widgets/widget-columnSelector.js @@ -1,4 +1,4 @@ -/* Column Selector/Responsive table widget (beta) for TableSorter - 12/22/2014 (v2.18.4) +/* Column Selector/Responsive table widget (beta) for TableSorter - 1/20/2015 (v2.18.5) * Requires tablesorter v2.8+ and jQuery 1.7+ * by Justin Hallett & Rob Garrison */ @@ -49,11 +49,28 @@ tsColSel = ts.columnSelector = { c.$table .off('refreshColumnSelector' + namespace) - .on('refreshColumnSelector' + namespace, function(){ + .on('refreshColumnSelector' + namespace, function(e, arry){ // make sure we're using current config settings - var c = this.config; - tsColSel.updateBreakpoints(c, c.widgetOptions); - tsColSel.updateCols(c, c.widgetOptions); + var i, + c = this.config, + wo = c.widgetOptions; + // see #798 + if ($.isArray(arry) && c.selector.$container.length) { + // make sure array contains numbers + $.each(arry, function(i,v){ + arry[i] = parseInt(v, 10); + }); + for (i = 0; i < c.columns; i++) { + c.selector.$container + .find('input[data-column=' + i + ']') + .prop('checked', $.inArray( i, arry ) >= 0 ); + } + // set auto to false to allow manual column selection & update columns + tsColSel.updateAuto( c, wo, colSel.$container.find('input[data-column="auto"]').prop('checked', false) ); + } else { + tsColSel.updateBreakpoints(c, wo); + tsColSel.updateCols(c, wo); + } }); }, @@ -91,7 +108,7 @@ tsColSel = ts.columnSelector = { // set default state; storage takes priority colSel.states[colId] = saved && typeof(saved[colId]) !== 'undefined' ? saved[colId] : typeof(wo.columnSelector_columns[colId]) !== 'undefined' ? - wo.columnSelector_columns[colId] : (state === 'true' || !(state === 'false')); + wo.columnSelector_columns[colId] : (state === 'true' || state !== 'false'); colSel.$column[colId] = $(this); // set default col title @@ -141,29 +158,7 @@ tsColSel = ts.columnSelector = { .prop('checked', colSel.auto) .toggleClass( wo.columnSelector_cssChecked, colSel.auto ) .on('change', function(){ - colSel.auto = this.checked; - $.each( colSel.$checkbox, function(i, $cb){ - if ($cb) { - $cb[0].disabled = colSel.auto; - colSel.$wrapper[i].toggleClass('disabled', colSel.auto); - } - }); - if (wo.columnSelector_mediaquery) { - tsColSel.updateBreakpoints(c, wo); - } - tsColSel.updateCols(c, wo); - // copy the column selector to a popup/tooltip - if (c.selector.$popup) { - c.selector.$popup.find('.tablesorter-column-selector') - .html( colSel.$container.html() ) - .find('input').each(function(){ - var indx = $(this).attr('data-column'); - $(this).prop( 'checked', indx === 'auto' ? colSel.auto : colSel.states[indx] ); - }); - } - if (wo.columnSelector_saveColumns && ts.storage) { - ts.storage( c.$table[0], 'tablesorter-columnSelector-auto', { auto : colSel.auto } ); - } + tsColSel.updateAuto(c, wo, $(this)); }).change(); } // Add a bind on update to re-run col setup @@ -173,6 +168,33 @@ tsColSel = ts.columnSelector = { } }, + updateAuto: function(c, wo, $el) { + var colSel = c.selector; + colSel.auto = $el.prop('checked') || false; + $.each( colSel.$checkbox, function(i, $cb){ + if ($cb) { + $cb[0].disabled = colSel.auto; + colSel.$wrapper[i].toggleClass('disabled', colSel.auto); + } + }); + if (wo.columnSelector_mediaquery) { + tsColSel.updateBreakpoints(c, wo); + } + tsColSel.updateCols(c, wo); + // copy the column selector to a popup/tooltip + if (c.selector.$popup) { + c.selector.$popup.find('.tablesorter-column-selector') + .html( colSel.$container.html() ) + .find('input').each(function(){ + var indx = $(this).attr('data-column'); + $(this).prop( 'checked', indx === 'auto' ? colSel.auto : colSel.states[indx] ); + }); + } + if (wo.columnSelector_saveColumns && ts.storage) { + ts.storage( c.$table[0], 'tablesorter-columnSelector-auto', { auto : colSel.auto } ); + } + }, + updateBreakpoints: function(c, wo) { var priority, column, breaks, colSel = c.selector,