Skip to content
Advertisement

jQuery DataTables refresh the grid when the datasource is an array

I have a datatable that should be refreshed every time I click a button.

By clicking this button I will load, using ajax, a JSON stream from an API. The data will be processed and then set as datasource in the datatables.

I can’t use the ajax builtin feature of datatables.

The code is the following (for simplicity the data process function has been removed):

JavaScript

The above function is called when the user clicks on some buttons, passing different categoryID values, therefore loading different data from the API.

The data returned from the API is like the following:

JavaScript

At the first load the datatable works fine and I can see the data.

At the second click, the ajax call works, as I can see the response in the network tab, but the datatable is not updated. I’ve tried all the suggestions in the documentation but none of them works and they generally refer to the ajax builtin feature of the datatable.

Advertisement

Answer

try

JavaScript

instead of

JavaScript

or destroy and reinitialize the datatable.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement