jQuery DataTables: How to expand/collapse all child rows - Regular table
Example for article at https://www.gyrocode.com/articles/jquery-datatables-how-to-expand-collapse-all-child-rows/
HTML
<link rel="stylesheet" href="https://cdn.datatables.net/v/dt/dt-1.10.16/datatables.min.css">
<script src="https://cdn.datatables.net/v/dt/dt-1.10.16/datatables.min.js"></script>
<h3><a target="_blank" href="https://www.gyrocode.com/articles/jquery-datatables-how-to-expand-collapse-all-child-rows/">jQuery DataTables: How to expand/collapse all child rows</a> <small>Regular table</small></h3>
<button id="btn-show-all-children" type="button">Expand All</button>
<button id="btn-hide-all-children" type="button">Collapse All</button>
<hr>
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Salary</th>
</tr>
</tfoot>
<tbody class="test">
<tr>
<td>1</td> <td>2</td> <td>3</td> <td>4</td> <td>5</td>
</tr>
<tr>
<td>1</td> <td>2</td> <td>3</td> <td>4</td> <td>5</td>
</tr>
<tr>
<td>1</td> <td>2</td> <td>3</td> <td>4</td> <td>5</td>
</tr>
<tr>
<td>1</td> <td>2</td> <td>3</td> <td>4</td> <td>5</td>
</tr>
<tr>
<td>1</td> <td>2</td> <td>3</td> <td>4</td> <td>5</td>
</tr>
<tr>
<td>1</td> <td>2</td> <td>3</td> <td>4</td> <td>5</td>
</tr>
<tr>
<td>1</td> <td>2</td> <td>3</td> <td>4</td> <td>5</td>
</tr>
<tr>
<td>1</td> <td>2</td> <td>3</td> <td>4</td> <td>5</td>
</tr>
<tr>
<td>1</td> <td>2</td> <td>3</td> <td>4</td> <td>5</td>
</tr>
<tr>
<td>1</td> <td>2</td> <td>3</td> <td>4</td> <td>5</td>
</tr>
<tr>
<td>1</td> <td>2</td> <td>3</td> <td>4</td> <td>5</td>
</tr>
<tr>
<td>1</td> <td>2</td> <td>3</td> <td>4</td> <td>5</td>
</tr>
<tr>
<td>1</td> <td>2</td>...
CSS
td.details-control {
background: url('https://cdn.rawgit.com/DataTables/DataTables/6c7ada53ebc228ea9bc28b1b216e793b1825d188/examples/resources/details_open.png') no-repeat center center;
cursor: pointer;
}
tr.shown td.details-control {
background: url('https://cdn.rawgit.com/DataTables/DataTables/6c7ada53ebc228ea9bc28b1b216e793b1825d188/examples/resources/details_close.png') no-repeat center center;
}
JavaScript
/* Formatting function for row details - modify as you need */
function format ( d ) {
// `d` is the original data object for the row
return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'+
'<tr>'+
'<td>Full name:</td>'+
'<td>'+d.name+'</td>'+
'</tr>'+
'<tr>'+
'<td>Extension number:</td>'+
'<td>'+d.extn+'</td>'+
'</tr>'+
'<tr>'+
'<td>Extra info:</td>'+
'<td>And any further details here (images etc)...</td>'+
'</tr>'+
'</table>';
}
$(document).ready(function() {
var table = $('#example').DataTable({
//'ajax': 'https://api.myjson.com/bins/16lp6',
'columns': [
{
'className': 'details-control',
'orderable': false,
'data': null,
'defaultContent': ''
},
{ 'data': 'name' },
{ 'data': 'position' },
{ 'data': 'office' },
{ 'data': 'salary' }
],
'order': [[1, 'asc']]
} );
// Add event listener for opening and closing details
$('#example tbody').on('click', 'td.details-control', function(){
var tr = $(this).closest('tr');
var row = table.row( tr );
if(row.child.isShown()){
// This row is already open - close it
row.child.hide();
tr.removeClass('shown');
} else {
// Open this row
row.child(format(row.data())).show();
tr.addClass('shown');
}
});
// Handle click on "Expand All" button
$('#btn-show-all-children').on('click', function(){
// Enumerate all rows
debugger;
table.rows().every(function(){
// If row has details collapsed
if(!this.child.isShown()){
// Open this row
this.child(format(this.data())).show();
...