Give an id/class to a dynamic column header (when creating in js)
Give an id/class to a dynamic column header (when creating in js)
tacman1123
Posts: 205Questions: 46Answers: 1
Is there a way to give an id to a th via the column definitions when creating a datatable dyanmically? How about a class?
https://datatables.net/reference/option/
<th data-dt-column="2" rowspan="1" colspan="1" class="dt-orderable-none" aria-label="thumbnail">
<span class="dt-column-title">thumbnail</span><span class="dt-column-order"></span>
</th>
I'd like to add my own properties here, I know I can add my own className but I think that applies to the column header and the cell data. I want to add an id to the header and perhaps my own class to just the th. Is that possible?
Thx.
This question has an accepted answers - jump to answer
Answers
Related: Instead of data-dt-column="2" which is a numeric index, I'd much rather be able to define my own unique code and make it accessible somehow, e.g. data-dt-column-code="thumbnail".
There isn't anything within Datatables to configure the header with an
id
. However there is nothing in Datatables to stop you from doing so by defining in the HTMl or using Javascript. or jQuery methods like jQuery attr(). Same with the classname. You can usecolumns.className
but that will apply to both theth
andtd
in thetbody
.Same as above you can create your own HTML5 data attributes. I would make sure to use something different from the Datatable attributes.
Kevin
Sorry, I'm not following where to do that. My table is created completely with javascript and populated with ajax.
the columns and the ajax data match in key name (id, url, thumbnail, etc.) but vary depending on the datasource (e.g. if an admin is logged in, there are more fields).
After its done initializing (I forget the event name) I guess I can navigate through the table / tr / th and add a class and id and data- attributes and map backwards from the column number back to my column data (as when using columnDefs).
But it'd be a nice feature if when defining the column I could simply add those.
Ultimately, I'm trying to figure out why the column headers aren't aligned with the data, I'll open a separate issue for that. But in the process we wanted to manipulate the header DOM, thus this issue. Is there a specific place to make feature requests?
Use
initComplete
to apply theid
, class names, etc to only the header cells.If you want to apply a class name to both the header and all the cells in the column use
columns.className
.To apply attributes to cells in each row use
createdRow
. See this example.Its hard to say without seeing the problem but there are a couple common issues:
columns.adjust()
when the table is made visible. See this example.If this doesn't help then please provide a test case replicating the issue so we can help debug.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
It's probably one of those things. I'll do a deeper dive, but here's what should be a showcase of all the awesomeness datatables offers (scroller, responsive, etc.);
https://museado.org/en/pixie/pixie/meili/larco/object?table=object
The problem only happens when responsive, not when the screen is full width. I'm guessing it's related to the columns.adjust() not being called.
Thanks!
Eventually this was solved by adding a class to the header, so columns are all aligned now!