Great find Guilhermebastian2, I've been following your question there hoping someone would know.
Applying to Knack meant:
-
Converting the code into a simple function to be called for any view we want.
-
Finding a spot to inject a link to show the controls (.clearfix element is it).
-
Changing the selectors to match Knack, and modifying the new controls to not clash with existing Knack IDs.
-
Positioning the controls.
Here's the code, change the render code to target views instead of adding this to all tables.
$(document).on('knack-view-render.view_6', function(event, view, data) {
addTableColumnChoose(view);
});
var addTableColumnChoose = function(view) {
//See http://helpdesk.knackhq.com/support/discussions/topics/5000074312 and https://jsfiddle.net/HvA4s/
var clearFix = $(’#’ + view.key + ’ > div.kn-records-nav.clearfix’);
clearFix.append("<div class=‘clear’><a href=’#’ class=‘choose-columns’>Choose Columns</a></div>");
$('#' + view.key + ' .choose-columns').click(function() {
var headers = $('#' + view.key + ' > table th').map(function() {
var th = $(this);
return {text: th.text(), shown: th.css('display') != 'none'};
});
var h = ['<div id=tableChooseColumns><button id=done>Done</button><table><thead><tr>'];
$.each(headers, function() {
h.push('<th><input type=checkbox',
(this.shown ? ' checked ' : ' '),
'/> ',
this.text,
'</th>');
});
h.push('</tr></thead></table></div>');
$('body').append(h.join(''));
var pos = $('#' + view.key + ' .choose-columns').position();
$('#tableChooseColumns').css({'position': 'absolute','left': '20px', 'top': pos.top,'padding': '5px', 'border': '1px solid #000', 'background': '#fff'});
$('#done').click(function() {
var showHeaders = $('#tableChooseColumns input').map(function() { return this.checked; });
$.each(showHeaders, function(i, show) {
var cssIndex = i + 1;
var tags = $('#' + view.key + ' > table th:nth-child(' + cssIndex + '), #' + view.key + ' > table td:nth-child(' + cssIndex + ')');
if (show)
tags.show();
else
tags.hide();
});
$('#tableChooseColumns').remove();
return false;
});
return false;
});
}
haven't tested in all situations including showing filter controls and the effect on the 'Choose Columns' link so this might need tweaking yet.
Thanks again to Guilhermebastian2 for getting this started.