Expand only one row with details (collapse others)
Expand only one row with details (collapse others)
Skazhikadyadya
Posts: 1Questions: 1Answers: 0
Hi, thank you for great plugin.
I read tonns of pages at forum and can't see solution for standard problem. Whan I expand row I want to collapse other opened rows. How I can do that? My code is:
$(document).ready(function() {
var table = $('#opHistory').DataTable( {
"language": {
"url": "/datatables/language_russian.json"
},
"ajax": "table-operations-history.txt",
"columns": [
{ "data": "hDate" },
{ "data": "hType" },
{ "data": "hInfo" },
{ "data": "hStatus" }
],
"order": [[1, 'asc']]
} );
$('#opHistory tbody').on('click', 'tr', function () {
var tr = $(this).closest('tr');
var row = table.row( tr );
if ( row.child.isShown() ) {
row.child.hide();
tr.removeClass('shown');
}
else
{
row.child( format(), 'child' ).show();
tr.addClass('shown');
}
})
} );
Thank you.
This discussion has been closed.
Answers
Hi together,
had the same problem which I have resolved as follows:
1. Store current opened rows in array (global variable 'openRows')
2. Create a function 'closeOpenedRows()' with parameter 'table' and current selected 'row'
For usage, the 'else' tree of the event listener needs to be updated
1. first the new JS function is called to close all other open rows including update the expand/collapse icon
2. further, the current row be stored within the global variable for later comparison within JS function
Hope it helps someone. Perhaps there are even more elegant solutions. Feedback is welcome.
Regards,
Uwe