According to the documentation here, Datatables supports colspan
and rowspan
in the headers, but with this caveat.
Each column must have one TH cell which is unique to it for the listeners to be added.
I figured out a hacky workaround and I don't know if it's the best solution, but it does provide a working solution nonetheless.
Create two header rows and use the first header row with the colspan
attribute and the second header row with all the individual columns. Then apply css display: none
to hide the row. This will allow the datatable to initialize without erroring. However, I'm not sure if it might cause any side effects with other functionality.
UPDATE
Initially I applied display: none
using css but this solution didn't draw the table using the full width of the container. So I modified this to apply the style in code after the table was initialized and drawn. This allows it to display in the full container width.
$(document).ready(function() {
$('#foo').DataTable();
$("tr.hideme").css({"display" : "none"});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.16/css/jquery.dataTables.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.16/js/jquery.dataTables.min.js"></script>
<table id="foo">
<thead>
<tr>
<th colspan="5">asdf</th>
</tr>
<tr class="hideme">
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</tbody>
</table>