DataTable AMD initialization callback

DataTable AMD initialization callback

MojimiMojimi Posts: 4Questions: 2Answers: 0
edited January 2018 in Free community support

For example, how can I achieve this form of callback in DataTable?

There are several things I need to do on the DataTable object once its loaded the data, but the initComplete callback does not returns the DataTable itself...

The Draw event wouldn't be ideal as it would fire on sorting, etc...

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,700Questions: 26Answers: 4,842
    Answer ✓

    Are you trying to access the Datatables API in the initComplete callback?

    If so you can use this $('#example').DataTable(). For example to get the row count you can use this in initComplete:
    $('#example').DataTable().rows().count()

    Kevin

  • MojimiMojimi Posts: 4Questions: 2Answers: 0

    Yes that does it thanks.

    I personally find it weird that an on load event does not return said loaded object...

  • kthorngrenkthorngren Posts: 20,700Questions: 26Answers: 4,842
    edited January 2018

    I personally find it weird that an on load event does not return said loaded object

    initComplete is still part of the DataTable() function. Once the function is done, ie, after initCompete, the function will return an API instance In your case this will be in the dtable variable. Outside of the DataTable() function you can access the API using dtable.

    Kevin

  • allanallan Posts: 62,338Questions: 1Answers: 10,228 Site admin
    $(document).ready( function () {
      var dtable = $('#example').DataTable({
        data : [
            [1],
            [2],
            [3],
        ],
        initComplete: settings => {
            console.log(dtable); //dtable is undefined
        }
    });
    } );
    

    With this form of initialisation, the $().DataTable() call is synchronous - i.e. the $().DataTable() call hasn't finished being executed when initComplete executes, hence why dtable is still undefined.

    Using the context of the callback as Kevin suggested is the right way to do it.

    Allan

This discussion has been closed.