Accessing datatableData inside fnDrawCallback

Accessing datatableData inside fnDrawCallback

soroushhakamisoroushhakami Posts: 10Questions: 0Answers: 0
edited September 2011 in General
[code]$(document).ready( function() {
var oTable = $('#example').dataTable( {
"fnDrawCallback": function() {
var data = oTable.fnGetData();
}
} );
} )[/code]

This gives me a 'can't call fnGetData on undefined'.

As I understood it, this is the purpose of fnDrawCallback, to be able to manipulate the created DOM, but clearly I misunderstood things. The API is quite brief on this ( " This function is called on every 'draw' event, and allows you to dynamically modify any aspect you want about the created DOM. " ). What I'm looking for is something equivalent of a callback-function I guess. Could anyone give me some clues on how to be able to solve this problem?

Thanks in advance

Replies

  • allanallan Posts: 63,530Questions: 1Answers: 10,474 Site admin
    That's because fnDrawCallback is called before oTable has been assigned (i.e. before the dataTable() function has finished everything it needs to do). This is just the why Javascript works. However the answer is quite easy :-). Since all callback functions in DataTables are executed with the scope of the original instance you can use the "this" keyword. For example:

    [code]
    $(document).ready( function() {
    var oTable = $('#example').dataTable( {
    "fnDrawCallback": function() {
    var data = this.fnGetData();
    }
    } );
    } )
    [/code]

    Allan
  • soroushhakamisoroushhakami Posts: 10Questions: 0Answers: 0
    Ah, but ofcourse. Thanks alot Allan!
This discussion has been closed.