How can I reload data in datatable after doing any CRUD operation?

How can I reload data in datatable after doing any CRUD operation?

krshkrsh Posts: 2Questions: 1Answers: 0

Hi,
I am developing an eletron app that uses a sqlite database. I use a query to fetch data and populate table data using its results. Whenever I do a CRUD operation I initialize table but despite change in database, no change in table data is occurred.
I use below code to populate table:

window.getData = function() {
  var db = require('./database/database');

  const results = db.prepare('SELECT name, family, mobile FROM customer').all();

  var html = '';

  for (const row of results) {
    html += '<tr>';
    html += '<td>';
    html += row.name;
    html += '</td>';
    html += '<td>';
    html += row.family;
    html += '</td>';
    html += '<td>';
    html += row.mobile;
    html += '</td>';
    html += '</tr>';
  }

  console.log(results);

  document.querySelector('#table_id > tbody').innerHTML = html;
};

and following to configure datatable:

$('#table_id').DataTable({
      retrieve: true
    });

Is there any way to reload data in datatable for my case?

Thanks in advance

Answers

  • krshkrsh Posts: 2Questions: 1Answers: 0

    Hello everybody,
    I finally solved my problem easily. Actually as Electron is used to build desktop applications, so there is no ajax-based solution to render recent changes of the database (SQLite in my case) because there is no Server. In order to reload data in datatable after every change I simply remove existing data and add new data (gathered from a database query) using following code lines:

    table.clear().draw();
    table.rows.add(data).draw();
    

    I posted it in case anyone had the same problem could use it.
    Thanks

This discussion has been closed.