Show/Hide Columns without column index reference
Show/Hide Columns without column index reference
Hi all, love the plugin!
I'm currently trying to use a button to show and hide a set of columns. It works fine, as long as I hard code the column indexes, e.g.
[code]
table.fnSetColumnVis(9, bVis);
table.fnSetColumnVis(10, bVis);
table.fnSetColumnVis(11, bVis);
[/code]
However, this becomes problematic for me as I have lots of tables in my web app, and I start to need to write lots of extra javascript just for this.
What I'd love to do is use the table header class to define which columns toggle. For example, when I initialise the datatable, I use:
[code]
"aoColumnDefs": [
{ "bVisible": false, "aTargets": [ "toggle" ] }
]
[/code]
This is perfect. What I'm looking for is a way to dynamically toggle whichever columns have the appropriate header class, after initialisation.
I had tried something like this:
[code]
function toggleColumns() {
var table = $('#report-table table').dataTable();
$('#report-table table thead tr th').each(function (index) {
if ($(this).hasClass('toggle'))
table.fnSetColumnVis(index, !detailedView);
});
detailedView = !detailedView;
}
[/code]
However this of course is a silly idea because when the columns are hidden, jquery won't iterate them.
Any thoughts?
I'm currently trying to use a button to show and hide a set of columns. It works fine, as long as I hard code the column indexes, e.g.
[code]
table.fnSetColumnVis(9, bVis);
table.fnSetColumnVis(10, bVis);
table.fnSetColumnVis(11, bVis);
[/code]
However, this becomes problematic for me as I have lots of tables in my web app, and I start to need to write lots of extra javascript just for this.
What I'd love to do is use the table header class to define which columns toggle. For example, when I initialise the datatable, I use:
[code]
"aoColumnDefs": [
{ "bVisible": false, "aTargets": [ "toggle" ] }
]
[/code]
This is perfect. What I'm looking for is a way to dynamically toggle whichever columns have the appropriate header class, after initialisation.
I had tried something like this:
[code]
function toggleColumns() {
var table = $('#report-table table').dataTable();
$('#report-table table thead tr th').each(function (index) {
if ($(this).hasClass('toggle'))
table.fnSetColumnVis(index, !detailedView);
});
detailedView = !detailedView;
}
[/code]
However this of course is a silly idea because when the columns are hidden, jquery won't iterate them.
Any thoughts?
This discussion has been closed.
Replies
Beyond that, I think more complex interactions would probably require a cached array of the TH elements so you can access them directly and in the required order.
Allan
Here is the code I've gone with in the end:
[code]
var detailedView = false;
function toggleColumns() {
detailedView = !detailedView;
var table = $('#myTable').dataTable();
$.each(table.fnSettings().aoColumns, function (index, value) {
if (value.nTh.classList.contains("toggle"))
table.fnSetColumnVis(index, detailedView);
});
}
[/code]
Regards,
Allan
I've been using this code for a while with v1.7.6 and recently updated to v1.9.0. Now the same code gives the error:
[code]a.aoColumns[o] is undefined[/code]
I also checked it with v1.8.2 with the same error.
If i try to set the visibility separately for the last column (which is the only one with the "toggle"-tag) with [code]table.fnSetColumnVis(8, 1);[/code] i also get the error.
Do you have any idea what could cause this?
Regards,
Ray
Thanks,
Allan
Regards,
Ray
while setting up the test case, i found the problem which causes the error.
[code] oTable.fnSetColumnVis(index, visible);[/code]
only accepts a boolean for visible. If you put an int there it doesnt work anymore.
You can see the problem on this test page:
http://www.racyics.de/dataTablesTest/
The first button works, the second doesnt. This behaviour seems to have changed from v1.7 to v1.8, because as I said, in v1.7.6 both ways work. Dont know, if this is volitional, but maybe good to know for others anyhow.
Regards,
Ray
Good to hear you got it working.
Allan