I try to build a table which is both a JQuery treeTable and a JQuery datatable at the same time. Attention please, my problem is not about how to use it both, i can view without problem if i fill the "table".
But when i send an empty array to my treetable building code, i am getting error.
Here are problem lines:
$('#table tbody tr').each(function(){
console.log(this);
if(mytable.fnGetData(mytable.fnGetPosition(this))[4]){
console.log('in set child before');
$(this).addClass('child-of-'+mytable.fnGetData(mytable.fnGetPosition(this))[4]);
console.log('in set child after');
}
$(this).attr('id', mytable.fnGetData(mytable.fnGetPosition(this))[0]);
});
When i do not populate the table, despite my wish, the process goes through to the above loop, and
console.log(this)
prints out:
<tr class="odd"><td valign="top" colspan="4" class="dataTables_empty">No data available in table</td></tr>
So it generates error, because the row data is not an expected one.
I want to ask, what is the most elegant way to control if it is a populated "data", or an empty warning row? Is checking the "class" for "dataTables_empty" an appropriate method?
Or is there any other way to not to go through above loop if table is empty.
How know if Datatable is empty
var table = $('#idTable').DataTable();
if ( ! table.data().any() ) {
alert( 'Empty table' );
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With