I am using jQuery Datatable and I want to add a button for expand/collapse all row of my table. But I have an issue for the expand when I have row already expanded, and I click on the Button expandALL the row that I expand first is collapsed now and the other expand .. Or if I do the inverse he will be expanded and the other collapse .. How can I do for expand all row even I have already one expand (same case with the collapse)
function init() {
Docu.table = $('.js-table-doc').DataTable(dataTableSettngs());
$('#btn-show-all-doc').on('click', expandCollapseAll);
}
function dataTableSettings() {
return {
responsive: {
details: {
type: 'column'
}
},
serverSide: true,
searching: false,
columnDefs: [
{targets: [0, 8], orderable: false}}
],
columns: [{data: 'id'},
{data: 'name'}]
}
function expandCollapseAll() {
Document.table.rows(':not(.parent)').nodes().to$().find('td:first-child').trigger('click').length ||
Document.table.rows('.parent').nodes().to$().find('td:first-child').trigger('click')
}
Your code has only open action. To close
table.rows('.parent').nodes().to$().find('td:first-child').trigger('click')
var table = $('#example').DataTable({
responsive: {
details: {
type: 'column'
}
},
columnDefs: [{
className: 'control',
orderable: false,
targets: 0
}],
order: [1, 'asc']
});
$('#btn-show-all-doc').on('click', expandCollapseAll);
function expandCollapseAll() {
table.rows('.parent').nodes().to$().find('td:first-child').trigger('click').length ||
table.rows(':not(.parent)').nodes().to$().find('td:first-child').trigger('click')
}
<link href="https://cdn.datatables.net/responsive/2.2.3/css/responsive.dataTables.min.css" rel="stylesheet" />
<link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/responsive/2.2.3/js/dataTables.responsive.min.js"></script>
<button id="btn-show-all-doc">Expand / Collapse</button>
<table id="example" class="display nowrap" style="width:100%">
<thead>
<tr>
<th></th>
<th class="all">First name</th>
<th class="all">Last name</th>
<th class="all">Position</th>
<th class="all">Office</th>
<th class="all">Age</th>
<th class="none">Start date</th>
<th class="none">Salary</th>
<th class="none">Extn.</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td>Tiger</td>
<td>Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
<td>$320,800</td>
<td>5421</td>
</tr>
<tr>
<td></td>
<td>Garrett</td>
<td>Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>2011/07/25</td>
<td>$170,750</td>
<td>8422</td>
</tr>
<tr>
<td></td>
<td>Ashton</td>
<td>Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>66</td>
<td>2009/01/12</td>
<td>$86,000</td>
<td>1562</td>
</tr>
<tr>
<td></td>
<td>Cedric</td>
<td>Kelly</td>
<td>Senior Javascript Developer</td>
<td>Edinburgh</td>
<td>22</td>
<td>2012/03/29</td>
<td>$433,060</td>
<td>6224</td>
</tr>
<tr>
<td></td>
<td>Airi</td>
<td>Satou</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>33</td>
<td>2008/11/28</td>
<td>$162,700</td>
<td>5407</td>
</tr>
<tr>
<td></td>
<td>Brielle</td>
<td>Williamson</td>
<td>Integration Specialist</td>
<td>New York</td>
<td>61</td>
<td>2012/12/02</td>
<td>$372,000</td>
<td>4804</td>
</tr>
</tbody>
</table>