JSFiddle - React, Tailwind, and code Playground
HTML
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="//cdn.datatables.net/1.10.0/css/jquery.dataTables.css">
<script src="//datatables.net/download/build/nightly/jquery.dataTables.js?_=40d3cd44d93ca5fb63a5b6029d899332"></script>
<table id="myTable">
<thead>
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
<th>Col 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>Sam</td>
<td>10</td>
<td>Left</td>
<td>Blue</td>
</tr>
<tr>
<td>Mike</td>
<td>8</td>
<td>Right</td>
<td>Red</td>
</tr>
<tr>
<td>Joe</td>
<td>3</td>
<td>Left</td>
<td>Blue</td>
</tr>
</tbody>
</table>
<script type="text/template" id="child_table">
<table>
<thead>
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Jen</td>
<td>100</td>
<td>Left</td>
</tr>
<tr>
<td>Sal</td>
<td>88</td>
<td>Right</td>
</tr>
</tbody>
</table>
</script>
<button id="createChild">Create Child</button>
<button id="recreateChild">Recreate Child</button>
<button id="hideChild">Hide Child</button>
JavaScript
var myDataTable;
var index = 0;
var parentParams = {
"order":[],
"columns":[
{"name":"Col 1"},
{"name":"Col 2"},
{"name":"Col 3"},
{"name":"Col 4"},
],
};
var childParams = {
"columns":[
{"name":"Col 1"},
{"name":"Col 2"},
{"name":"Col 3"},
],
"order":[],
};
$(document).ready(function(){
myDataTable = $('#myTable').DataTable(parentParams);
$('#createChild').click(function(e){
e.preventDefault();
if(typeof myDataTable.row(index).child() == "undefined"){
var $table = $($('#child_table').html());
$table.css('width','100%');
var newChildParams = $.extend({}, childParams);
var childTable = $table.DataTable(newChildParams);
myDataTable.row(index).child(
childTable.table().container()
);
}
myDataTable.row(index).child.show();
index++;
});
$('#recreateChild').click(function(e){
e.preventDefault();
var lastIndex = index - 1;
var $table = $($('#child_table').html());
$table.css('width','100%');
var childTable = $table.DataTable(childParams);
myDataTable.row(lastIndex).child(
childTable.table().container()
);
myDataTable.row(lastIndex).child.show();
});
$('#hideChild').click(function(e){
myDataTable.row(--index).child.hide();
});
});