0

I'm working on a JavaScript web application that uses DataTables to display a list of instructors. I'm having an issue where the DataTable doesn't update immediately after adding a new instructor to the underlying data array.

The Problem

When I add a new instructor through a form submission, the underlying data array updates correctly, but the DataTable doesn't reflect these changes until I manually refresh the page.

Code

I've reproduced this issue in both Vue.js and vanilla JavaScript. Here are CodePen links demonstrating the problem:

What I've Tried

  1. I've attempted to destroy and reinitialize the DataTable after adding new data:
updateDataTable() {
    if (this.dataTable) {
        this.dataTable.destroy();
    }
    this.$nextTick(() => {
        this.dataTable = $('#instructor-table').DataTable();
    });
}
  1. I've also tried using setTimeout to delay the reinitialization:
updateDataTable() {
    if (this.dataTable) {
        this.dataTable.destroy();
    }
    setTimeout(() => {
        this.dataTable = $('#instructor-table').DataTable();
    }, 0);
}
  1. I've made sure that the underlying data array (instructors) is updated correctly before calling updateDataTable().

Expected Behavior

I expect the DataTable to reflect the new data immediately after adding a new instructor, without requiring a page refresh.

Important Notes

  1. My main goal is to fix this issue in the Vue implementation but I noticed it happened on pure javascript .
  2. I don't want solutions that pass the instructors array directly to DataTables. The data should remain managed by Vue v-for.

Question

How can I update the DataTable in my application to reflect new data without requiring a page refresh? Is there a more efficient way to add new rows to a DataTable dynamically while keeping the data management within Vue?

Any help or guidance would be greatly appreciated. Thank you!

4
  • Have you set the parameters to listen for xhr events? datatables.net/manual/vue#Events Commented Sep 11, 2024 at 7:35
  • I'm not using XHR directly to load data into DataTables. Instead, I'm fetching the data via Axios in my Vue.js app and then binding it to the table. After fetching the data, I manually reinitialize DataTables to reflect changes. Commented Sep 11, 2024 at 7:41
  • "The data should remain managed by Vue v-for." - this is a bad idea overall. Using Vue and non-vue DOM libs like jquery together is quirky enough and should be avoided if possible, a lot of questions are dedicated to this. But what you're trying to do is to modify DOM that is rendered by Vue which can lead to unpredictable results. In this case your best bet is to minimize their friction with this.dataTable.destroy(); this.instructors.push(...); this.nextTick(() => this.dataTable = $('#instructor-table').DataTable()). And then hope that DataTable and Vue won't interfere in other ways Commented Sep 11, 2024 at 8:26
  • You could add a numbered key (or string) to your table tag instead, then change/update the key in your updateDataTable. I tried it in your codepen and it works for me.
    – Cerceis
    Commented Sep 11, 2024 at 8:49

2 Answers 2

2

To solve this issue you must to know Datatable working logic. Datatable doesn't support dom change since 2.0. You can add new data by using Datatable functions. Like sample below. There is a sample to add datatable row. Datatables

var table = new DataTable("#myTable");

table.row
  .add({
    name: "Tiger Nixon",
    position: "System Architect",
    salary: "$3,120",
    start_date: "2011/04/25",
    office: "Edinburgh",
    extn: "5421",
  })
  .draw();

Or you can destroy datatable and reinitialize it. I think your solution very simple. I only change the order updateDataTable(); renderInstructors(); in your codepen where it's inside submitForm function. You must to destroy datatable before update content. After that add new datas you can reinitialize the DataTable.

I'm sorry about my english. I hope I helped. I edit your function

function submitForm(event) {
  event.preventDefault();
  const newInstructor = {
    id: instructors.length + 1,
    firstName: document.getElementById("firstName").value,
    lastName: document.getElementById("lastName").value,
    email: document.getElementById("email").value,
  };
  instructors.push(newInstructor);
  hideModal();
  updateDataTable();
  renderInstructors();
}
0

This is without using model:

dataTable.row.add(['dwadwa','dwadwadaw','dawdawdawdaw']).draw()

This is using model:

dataTable.row.add([newInstructor.id,newInstructor.firstName + ' ' + newInstructor.lastName,newInstructor.email]).draw()

Add one of these lines of code in function submitForm(event) its simply adding a row to existing table with passed data in brackets. Because your json is not equal actual table cols u cant just add(newInstructor)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.