ready() API documentation suggestion

ready() API documentation suggestion

kthorngrenkthorngren Posts: 21,571Questions: 26Answers: 4,996

I was providing an example of using ready() with multiple tables. The docs have this comment:

Scope is set to an API instance for the DataTable in question.

It took me a bit to figure out what this meant and how to get the API instance - I'm a bit slow :smile: The suggestion is to provide an example using multiple tables. For example:

let tables = new DataTable('.display');
 
tables.ready(function () {
    // Get the API instance
    let table = $( this ).DataTable();

    // Actions to take when the table is ready
    // ...
});

Kevin

Replies

  • allanallan Posts: 63,872Questions: 1Answers: 10,527 Site admin

    Hi Kevin,

    Good point - that requires some clarity and an example! However, it actually goes further than what your example has - this is already the API instance - e.g. the following would be equivalent to your example:

    let tables = new DataTable('.display');
      
    tables.ready(function () {
        // Get the API instance
        let table = this;
     
        // Actions to take when the table is ready
        // ...
        
        this.draw();
        // or
        table.draw();
        // both are the same thing with the above.
    });
    

    No need to create a new API instance, the scope (this) already is the API instance.

    Allan

  • kthorngrenkthorngren Posts: 21,571Questions: 26Answers: 4,996

    I tried that and it doesn't work. These errors appear depending on the form used:

    Uncaught TypeError: this.draw is not a function

    Uncaught TypeError: table.draw is not a function

    Test case:
    https://live.datatables.net/socirone/36/edit

    Kevin

  • kthorngrenkthorngren Posts: 21,571Questions: 26Answers: 4,996

    It looks like this is the table element not the API:
    https://live.datatables.net/gixezuwa/1/edit

    See the console output.

    Kevin

  • allanallan Posts: 63,872Questions: 1Answers: 10,527 Site admin

    Oooo - I think you might have a bug there. I need to dash off atm (trees to chop down on a cold Scottish morning...), but I'll take a look when I get back.

    Allan

  • allanallan Posts: 63,872Questions: 1Answers: 10,527 Site admin

    Yup, most certainly a bug. The context of the callback was incorrect if the table wasn't yet "ready" (it was correct if it was already "ready").

    I've committed a fix with a couple of unit tests.

    Thanks for flagging this up and the excellent example as always!

    Allan

  • kthorngrenkthorngren Posts: 21,571Questions: 26Answers: 4,996

    Good thanks!

    Kevin

Sign In or Register to comment.