Datatables not respecting columns.className
Datatables not respecting columns.className
I am trying to put a button in a specific column (e.g. a named column). I need to do this as I won't know if this column is the the 5th or the 2nd in the index so a numbered index won't work.
Based on the documenation it states that I can use columns.className to put a class on the <td> tag and based on this documentation it should work https://datatables.net/reference/option/columnDefs. I would expect that column to have a button in it as both the th and td have a class name "test".
Here is a fiddle of the issue.
http://live.datatables.net/luxuxere/4/edit?html,css,js,output
This question has an accepted answers - jump to answer
Answers
The only way I was able to get it to "work" is by doing, but that doesn't really help me, and I think its just simply due to still setting a "target".
columnDefs: [
{
className: "test",
targets: [3],
render: function (data, type, full, meta) {
return '<button class="buttonData">' + data + '</button>';
}
}];
Yes, you can't use
columns.className
in combination withcolumnDefs.target
since the class names are added to the columns after the columnDefs have been resolved.Allan
So is there not a way to create a "named"? I won't know the index of the column at all when the data is coming to me. Is there a workaround for this?
For example I could get "id,name,reference,animals" back as my columns one time and then "name,reference,address,zip" the next time. So I can't say "targets: x" to identify say the "reference" column every time.
Can you not just loop over your
columnSet
array and attach the rendering function when you find a suitable class? i.e. just do it before you initialise DataTable.You can't really use the naming functions during initialisation for the reason described above, and you can't change things like the renderer after initialisation.
Allan
Sorry, I'm not sure what you mean, could you maybe provide an example of what you mean?
This almost seems like a chicken and the egg problem, the className is on the column in the columnSet but it has to be rendered first to have the columnDefs applied appropriately?
This is my point - it is chicken and the egg, and the only way to resolve it is to do it outside of DataTables. Simply use a
for
loop to spin over yourcolumnSet
array and set the rendering method there: http://live.datatables.net/luxuxere/5/edit .Allan