Collapse and expand row group not working when using .DataTable().clear().destroy();
Collapse and expand row group not working when using .DataTable().clear().destroy();
bbrindza
Posts: 316Questions: 73Answers: 1
I'm reloading my table using a dropdown with a year selection. The first time in the RowGroup expand and collapse function works. When I reload the table, that functionality is disable. Any thoughts.
The RowGroup expand and collapse function
// Expand/Collaspe Groups
$('#timeLogTable_OutOfOffice tbody').on('click', 'tr.dtrg-start', function () {
var pos = $('div.dataTables_scrollBody').scrollTop();
//console.log(pos)
var name = $(this).data('name');
collapsedGroups[name] = !collapsedGroups[name];
table.draw('page');
});
The DataTable().clear().destroy(); function
function reloadOutOfOficeTable(){
$("#timeLogTable_OutOfOffice").DataTable().off( 'select deselect');
$("#timeLogTable_OutOfOffice").DataTable().clear().destroy();
loadTable()
}
This discussion has been closed.
Answers
Can you get a live.datatable.net version running? I have a few theories but without seeing the
loadTable()
function I can't say for sure.CollapsedGroups is not part of the DataTable, so it does not get cleared by
clear()
ordestroy()
. This may mean you have collapsedGroups that are no longer valid, or may not be tied to your table any longer.I see you're setting the collapsedGroups[name] to the inverse of what it currently is, because collapsedGroups are remaining this may be setting previously collapsed groups to open, which is not what you would expect.
You're turning off select and deselect for the reload, I assume you turn them back on and I don't think that is the reason, but like I said without seeing the entire table interacting I can't say for sure.
Unfortunately I can not provide a live.datatable.net example due to the use of ssp.
However here is the rest of the HTML and JS
Have you verified your
click
event is being called?Kevin
Yes, it is being called. Tested in console.log
You may need to reset the
collapsedGroups
variable in thereloadOutOfOficeTable()
function. Maybe simply usingcollapsedGroups = {};
will work. If this doesn't help then you will need to trace through therowGroup.startRender
function to understand where the problem is. Or post a link to your page or a test case with an example of your data so we can help debug. We don't need the exact SSP data just need some Javascript data to fill the table. See this Javascript sourced data example.Kevin
Thanks for the input Kevin, I will let you know how it goes.
It might be because you are destroying the table. In doing so your on click event is no longer associated with the previously created table.
I don't use destroy. Once I create the table and any events for that table, I only reload my data. After the data is loaded, then I clear the old data, add the new data and draw it to display. This can all be chained like below: