user1829716
user1829716

Reputation: 307

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!

Upvotes: 5

Views: 9383

Answers (3)

MohannadNaj
MohannadNaj

Reputation: 1079

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.

Upvotes: 9

SSA
SSA

Reputation: 5493

You can use this, as per get filtered rows:

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

Upvotes: 10

Greg
Greg

Reputation: 8784

try something like this:

...
var total_row_count = dataTable.fnSettings().fnRecordsTotal();
var filtered_row_count = dataTable.fnSettings().fnRecordsDisplay();
console.log(total_row_count + " total rows filtered down to " + filtered_row_count);

http://datatables.net/forums/discussion/2278/how-to-get-number-of-rows

Upvotes: 2

Related Questions