Error with JQuery DataTables and ColVis plugin "Cannot read property 'sWidth' of undefined"

I had this error when the number of columns in

<thead></thead>

was different from the number of columns in

$('#ls-table').DataTable($.extend({}, window.coonDataTableOptions, {
    columns: [
         <here>
    ]
 }));

On line 3255 of the DataTables source code is this line of code:

nThs[i].style.width = o.aoColumns[iVis].sWidth;

In this case o.aoColumns[iVis] is null because the column represented by the index has just been hidden. It seems like I've run into a corner case that the creators of the plugins weren't expecting. The above code gets called in response to an internal datatables event, which is triggered by a method called by ColVis when a column is hidden. All that's needed to work around this is to change the above code to:

var column = o.aoColumns[iVis];

if(column != null) {
    nThs[i].style.width = o.aoColumns[iVis].sWidth;
}

unfortunately this requires editing the core plugin code, but I'll put in a bug report and hope that they resolve this soon. in the meantime, hopefully this helps people looking for a workaround.


Such error occurs just due to populating columns within

...DataTable(... "columns":... )

mismatch with the defined HTML page Have number of columns.

.. ..