Dynamically identify column
Dynamically identify column
Link to test case: Intranet app
Debugger code (debug.datatables.net):
Error messages shown: N/A
Description of problem:
It appears there is currently no way to create table columns at run time, just before binding ajax returned data to datatable. So, I had to initially include all possible columns to the table. I am trying to show some columns based on data in an array (of column names) and make others not visible.
These are a set of outcome labels and user can select all or a subset of these to get a count of each selected outcome.
Assuming table columns (i.e. all possible outcomes) are DB, F1, F2, F3 H1, H2, H3 and my array (selOutcomes) contains [BD, F2, H2], how can I form the following statements
var column = table.column( '**BD**:name' );
in this loop?
$.each(selOutcomes, function (index, item) {
var column = table.column('??:name');
column.visible(true);
});
Or any other solution that allows me to show/hide column based on contents of an array.
Answers
Sounds like you are using '
column.name
to name the columns. Looks also like you want to use thecolumn-selector
of {string}:name to choose the column in your loop. It seems like you loop should like something like this:This is assuming you have the
column.name
defined like this (plus any other column definitions):Not sure if this example will help but it uses jQuery ajax() to fetch the table data and defines the columns before initializing Datatables:
http://live.datatables.net/huyexejo/1/edit
Kevin
Thank you Kevin.
I saw a post similar to what you provided a few minutes after posting my question.
live.datatables.net/qimukefe/1/edit
I can work with this. Thank you.
Just in case someone looks at this post later:
I noticed after initial data table display where ALL outcome codes were displayed; when I selected a subset of outcome codes and recreated/re-initialized datatable, old column headers were still intact even though data table was only showing the selected columns data.
I had to add these two statements before recreating datatable:
Then,
Good, glad its working. FYI the
destroy()
docs use.empty()
in one the examples for the same reasonKevin