jQuery 检查数据表是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13760999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:56:39  来源:igfitidea点击:

Checking if datatable is empty

jquerydatatable

提问by merveotesi

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".

我尝试构建一个表,它同时是 JQuery 树表和 JQuery 数据表。请注意,我的问题不在于如何同时使用它,如果我填写“表格”,我可以毫无问题地查看。

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:

console.log(this)打印出来:

<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?

我想问一下,控制它是填充的“数据”还是空的警告行的最优雅的方法是什么?检查“dataTables_empty”的“class”是否合适?

Or is there any other way to not to go through above loop if table is empty.

或者,如果表为空,是否还有其他方法可以不通过上述循环。

回答by David Zambrano

How know if Datatable is empty

如何知道数据表是否为空

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

if ( ! table.data().any() ) {
    alert( 'Empty table' );
}

回答by Dev

You can also check if dataTable is empty using page.info() as described in this stackoverflow post. eg.

您还可以使用 page.info() 检查 dataTable 是否为空,如此stackoverflow post 中所述。例如。

    //gives the total number of filtered records
    var totalDisplayRecord = $(".dTable").DataTable().page.info().recordsDisplay
(totalDisplayRecord === 0)? alert("table is empty") : alert("table is not empty");

or

或者

//gives the total number of records in table
var totalRecords =$(".dTable").DataTable().page.info().recordsTotal;
//test if dataTable is empty
(totalRecords === 0)? alert("table is empty") : alert("table is not empty");

回答by Ulrich Dohou

Another way

其它的办法

var count = $("table.dTable").dataTable().fnSettings().aoData.length;
if (count == 0)
{
   alert("Please enter the Data");
}else{
   alert("Table contains " + count + ' rows');
}
<table class="dTable"></table>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.16/js/jquery.dataTables.min.js"></script>

回答by elad silver

from the forumthis might be what you're looking for:

论坛这可能是你要找的:

table.fnSettings().aoData.length

gives You the length of data in table. So if it will be equal to 0 then there is no data.

给你表中数据的长度。因此,如果它将等于 0,则没有数据。

    if(table.fnSettings().aoData.length===0) {
        alert('no data');
    } else {
        alert('data exists!');
    }