Created
January 13, 2015 04:00
-
-
Save krusynth/c8ae8f3c9ab2f73a44ec to your computer and use it in GitHub Desktop.
dataTables.js requires you to have "good" tables. If your table has an uneven number of columns per row, and you're getting the dreaded " Cannot read property 'mData' of undefined " this will fix it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// dataTables.js is great! But it requires you to have "good" tables in | |
// the first place. If your table has an uneven number of columns per | |
// row, and you're getting the dreaded "Cannot read property | |
// 'mData' of undefined" this'll fix it. If you're missing a thead or | |
// tbody you'll get that error too - this won't fix that! | |
$(document).ready(function() { | |
// We need to make sure every row in our table has the same number of columns. | |
// Otherwise dataTable doesn't work. | |
$('table').each(function(i, elm) { | |
elm = $(elm); | |
// Get the longest row length. | |
var maxsize = 0; | |
var trs = elm.find('tr'); | |
trs.each(function(i, tr) { | |
if($(tr).find('td,th').length > maxsize) { | |
maxsize = $(tr).find('td,th').length; | |
} | |
}); | |
// Repeat the loop, this time padding out our rows | |
trs.each(function(i, tr) { | |
var tr = $(tr); | |
var missing = maxsize - tr.find('td,th').length; | |
if(missing > 0) { | |
var extra_elms = '<td></td>'; | |
if(tr.parent().prop('tagName').toLowerCase() === 'thead') { | |
extra_elms = '<th></th>'; | |
} | |
for(i=0; i<missing; i++) { | |
tr.append(extra_elms); | |
} | |
} | |
}); | |
}); | |
// | |
// Put your actual call to dataTable here!!!! | |
// $('table').dataTable({ | |
// 'scrollY': '400px', | |
// 'scrollX': true, | |
// 'scrollCollapse': true, | |
// 'paging': false, | |
// 'ordering': false, | |
// 'info': false | |
// }); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment