fnDraw appears to be asynchronous.

fnDraw appears to be asynchronous.

benwilsonbenwilson Posts: 12Questions: 0Answers: 0
edited September 2012 in General
I am trying to delete some rows on the server, then redraw my table, then get the number of rows remaining in the table. I am using server-side processing.

[code]

oTable.fnStandingRedraw();
var numRows = oTable.fnSettings().fnRecordsTotal();
...
[/code]

The problem is that my numRows is calculated before the table is redrawn, so it displays the number of rows prior to this operation. Is there a way to wait until the table is redrawn before getting the number of rows?

Replies

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    If you are using server-side processing, then yes, absolutely fnDraw is async, since it makes an Ajax call to the server :-).

    What you can do is listen for the 'draw' event that DataTables fires:

    [code]
    $(myTableSelector).one('draw', function () {
    ... number calculation;
    } );
    [/code]

    The jQuery 'one' method is very handy for situations such as this, where yo want to listen for the event once and then detect the listener - jQuery does that later part for you :-)

    Allan
  • benwilsonbenwilson Posts: 12Questions: 0Answers: 0
    Thanks, that's exactly what I needed.

    -Ben
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    No problem at all. I'm going to improving the events documentation as part of the 1.10 documentation revamp :-).

    Regards,
    Allan
This discussion has been closed.