Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix prefix/postfix buttons disappearing when colReorder is also used #190

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 47 additions & 11 deletions js/buttons.colVis.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ $.extend( DataTable.ext.buttons, {
// A collection of column visibility buttons
colvis: function ( dt, conf ) {
var node = null;
var _visOriginal = dt.columns().indexes().map( function ( idx ) {
return dt.column( idx ).visible();
} ).toArray();

var buttonConf = {
extend: 'collection',
init: function ( dt, n ) {
Expand All @@ -54,35 +58,65 @@ $.extend( DataTable.ext.buttons, {
buttons: [ {
extend: 'columnsToggle',
columns: conf.columns,
columnText: conf.columnText
columnText: conf.columnText,
_visOriginal: _visOriginal
} ]
};

// Rebuild the collection with the new column structure if columns are reordered
dt.on( 'column-reorder.dt'+conf.namespace, function (e, settings, details) {
// console.log(node);
// console.log('node', dt.button(null, node).node());
dt.button(null, dt.button(null, node).node()).collectionRebuild([{
var buttons = [{
extend: 'columnsToggle',
columns: conf.columns,
columnText: conf.columnText
}]);
columnText: conf.columnText,
prefixButtons: conf.prefixButtons,
postfixButtons: conf.postfixButtons,
_visOriginal: _visOriginal
}];

dt.button(null, dt.button(null, node).node()).collectionRebuild(buttons);
});

return buttonConf;
},

// Selected columns with individual buttons - toggle column visibility
columnsToggle: function ( dt, conf ) {
var columns = dt.columns( conf.columns ).indexes().map( function ( idx ) {
var allButtons = [];
var prefixButtons;
var postfixButtons;

var buttons = dt.columns( conf.columns ).indexes().map( function ( idx ) {
return {
extend: 'columnToggle',
columns: idx,
columnText: conf.columnText
};
} ).toArray();

return columns;
function addOriginalVisible (buttons) {
var res = buttons.map( function (config) {
if (config.extend === 'visibleRestore') {
config._visOriginal = conf._visOriginal;
}
return config;
})
return res;
}

if (conf.prefixButtons) {
prefixButtons = addOriginalVisible(conf.prefixButtons);
allButtons.push(...prefixButtons);
}

allButtons.push(...buttons);

if (conf.postfixButtons) {
postfixButtons = addOriginalVisible(conf.postfixButtons);
allButtons.push(...postfixButtons);
}

return allButtons;
},

// Single button to toggle column visibility
Expand Down Expand Up @@ -195,9 +229,11 @@ $.extend( DataTable.ext.buttons, {
},

init: function ( dt, button, conf ) {
conf._visOriginal = dt.columns().indexes().map( function ( idx ) {
return dt.column( idx ).visible();
} ).toArray();
if (!conf._visOriginal) {
conf._visOriginal = dt.columns().indexes().map( function ( idx ) {
return dt.column( idx ).visible();
} ).toArray();
}
},

action: function ( e, dt, button, conf ) {
Expand Down