I'm trying to make a Button which closes onClick all opened tr in my Datatable.
I'm opening the rows with the following command :
oTable.fnOpen(nTr, fnFormatDetails(oTable, nTr), 'newtr');
Is there a simply possibility to close all opened tr rows in my Datatable?
I think your trying to accomplish the same thing i was which is when i open a second row i want the previous row to close.
Meaning i would only be able to display one rows details at a time.
$(document).ready(function () {
var previousTr;
oTable = $('#bookingstable').dataTable
({
"aoColumnDefs": [
{ "bVisible": false, "aTargets": [2, 6, 7, 8, 9, 12, 13, 14, 15] }
]
});
$('#bookingstable tbody td').live('click', function () {
var selectedTr = $(this).parents('tr')[0];
if (oTable.fnIsOpen(previousTr) && previousTr != selectedTr)) {
oTable.fnClose(previousTr);
}
if (oTable.fnIsOpen(selectedTr)) {
oTable.fnClose(selectedTr);
}
else {
oTable.fnOpen(selectedTr, fnDetailsRow(selectedTr), 'details-row');
previousTr = selectedTr;
}
});
});
function fnDetailsRow(selectedTr) {
var TrData = oTable.fnGetData(selectedTr);
var detailsRow = '<table border="0">';
detailsRow += '<tr><td class="space" rowspan="4"><br /><h1>⇉</h1></td><td class="LabelField">Passenger Name</td><td class="ValueField">' + TrData[3] + '</td><td class="LabelField" rowspan="2">Pickup Details</td><td class="ValueField" rowspan="2">' + TrData[14] + '</td><td class="LabelField" rowspan="2">Customer Notes</td><td class="ValueField" rowspan="2">' + TrData[9] + '</td></tr>';
detailsRow += '<tr><td class="LabelField">Phone Number</td><td class="ValueField">' + TrData[2] + '</td></tr>';
detailsRow += '<tr><td class="LabelField">Car Type</td><td class="ValueField">' + TrData[6] + '</td><td class="LabelField" rowspan="2">Dropoff Details</td><td class="ValueField" rowspan="2">' + TrData[15] + '</td><td class="LabelField" rowspan="2">Office Notes</td><td class="ValueField" rowspan="2">' + TrData[12] + '</td></tr>';
detailsRow += '<tr><td class="LabelField">Pax and Bags</td><td class="ValueField">' + TrData[7] + ' Paxs & ' + TrData[8] + ' Bags' + '</td></tr>';
return detailsRow;
}
Notice I am declaring a var previousTr
on document ready and checking it before opening any 2nd row. remembering of course to set it again once you've opened a new row.
After having initialized your datatable, you can bind click event on all tr and close selected row using :
oTable.$('tr').click(function(){
if ( oTable.fnIsOpen(this) ) {
oTable.fnClose( this );
}
});
Please see here for further documentation about datatable API.