Reset column visibility and reorder on a single button
Reset column visibility and reorder on a single button
Hello everyone!
I'm using datatables with the buttons colvis, colReorder and stateSave enabled.
I want to provider users to option to reset both the order and the column visibility back to the original state.
To reset the column reorder, I found that I could create a custom button, and it works fine:
{
"text": 'Reset order',
"action": function ( e, dt, node, config ) { table.colReorder.reset(); }
}
But I couldn't find a way to include on tat button the code to reset the column visibility back to original state. The only way I could do that was adding another button that shows all columns:
{
extend: 'colvisGroup',
text: 'Show all',
show: ':hidden'
}
So, does anyone know a a way to combine both operations on a single button?
PS: I tried adding the "postfixButtons: [ 'colvisRestore' ]" on the column picker, but since I'm using stateSave, it doesn't revert back to original state but instead into the previous saved state.
Thank you!
Answers
I think I figured that out after looking into source code.
{
"text": 'Column Reset',
"action": function ( e, dt, node, config ) { table.colReorder.reset(); dt.columns( config.show ).visible( true ); }
}
You are correct in regards to colReorder.reset().
As for column visibility reset, check out my answer in this post, https://datatables.net/forums/discussion/comment/89772/#Comment_89772.
This will allow you to have permanently hidden columns, that you can then control via code when to show/hide, and columns that you wish not to be hidden.