I've tried to Google, but there's no library that has an API to toggle table columns when clicked elsewhere on the page, so I've decided to do this:
function deleteColumnOnClick() {
$('.hide div').click(function() {
var index = $(this).index();
$('table thead th').eq(index).toggleClass('hidden');
var hidden = $('table thead th.hidden')
$.each(hidden, function() {
var idx = $(this).index();
$.each($('table tbody tr'), function() {
$(this).find('td').eq(idx).hide();
});
});
var visible = $('table thead th:not(.hidden)');
$.each(visible, function() {
var idx = $(this).index();
$.each($('table tbody tr'), function() {
$(this).find('td').eq(idx).show();
});
});
});
}
deleteColumnOnClick();
Is this an awful solution? If yes, how do I refactor it?