any() option
any() option
marisatraini
Posts: 10Questions: 3Answers: 0
the any() option dosen't work because if the table is empty, however the table has one row for the message of attribute "emptyTable".
the same with count()
Answers
I just tried both the
any()
andcount()
with an empty table in this test case:https://live.datatables.net/xohutequ/1/edit
They both seem to work as expected, for example:
Please provide more details about the issue you are seeing. Better is a link to a test case (update mine if you wish) to show the issue.
Kevin
Yes, the control if with the option count() works well, but I was deceived by the alert which returns the value 0 even when there is a valid row in the table. This is the code:
What alert? There are several possible error conditions.
If you link to a test case as Kevin requested, we'd be happy to help.
Allan
Do you mean the alert in your code on line 3? As Kevin's test case shows, an empty table will give a count of 0 there.
Allan
Just to make sure I'm clear when you say a valid row in the table are you referring to the row that shows the
No data available in table
message? The Datatables API's likeapi data()
,rows().data()
, etc concern themselves with the actual table data. For this case Datatables doesn't consider theNo data available in table
row as table data. Its a row that Datatables inserts to show the message but is not counted as part of the table data. Note that the information element also shows 0 rows, ieShowing 0 to 0 of 0 entries
.If the table has actual table data but
data()
returns 0 rows then likely you are populating the table after Datatables is initialized using Javascript or jQuery methods. Datatables won't know about this. See this FAQ for details of how to append rows to the table.If neither of these helps then please provide a link to your page or a test case showing the issue so we can help debug.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
Thank you Allan and Kthorngren.
Yes Allan, I was referring to line 3 when there is at least a valid record in the table no empty table.
Now I see my problem: I initialize the table with ajax immediately before the alert statement and/or the if control that I reported in the code above.
It seems that data().count() does not have enough time to realize that the table has valid rows. The code refers to the "btn_apri_tutto" button which I would like to enable/disable depending on whether the table has valid records or it is empty. What can I do? Thank you for your help.
Wait for the table to become ready with the
initComplete
callback function. Run yourtable.data().count()
check in there.Allan
Thanks for your help. I tried and it works fine.