4

I'm using the jquery DataTables plugin to display an array of objects. I would like to clear the table and redraw using the changed data. However, the only way I've been able to do that so far is to destroy the table and reinitialize. I'd like to know if there is a simple way to refresh from JS data source. This is what I'm doing, it works but feels wrong...

if (NAMESPACE.table){
  NAMESPACE.table.destroy();
}

NAMESPACE.table = $('#assets-table').DataTable({
  "data": filteredData,
  "columns": [
      { "data": "id" },
      { "data": "type" },
      { "data": "city" },
      { "data": "state"}
  ]
 });

2 Answers 2

6

if you want to clear the data present in dataTable simply call table.clear() it clears all cells in table.

and then add new data using table.row.add().draw();

table.destroy() does not removes data present in cell of table, it only destroys the current instance of dataTable you created.

Sign up to request clarification or add additional context in comments.

1 Comment

It seems so inefficient to do that, clearing everything when you just want to add a row any way. The problem is I want the user to remove items, then probably this is the only way, since there is no table.row.remove API which is a bit weird to me? But I just needed somebody to say this is the only way.. so here we go. Thanks +1
5

Make it simpler:

NAMESPACE.table = $('#assets-table').DataTable({
  "data": filteredData,
  "columns": [
      { "data": "id" },
      { "data": "type" },
      { "data": "city" },
      { "data": "state"}
  ],
 "destroy": true
 });

2 Comments

Nice, that is cleaner.
Great Solution!!

Your Answer

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.