Refresh table

Refresh table

cranworthcranworth Posts: 20Questions: 10Answers: 0
edited August 2014 in DataTables 1.10

I wish to refresh my data table upon the addition of a new row. Row addition is achieved with data collected from an input form and its data submitted via ajax (using JQuery). It might be worth mentioning that the data displayed by the datatable is NOT an ajax source but a javascript object array (i.e. setting the 'data' option in datatable's initialization).

var table_content = '[{"col-name-1":"data"}, {"col-name-2":"data"}]';   // content from php

 // table init
 var data_table = $('#div_selector').DataTable({
    data: table_content,  
    /*
    *    Other options omitted
    */ 
  });

After adding a row on the server, I update table_content with the new data. How do I now refresh the table?

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Use clear() and rows.add() to first clear the old data, and the secondly add the new data.

    Allan

  • cranworthcranworth Posts: 20Questions: 10Answers: 0
    edited August 2014

    Thanks. I tried clear() and rows.add(). I get this error message on chrome, Screenshot here: http://postimg.org/image/o5ryv9um9/

    It is strange because the data passed was in the same format as used in the initialization of the table.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Are you adding a single row? If so use row.add().

    Allan

  • cranworthcranworth Posts: 20Questions: 10Answers: 0
    edited August 2014

    I used clear() to clear the table and then rows.add() to add all rows. Doesn't update the table. It's strange. Here is a snippet of my code:

    data_table.clear().draw();          
    var table_data = eval(response); 
    data_table.rows.add(table_data).draw(); 
    

    the response is a string containing the javascript array declaration (returned from server).

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    I would probably need a link to the page to see what is happening then. That should work.

    Allan

  • cranworthcranworth Posts: 20Questions: 10Answers: 0
    edited August 2014

    I have PM the link to you. thanks

This discussion has been closed.