Whenever I use <td colspan="x"></td>
, I get the following error:
Uncaught TypeError: Cannot set property '_DT_CellIndex' of undefined(…)
Demo
$("table").DataTable({});
<link href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.12/css/jquery.dataTables.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.12/js/jquery.dataTables.min.js"></script>
<table style="width:50%;">
<thead>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
<th>6</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>1</td>
<td colspan="2">3 4</td>
<td colspan="3">4 5 6</td>
</tr>
</tbody>
</table>
It's working properly without DataTables.js, but when we use this structure with datatable.js it is not working. We need above table structure. Does anyone have any idea how we can use this table structure datatable.js?
You can hack around the lack of colspan support by adding an "invisible" cell for every cell that's eliminated:
DataTables doesn't complain, the table renders normally and sorting works (invisible columns sort as the empty string).
I haven't tried this with rowspan.
Because datatables doesn't support colspan, I usually ended up with using unordered lists (ul) with list-style: none;
here is the result
COLSPAN: Ajax or JavaScript sourced data
Excellent solution provided by Dirk Bergstrom only works with HTML sourced data.
It is also possible to use the same idea with Ajax sourced data.
You need to use createdRow option to modify required cells when
TR
element is created in table body.For example:
See this example for code and demonstration.
See jQuery DataTables: COLSPAN in table body TBODY - Ajax data article for more details.
ROWSPAN
It is possible to emulate
ROWSPAN
attribute using RowsGroup plug-in.To use the plugin, you need to include JavaScript file
dataTables.rowsGroup.js
and userowsGroup
option to indicate indexes of the columns where you want grouping to be applied.See this example for code and demonstration.
See jQuery DataTables: ROWSPAN in table body TBODY article for more details.
COLSPAN and ROWSPAN
It is possible to group cells both vertically and horizontally simultaneously if we combine both workarounds described above.
See this example for code and demonstration.
See jQuery DataTables: COLSPAN in table body TBODY - COLSPAN and ROWSPAN article for more details.
I don't know at what point, but I guess the fnFakeRowspan does not support the current DataTable version.
For an alternative, checkout RowsGroup plugin.
Found it pretty easy to implement and works well. Searching works perfectly, ordering doesn't.
Check here for an implementation.
If you are using
scrollX
Modified answer of #Dirk_Bergstrom
Right now, datatables do not support rowspan or colspan to the table body.
Reference
But, the possible solution should be
DataTables hidden row details example
Hope it works.