How to know if DataTable is ready?

How to know if DataTable is ready?

WarbounthoWarbountho Posts: 17Questions: 7Answers: 0

Hi,

I test my app with Cypress, i often struggle to write tests with DataTables because it's hard to know when the instance is fully loaded.

How, with HTML only may i know if my instance is ready? Is a class added? Or will it be possible to add a class to the DataTable container when the instance is ready? I cannot rely on events.

Thanks!

Answers

  • kthorngrenkthorngren Posts: 20,938Questions: 26Answers: 4,875

    initComplete and init execute when the Datatables initialization is complete. You could add a class or whatever you need to do in one of those events to perform the actions needed when Datatables is ready.

    Kevin

  • WarbounthoWarbountho Posts: 17Questions: 7Answers: 0

    In fact i tried with initComplete but it's not working... maybe because my table use ajax? I will look for ajax events.

  • WarbounthoWarbountho Posts: 17Questions: 7Answers: 0

    OK i just figured out that if you search something that matches all rows, you don't have the placeholder 'Filtered from...' and i was relying on this. :/

  • kthorngrenkthorngren Posts: 20,938Questions: 26Answers: 4,875
    edited November 2023

    What did you try and what exactly is not working?

    I think you will find the xhr event executes before initComplete so the table initialization, ie populating the rows, has not completed when xhr fires. See this example:
    https://live.datatables.net/ralujoqo/1/edit

    Maybe update the test case to show what is not working for you.

    Kevin

  • WarbounthoWarbountho Posts: 17Questions: 7Answers: 0

    Thanks for you help, in fact i was looking at the wrong place,

    i assumed that "filtered from" text was always shown even if search matches all rows but it's not the case.

    Don't this text should be always shown when a filter is applied?

  • kthorngrenkthorngren Posts: 20,938Questions: 26Answers: 4,875

    Don't this text should be always shown when a filter is applied?

    Yes but unless you are using search or searchCols there won't be any initial filtering.

    What are you doing that would cause the table to be initially filtered?

    Maybe you can post some code showing what you are doing.

    Kevin

  • kthorngrenkthorngren Posts: 20,938Questions: 26Answers: 4,875

    You mentioned adding a class. How about something like this:
    https://live.datatables.net/ralujoqo/2/edit

    Kevin

Sign In or Register to comment.