Target key instead of index in columnDefs
Target key instead of index in columnDefs
Rawland_Hustle
Posts: 94Questions: 16Answers: 0
Link to test case: http://themeadow.se/ntjperrors/
Debugger code (debug.datatables.net):
Error messages shown: No error message shown.
Description of problem:
All my indexes are changed everytime I add or remove a data point. It's very tidious to correct this. Is it possible to somehow use:
"columnDefs": [
{
"targets": ["ServiceProducerDescription"],
"visible": false,
}
]
...instead of this:
"columnDefs": [
{
"targets": [ 8 ],
"visible": false,
}
]
This question has an accepted answers - jump to answer
Answers
I know. The examples in the docs are full of those hard coded column numbers. I cleaned up my code some time ago and got rid of every single hard coded number.
As you can see you can target a column by its class and you can also assign classes to the td elements targeting the column (th). That is also quite useful.
https://datatables.net/reference/option/columnDefs.targets
I'm not sure I understand. Do you mean that
{ targets: "hiddenCols", visible: false}
actually works for you? If so, what do I have to change in order for it for me (because it does not)?See the
columnDefs.targets
docs for the options:The string is a class not a
columns.data
definition, if thats what you are asking. If you are usingcolumns
and you are targeting only one column withcolumnDefs
then you can move your config options to thecolumns
definitions.columnDefs
is designed for applying the same config options to multiple columns.Kevin
I assign classes in my HTML table definitions. All I need to make sure is to assign the right class to the respective column. Then I don't have to worry about the position of the column in the table.
With my code above the 2nd column isn't visible and the tds of the third column get the class "text-center".
Thanks alot!