I am using jQuery datatables in my AngularJS app. On success of my GET API call, I am setting the result to a scope variable and using a timeout to initialize the datatable.
$scope.successCallbackOfAPI = function(data) {
$timeout(function() {
$scope.items = angular.copy(data);
$("#myDatatable").DataTable();
}, 200);
};
I have a scenario where I have to call the API again and refresh the datatable with the new data from the API response. In this case, I will be calling the same callback to reinitialize the datatable.
The datatable still hold the previous data before refreshing even though the scope variables are getting updated.
Is there a way to achieve this without using a directive?
-
Why are you using timeout for? Is it because $("#myDatatable").DataTable() is asynchronous?ocket-san– ocket-san2016年02月09日 10:24:51 +00:00Commented Feb 9, 2016 at 10:24
-
Yes. It doesn't initalize otherwiseJaseem Abbas– Jaseem Abbas2016年02月09日 10:26:27 +00:00Commented Feb 9, 2016 at 10:26
-
i can suggest you to try angular-datatables. this case seems to be good for youVanojx1– Vanojx12016年02月09日 10:27:44 +00:00Commented Feb 9, 2016 at 10:27
-
Need the html to be able to answerWalfrat– Walfrat2016年02月09日 10:31:21 +00:00Commented Feb 9, 2016 at 10:31
2 Answers 2
When I had this problem I created different function to update my table. It had this part of code:
dataTable.clear().draw()
dataTable.rows.add( NEWDATA );
dataTable.columns.adjust().draw();
Hope it helps
Comments
// Apply this code before the api call or in the render() function.
var table = $('#table_id').DataTable();
table.clear().draw(); // To reset the table