Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Datatable: Number of matching records by using search()

I'm searching my datatable

with the following statement:

var dataTable =  $(#dt).DataTable().search("searchpattern");
dataTable.draw();

So everything is fine and the datatable is just showing entries that matchs to the searchpattern.

But how i can determine, how many entries are matching to the pattern?

I've tried the following:

dataTable.rows().nodes().length;

but returned is always the number of all records and not the count of matching records. Thanks for your help in advise!

like image 874
user1829716 Avatar asked Dec 01 '22 18:12

user1829716


2 Answers

You can use this, as per get filtered rows:

dataTable.$('tr', {"filter":"applied"}).length;

like image 167
SSA Avatar answered Dec 03 '22 08:12

SSA


Since DataTables 1.10 , using dataTable.page.info() looks the best way to get current info (after processing/ search/ reload).

So..

var table = $('#example').DataTable();
var info = table.page.info();

console.log(info);

result :

{
    "page": 1,
    "pages": 6,
    "start": 10,
    "end": 20,
    "length": 10,
    "recordsTotal": 57,
    "recordsDisplay": 57,
    "serverSide": false
}

info.recordsDisplay is what you asking for. ref: page.info() at dataTables API.

like image 38
MohannadNaj Avatar answered Dec 03 '22 08:12

MohannadNaj