Passing DataTable object to initComplete callback

Passing DataTable object to initComplete callback

dabeedabee Posts: 19Questions: 3Answers: 0

Hi @alan
Thank you for your great work!

Can't figure out
Let's say I have this code:

function afterTableInitialization(table) {
    table.columns().every( function () {
          // any code ...
    });
}

table = $('table').DataTable({
     "initComplete": afterTableInitialization (table)
});

And I certainly get Uncaught ReferenceError: table is not defined
I do this with external callback cause of using ajax to load table contants and then initiating DataTables itself

So the question is - how do I pass initialized DataTable object to my callback so I can operate it via API?

Thanks in advance

Replies

  • dabeedabee Posts: 19Questions: 3Answers: 0

    Well I guess I've just figured it out myself.
    So the code should look like this:

    function afterTableInitialization(settings, json) {
        table = settings.oInstance.api();
        table.columns().every( function () {
              // any code ...
        });
    }
     
    table = $('table').DataTable({
         "initComplete":  function (settings, json) {
                        afterTableInitialization(settings,json)
          }
    });
    

    So the main changes is that I pass settings to my callback (json is optional in this case) and then get DataTables object out of it with table = settings.oInstance.api();

    Hope this will help someone:)

This discussion has been closed.