Load ajax after table inizialization

Load ajax after table inizialization

poisonspoisons Posts: 21Questions: 1Answers: 0

Is it possible to load the table content after the table initialization?

I mean I need to initialize the table

let mytable=$('#htmlTableId').DataTable({...});

and then, when a modal show up, load the table content by ajax, based on the button clicked to open the modal.

I would prefer to initiate the table on the body load and not on the modal shown, because I assign the Datatable to a variable, that I use intensively in other routines. How can I load the remote content only when the modal is shown?

The only way I found at the moment is loading the ajax config and let it return an empty result on page load, then load again and return proper content if called by the shown modal.

Replies

  • kthorngrenkthorngren Posts: 20,993Questions: 26Answers: 4,887
    edited June 2023

    You can use jQuery ajax() to fetch the data. Use the success function to execute rows.add() to place the fetched rows into the table. You can initialize an empty Datatable by not providing the ajax or data options.

    Kevin

  • poisonspoisons Posts: 21Questions: 1Answers: 0

    Thanks Kevin. Yes, I was looking also to this, just wondering if there is a built-in method to set the configuration in the initial initialization, and defer the proper loading to a delayed event.

  • kthorngrenkthorngren Posts: 20,993Questions: 26Answers: 4,887

    There is nothing built into Datatables to delay loading the table data. Here is an example of what I described:
    https://live.datatables.net/huyexejo/1672/edit

    Kevin

Sign In or Register to comment.