Unable to find DataTable count when i search records

Unable to find DataTable count when i search records

TalhaDXTalhaDX Posts: 3Questions: 1Answers: 0
edited January 2019 in Free community support

I have used various filters on my data table and I an easily extract the number of rows now the table has using:

var count = $('#myTable').DataTable().data().count();

or just by getting length of the final filtered list.

It does not return right result and stays with the previous result when I search using the search box. Here is my search box implementation:

$('#searchText').keyup(function(){
            table.search($(this).val()).draw();
});

When i use the data().count() after this draw() method. It sticks with previous values.

How can i get the updated table count in this after the search has been performed ?

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Hi @TalhaDX ,

    It's easiest to use page.info() for that info - see example here.

    Cheers,

    Colin

  • TalhaDXTalhaDX Posts: 3Questions: 1Answers: 0
    edited January 2019

    @colin

    My Data table is already initialized. So when I call this on searchText keyup method or even in document.ready it throws an error:

    DataTables warning: table id=myTable - Cannot reinitialise DataTable. For more information about this error, please see http://datatables.net/tn/3

    How can i use this callback internally without initializing again.

    PS: I am not using the data table, default search box.

  • colincolin Posts: 15,142Questions: 1Answers: 2,586
    Answer ✓

    Before, where you had

    $('#myTable').DataTable().data().count();
    

    replace that with

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

    Cheers,

    Colin

  • TalhaDXTalhaDX Posts: 3Questions: 1Answers: 0

    Ahh finally. :smile: Great thanks

This discussion has been closed.