I lost two days trying to find a good solution on how to hide empty columns in jQuery dataTables via javascript, so I came up with my own solution encoding a new plugin, I think this will help others do it very quickly if you find this the plugin is useful, feel free to extend it and publish your code to help others improve their data tables.
$.fn.dataTableExt.oApi.fnHideEmptyColumns = function ( oSettings, tableObject ) { var selector = tableObject.selector; var columnsToHide = []; $(selector).find('th').each(function(i) { var columnIndex = $(this).index(); var rows = $(this).parents('table').find('tr td:nth-child(' + (i + 1) + ')');
Call plugin:
"fnInitComplete": function () { this.fnHideEmptyColumns(this); }
If you have any kind of code observation, be polite, this is not the last word on how to hide empty columns for jQuery dataTables plugin.
source share