2014-05-22 12:51:24 +00:00
|
|
|
/* Column Selector/Responsive table widget (beta) for TableSorter 5/22/2014 (v2.17.0)
|
2013-12-17 23:39:39 +00:00
|
|
|
* Requires tablesorter v2.8+ and jQuery 1.7+
|
|
|
|
* by Justin Hallett & Rob Garrison
|
|
|
|
*/
|
|
|
|
/*jshint browser:true, jquery:true, unused:false */
|
|
|
|
/*global jQuery: false */
|
|
|
|
;(function($){
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var ts = $.tablesorter,
|
|
|
|
namespace = '.tscolsel',
|
|
|
|
tsColSel = ts.columnSelector = {
|
|
|
|
|
|
|
|
queryAll : '@media only all { [columns] { display: none; } }',
|
2014-05-06 00:46:01 +00:00
|
|
|
queryBreak : '@media all and (min-width: [size]) { [columns] { display: table-cell; } }',
|
2013-12-17 23:39:39 +00:00
|
|
|
|
|
|
|
init: function(table, c, wo) {
|
2014-02-06 04:20:25 +00:00
|
|
|
var $t, colSel;
|
|
|
|
|
|
|
|
// abort if no input is contained within the layout
|
|
|
|
$t = $(wo.columnSelector_layout);
|
|
|
|
if (!$t.find('input').add( $t.filter('input') ).length) {
|
|
|
|
if (c.debug) {
|
|
|
|
ts.log('*** ERROR: Column Selector aborting, no input found in the layout! ***');
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2013-12-17 23:39:39 +00:00
|
|
|
|
|
|
|
// unique table class name
|
|
|
|
c.tableId = 'tablesorter' + new Date().getTime();
|
|
|
|
c.$table.addClass( c.tableId );
|
|
|
|
|
|
|
|
// build column selector/state array
|
2014-02-06 04:20:25 +00:00
|
|
|
colSel = c.selector = { $container : $(wo.columnSelector_container || '<div>') };
|
2014-03-10 01:20:17 +00:00
|
|
|
colSel.$style = $('<style></style>').prop('disabled', true).appendTo('head');
|
|
|
|
colSel.$breakpoints = $('<style></style>').prop('disabled', true).appendTo('head');
|
|
|
|
|
|
|
|
colSel.isInitializing = true;
|
2013-12-17 23:39:39 +00:00
|
|
|
tsColSel.setupSelector(table, c, wo);
|
|
|
|
|
|
|
|
if (wo.columnSelector_mediaquery) {
|
|
|
|
tsColSel.setupBreakpoints(c, wo);
|
|
|
|
}
|
|
|
|
|
2014-03-10 01:20:17 +00:00
|
|
|
colSel.isInitializing = false;
|
2013-12-17 23:39:39 +00:00
|
|
|
if (colSel.$container.length) {
|
|
|
|
tsColSel.updateCols(c, wo);
|
|
|
|
}
|
|
|
|
|
2014-05-21 01:47:55 +00:00
|
|
|
c.$table
|
|
|
|
.off('refreshColumnSelector' + namespace)
|
|
|
|
.on('refreshColumnSelector' + namespace, function(){
|
|
|
|
// make sure we're using current config settings
|
|
|
|
var c = this.config;
|
|
|
|
tsColSel.updateBreakpoints(c, c.widgetOptions);
|
|
|
|
tsColSel.updateCols(c, c.widgetOptions);
|
|
|
|
});
|
|
|
|
|
2013-12-17 23:39:39 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setupSelector: function(table, c, wo) {
|
|
|
|
var name,
|
|
|
|
colSel = c.selector,
|
|
|
|
$container = colSel.$container,
|
2014-03-10 01:20:17 +00:00
|
|
|
useStorage = wo.columnSelector_saveColumns && ts.storage,
|
2013-12-17 23:39:39 +00:00
|
|
|
// get stored column states
|
2014-03-10 01:20:17 +00:00
|
|
|
saved = useStorage ? ts.storage( table, 'tablesorter-columnSelector' ) : [],
|
|
|
|
state = useStorage ? ts.storage( table, 'tablesorter-columnSelector-auto') : {};
|
2013-12-17 23:39:39 +00:00
|
|
|
|
|
|
|
// initial states
|
2014-03-10 01:20:17 +00:00
|
|
|
colSel.auto = $.isEmptyObject(state) || $.type(state.auto) !== "boolean" ? wo.columnSelector_mediaqueryState : state.auto;
|
2013-12-17 23:39:39 +00:00
|
|
|
colSel.states = [];
|
|
|
|
colSel.$column = [];
|
|
|
|
colSel.$wrapper = [];
|
|
|
|
colSel.$checkbox = [];
|
|
|
|
// populate the selector container
|
|
|
|
c.$table.children('thead').find('tr:first th', table).each(function() {
|
|
|
|
var $this = $(this),
|
|
|
|
// if no data-priority is assigned, default to 1, but don't remove it from the selector list
|
|
|
|
priority = $this.attr(wo.columnSelector_priority) || 1,
|
2014-03-01 12:23:44 +00:00
|
|
|
colId = $this.attr('data-column'),
|
|
|
|
state = ts.getData(this, c.headers[colId], 'columnSelector');
|
|
|
|
|
2013-12-17 23:39:39 +00:00
|
|
|
|
|
|
|
// if this column not hidable at all
|
|
|
|
// include getData check (includes "columnSelector-false" class, data attribute, etc)
|
2014-03-01 12:23:44 +00:00
|
|
|
if ( isNaN(priority) && priority.length > 0 || state === 'disable' ||
|
2013-12-17 23:39:39 +00:00
|
|
|
( wo.columnSelector_columns[colId] && wo.columnSelector_columns[colId] === 'disable') ) {
|
|
|
|
return true; // goto next
|
|
|
|
}
|
|
|
|
|
2014-03-01 12:23:44 +00:00
|
|
|
// set default state; storage takes priority
|
2013-12-17 23:39:39 +00:00
|
|
|
colSel.states[colId] = saved && typeof(saved[colId]) !== 'undefined' ?
|
2014-03-01 12:23:44 +00:00
|
|
|
saved[colId] : typeof(wo.columnSelector_columns[colId]) !== 'undefined' ?
|
2014-03-31 11:13:35 +00:00
|
|
|
wo.columnSelector_columns[colId] : (state === 'true' || !(state === 'false'));
|
2013-12-17 23:39:39 +00:00
|
|
|
colSel.$column[colId] = $(this);
|
|
|
|
|
|
|
|
// set default col title
|
|
|
|
name = $this.attr(wo.columnSelector_name) || $this.text();
|
|
|
|
|
|
|
|
if ($container.length) {
|
|
|
|
colSel.$wrapper[colId] = $(wo.columnSelector_layout.replace(/\{name\}/g, name)).appendTo($container);
|
|
|
|
colSel.$checkbox[colId] = colSel.$wrapper[colId]
|
2014-02-06 04:20:25 +00:00
|
|
|
// input may not be wrapped within the layout template
|
|
|
|
.find('input').add( colSel.$wrapper[colId].filter('input') )
|
2013-12-17 23:39:39 +00:00
|
|
|
.attr('data-column', colId)
|
|
|
|
.prop('checked', colSel.states[colId])
|
2014-03-31 07:29:38 +00:00
|
|
|
.on('change', function(){
|
2013-12-17 23:39:39 +00:00
|
|
|
colSel.states[colId] = this.checked;
|
|
|
|
tsColSel.updateCols(c, wo);
|
|
|
|
}).change();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
setupBreakpoints: function(c, wo){
|
2014-03-10 01:20:17 +00:00
|
|
|
var colSel = c.selector;
|
2013-12-17 23:39:39 +00:00
|
|
|
|
|
|
|
// add responsive breakpoints
|
|
|
|
if (wo.columnSelector_mediaquery) {
|
|
|
|
// used by window resize function
|
|
|
|
colSel.lastIndex = -1;
|
|
|
|
wo.columnSelector_breakpoints.sort();
|
|
|
|
tsColSel.updateBreakpoints(c, wo);
|
2014-05-18 18:14:48 +00:00
|
|
|
c.$table
|
2014-05-21 01:47:55 +00:00
|
|
|
.off('updateAll' + namespace)
|
|
|
|
.on('updateAll' + namespace, function(){
|
2014-05-18 18:14:48 +00:00
|
|
|
tsColSel.updateBreakpoints(c, wo);
|
|
|
|
tsColSel.updateCols(c, wo);
|
|
|
|
});
|
2013-12-17 23:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (colSel.$container.length) {
|
|
|
|
// Add media queries toggle
|
2014-03-10 01:20:17 +00:00
|
|
|
if (wo.columnSelector_mediaquery) {
|
|
|
|
colSel.$auto = $( wo.columnSelector_layout.replace(/\{name\}/g, wo.columnSelector_mediaqueryName) ).prependTo(colSel.$container);
|
|
|
|
colSel.$auto
|
2014-02-06 04:20:25 +00:00
|
|
|
// needed in case the input in the layout is not wrapped
|
2014-03-10 01:20:17 +00:00
|
|
|
.find('input').add( colSel.$auto.filter('input') )
|
2014-02-06 04:20:25 +00:00
|
|
|
.attr('data-column', 'auto')
|
2014-03-10 01:20:17 +00:00
|
|
|
.prop('checked', colSel.auto)
|
2014-03-31 07:29:38 +00:00
|
|
|
.on('change', function(){
|
2014-03-10 01:20:17 +00:00
|
|
|
colSel.auto = this.checked;
|
2013-12-17 23:39:39 +00:00
|
|
|
$.each( colSel.$checkbox, function(i, $cb){
|
|
|
|
if ($cb) {
|
2014-03-10 01:20:17 +00:00
|
|
|
$cb[0].disabled = colSel.auto;
|
|
|
|
colSel.$wrapper[i].toggleClass('disabled', colSel.auto);
|
2013-12-17 23:39:39 +00:00
|
|
|
}
|
|
|
|
});
|
2014-03-10 01:20:17 +00:00
|
|
|
if (wo.columnSelector_mediaquery) {
|
|
|
|
tsColSel.updateBreakpoints(c, wo);
|
|
|
|
}
|
2013-12-17 23:39:39 +00:00
|
|
|
tsColSel.updateCols(c, wo);
|
2014-02-06 04:20:25 +00:00
|
|
|
// 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(){
|
2014-03-07 18:53:21 +00:00
|
|
|
var indx = $(this).attr('data-column');
|
2014-03-10 01:20:17 +00:00
|
|
|
$(this).prop( 'checked', indx === 'auto' ? colSel.auto : colSel.states[indx] );
|
2014-02-06 04:20:25 +00:00
|
|
|
});
|
|
|
|
}
|
2014-03-10 01:20:17 +00:00
|
|
|
if (wo.columnSelector_saveColumns && ts.storage) {
|
|
|
|
ts.storage( c.$table[0], 'tablesorter-columnSelector-auto', { auto : colSel.auto } );
|
|
|
|
}
|
2013-12-17 23:39:39 +00:00
|
|
|
}).change();
|
|
|
|
}
|
|
|
|
// Add a bind on update to re-run col setup
|
2014-03-31 07:29:38 +00:00
|
|
|
c.$table.off('update' + namespace).on('update' + namespace, function() {
|
2013-12-17 23:39:39 +00:00
|
|
|
tsColSel.updateCols(c, wo);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-02-06 04:20:25 +00:00
|
|
|
updateBreakpoints: function(c, wo) {
|
2013-12-17 23:39:39 +00:00
|
|
|
var priority, column, breaks,
|
|
|
|
colSel = c.selector,
|
|
|
|
prefix = '.' + c.tableId,
|
|
|
|
mediaAll = [],
|
|
|
|
breakpts = '';
|
2014-03-10 01:20:17 +00:00
|
|
|
if (wo.columnSelector_mediaquery && !colSel.auto) {
|
2013-12-17 23:39:39 +00:00
|
|
|
colSel.$breakpoints.prop('disabled', true);
|
|
|
|
colSel.$style.prop('disabled', false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// only 6 breakpoints (same as jQuery Mobile)
|
|
|
|
for (priority = 0; priority < 6; priority++){
|
|
|
|
/*jshint loopfunc:true */
|
|
|
|
breaks = [];
|
|
|
|
c.$headers.filter('[' + wo.columnSelector_priority + '=' + (priority + 1) + ']').each(function(){
|
|
|
|
column = parseInt($(this).attr('data-column'), 10) + 1;
|
|
|
|
breaks.push(prefix + ' tr th:nth-child(' + column + ')');
|
|
|
|
breaks.push(prefix + ' tr td:nth-child(' + column + ')');
|
|
|
|
});
|
|
|
|
if (breaks.length) {
|
|
|
|
mediaAll = mediaAll.concat( breaks );
|
|
|
|
breakpts += tsColSel.queryBreak
|
|
|
|
.replace(/\[size\]/g, wo.columnSelector_breakpoints[priority])
|
|
|
|
.replace(/\[columns\]/g, breaks.join(','));
|
|
|
|
}
|
|
|
|
}
|
2014-02-19 18:11:23 +00:00
|
|
|
if (colSel.$style) {
|
|
|
|
colSel.$style.prop('disabled', true);
|
|
|
|
}
|
|
|
|
colSel.$breakpoints
|
|
|
|
.prop('disabled', false)
|
2013-12-17 23:39:39 +00:00
|
|
|
.html( tsColSel.queryAll.replace(/\[columns\]/g, mediaAll.join(',')) + breakpts );
|
|
|
|
},
|
|
|
|
|
|
|
|
updateCols: function(c, wo) {
|
2014-03-10 01:20:17 +00:00
|
|
|
if (wo.columnSelector_mediaquery && c.selector.auto || c.selector.isInitializing) {
|
2013-12-17 23:39:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var column,
|
2014-03-10 01:20:17 +00:00
|
|
|
colSel = c.selector,
|
2013-12-17 23:39:39 +00:00
|
|
|
styles = [],
|
|
|
|
prefix = '.' + c.tableId;
|
2014-03-10 01:20:17 +00:00
|
|
|
colSel.$container.find('input[data-column]').filter('[data-column!="auto"]').each(function(){
|
2013-12-17 23:39:39 +00:00
|
|
|
if (!this.checked) {
|
|
|
|
column = parseInt( $(this).attr('data-column'), 10 ) + 1;
|
|
|
|
styles.push(prefix + ' tr th:nth-child(' + column + ')');
|
|
|
|
styles.push(prefix + ' tr td:nth-child(' + column + ')');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (wo.columnSelector_mediaquery){
|
2014-03-10 01:20:17 +00:00
|
|
|
colSel.$breakpoints.prop('disabled', true);
|
2013-12-17 23:39:39 +00:00
|
|
|
}
|
2014-03-10 01:20:17 +00:00
|
|
|
if (colSel.$style) {
|
|
|
|
colSel.$style.prop('disabled', false).html( styles.length ? styles.join(',') + ' { display: none; }' : '' );
|
2013-12-17 23:39:39 +00:00
|
|
|
}
|
|
|
|
if (wo.columnSelector_saveColumns && ts.storage) {
|
2014-03-10 01:20:17 +00:00
|
|
|
ts.storage( c.$table[0], 'tablesorter-columnSelector', colSel.states );
|
2013-12-17 23:39:39 +00:00
|
|
|
}
|
2014-02-06 04:20:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
attachTo : function(table, elm) {
|
2014-03-07 18:53:21 +00:00
|
|
|
table = $(table)[0];
|
2014-02-06 04:20:25 +00:00
|
|
|
var colSel, wo, indx,
|
|
|
|
c = table.config,
|
|
|
|
$popup = $(elm);
|
|
|
|
if ($popup.length && c) {
|
|
|
|
if (!$popup.find('.tablesorter-column-selector').length) {
|
|
|
|
// add a wrapper to add the selector into, in case the popup has other content
|
|
|
|
$popup.append('<span class="tablesorter-column-selector"></span>');
|
|
|
|
}
|
|
|
|
colSel = c.selector;
|
|
|
|
wo = c.widgetOptions;
|
|
|
|
$popup.find('.tablesorter-column-selector')
|
|
|
|
.html( colSel.$container.html() )
|
|
|
|
.find('input').each(function(){
|
|
|
|
var indx = $(this).attr('data-column');
|
2014-03-10 01:20:17 +00:00
|
|
|
$(this).prop( 'checked', indx === 'auto' ? colSel.auto : colSel.states[indx] );
|
2014-02-06 04:20:25 +00:00
|
|
|
});
|
|
|
|
colSel.$popup = $popup.on('change', 'input', function(){
|
|
|
|
// data input
|
|
|
|
indx = $(this).attr('data-column');
|
|
|
|
// update original popup
|
|
|
|
colSel.$container.find('input[data-column="' + indx + '"]')
|
|
|
|
.prop('checked', this.checked)
|
|
|
|
.trigger('change');
|
|
|
|
});
|
|
|
|
}
|
2013-12-17 23:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
ts.addWidget({
|
|
|
|
id: "columnSelector",
|
|
|
|
priority: 10,
|
|
|
|
options: {
|
|
|
|
// target the column selector markup
|
|
|
|
columnSelector_container : null,
|
|
|
|
// column status, true = display, false = hide
|
|
|
|
// disable = do not display on list
|
|
|
|
columnSelector_columns : {},
|
|
|
|
// remember selected columns
|
|
|
|
columnSelector_saveColumns: true,
|
|
|
|
|
|
|
|
// container layout
|
|
|
|
columnSelector_layout : '<label><input type="checkbox">{name}</label>',
|
|
|
|
// data attribute containing column name to use in the selector container
|
|
|
|
columnSelector_name : 'data-selector-name',
|
|
|
|
|
|
|
|
/* Responsive Media Query settings */
|
|
|
|
// enable/disable mediaquery breakpoints
|
|
|
|
columnSelector_mediaquery: true,
|
|
|
|
// toggle checkbox name
|
|
|
|
columnSelector_mediaqueryName: 'Auto: ',
|
|
|
|
// breakpoints checkbox initial setting
|
|
|
|
columnSelector_mediaqueryState: true,
|
|
|
|
// responsive table hides columns with priority 1-6 at these breakpoints
|
|
|
|
// see http://view.jquerymobile.com/1.3.2/dist/demos/widgets/table-column-toggle/#Applyingapresetbreakpoint
|
|
|
|
// *** set to false to disable ***
|
|
|
|
columnSelector_breakpoints : [ '20em', '30em', '40em', '50em', '60em', '70em' ],
|
|
|
|
// data attribute containing column priority
|
|
|
|
// duplicates how jQuery mobile uses priorities:
|
|
|
|
// http://view.jquerymobile.com/1.3.2/dist/demos/widgets/table-column-toggle/
|
|
|
|
columnSelector_priority : 'data-priority'
|
|
|
|
|
|
|
|
},
|
|
|
|
init: function(table, thisWidget, c, wo) {
|
|
|
|
tsColSel.init(table, c, wo);
|
|
|
|
},
|
|
|
|
remove: function(table, c){
|
|
|
|
var csel = c.selector;
|
|
|
|
csel.$container.empty();
|
2014-05-16 18:37:13 +00:00
|
|
|
if (csel.$popup) { csel.$popup.empty(); }
|
2013-12-17 23:39:39 +00:00
|
|
|
csel.$style.remove();
|
|
|
|
csel.$breakpoints.remove();
|
2014-03-31 07:29:38 +00:00
|
|
|
c.$table.off('updateAll' + namespace + ' update' + namespace);
|
2013-12-17 23:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
})(jQuery);
|