Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if datatable is empty

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.

like image 914
merveotesi Avatar asked Dec 07 '12 10:12

merveotesi


1 Answers

How know if Datatable is empty

var table = $('#idTable').DataTable();

if ( ! table.data().any() ) {
    alert( 'Empty table' );
}
like image 162
David Zambrano Avatar answered Sep 28 '22 06:09

David Zambrano