1

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?

asked Feb 9, 2016 at 10:14
4
  • Why are you using timeout for? Is it because $("#myDatatable").DataTable() is asynchronous? Commented Feb 9, 2016 at 10:24
  • Yes. It doesn't initalize otherwise Commented Feb 9, 2016 at 10:26
  • i can suggest you to try angular-datatables. this case seems to be good for you Commented Feb 9, 2016 at 10:27
  • Need the html to be able to answer Commented Feb 9, 2016 at 10:31

2 Answers 2

1

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

answered Feb 9, 2016 at 10:31
Sign up to request clarification or add additional context in comments.

Comments

0
// Apply this code before the api call or in the render() function.
var table = $('#table_id').DataTable();
 
table.clear().draw(); // To reset the table
Tyler2P
2,37030 gold badges26 silver badges34 bronze badges
answered Feb 7, 2024 at 15:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.