Using Responsive with Details Column and AJAX Data
Using Responsive with Details Column and AJAX Data
I see from the examples that it is indeed possible to use Responsive with AJAX data, but in the example, the show/hide button is in the first cell. I use the following in my DataTable markup:
responsive: {
details: {
type: 'column'
}
},
I typically like to have the button in its own column, that only appears when the viewport triggers the Responsive extension by having an empty <th></th> and an empty <td></td> to contain the show/hide button.
However, my AJAX data does not contain an empty first column, and the <tbody> structure is rendered by DataTables for the AJAX data. I have tried putting an empty column using columns.data (didn't work), so I am starting to wonder if it is not possible to have a dedicated column for the Responsive show/hide buttons with AJAX data.
Is this possible?
Answers
Use the render function for the columns and a null value for the first data value.
https://datatables.net/reference/option/columns.render
@Dalex73 - I tried your suggestion, but putting a NULL in columns causes the JSON data from all the columns to display in the NULL column, with the responsive controls inside the column as well.
Here's my DataTables markup:
In my HTML markup, I have provided an empty
<th></th>and in my<tfoot>I specify the colspan to be 7, so display-wise my table is semantically correct.I also tried defining all my columns using
columnDefsand got the same result.Success! I figured out adding
defaultContent: ""to the target 0 column (the one used for the responsive button) keeps the JSON data from appearing.So, for anyone else who has this issue (cleaned up a bit and removed my custom:
Make sure your HTML markup has an empty
<th></th>for target 0 (the responsive control column), and if using a footer, make sure the number of columns matches or thecolspanequals the number of columns, including the hidden responsive column (in this case target 0).