How do I refresh the datatable after an error?

How do I refresh the datatable after an error?

sushmassushmas Posts: 22Questions: 5Answers: 2

Hi,

I have a datatable with editor where I change a field and update table accordingly. In case of an error, I would like to alert the user and then reload/redraw the table. I am right now just using the javascript window reload feature as I cannot get table.draw() to work. I am doing something wrong, please suggest -
Here is snippet of my code:

function update_table(mytable, new_data, old_data, success){
run_task( //call appropriate backend python function
'modify_comp',
{ var1,
  var2
},
function(taskid, ttime, result) {
    var myrow = {data: []} 
    myrow.data.push(result[1])
    if(result[0] == 'error') {
        alert('Nothing changed!')
        mytable.draw();
        //location.reload(true);
    } else{
        return success(myrow)
    }
},
function(taskid, ttime) {
    alert('update failed')
   //location.reload(true);
    return(old_data)
}
);
}


editor = new $.fn.dataTable.Editor( {
    ajax: function(method, url, d, success, error){
             if (d.action === 'edit'){
             // get data into editedRow
             }
        var c_table = $('#c-table').DataTable();
        update_table(c_table, editedRow, d.rowData, function(updates){
                 success(updates);
        });
},
table: "#c-table'
idSrc: "id",
fields: [ {
---
----
});

var c_table = $('#c-table').DataTable({
'data': [],
ajax: function (method, url, data, success, error){
    populate_table(); // Gets data, uses fnAddData to add all rows
},
'columns': [
....
....
});

Thank you

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    Hi,

    Are you able to give me a link to the page please? The above code looks like it should call the draw() method, but as it is just clips of the code it is hard to say for certain.

    Also, what error is given in the Javascript console when you do call draw()?

    Thanks,
    Allan

  • sushmassushmas Posts: 22Questions: 5Answers: 2

    Hi Allan,

    I don't think you will be able to access this page, I am testing on a local django server and that is within our secure environment.
    The draw() does not give me any error, but also does not redraw the table either. It just continues to hang or show progress of 'submit' button.
    Thank you

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Answer ✓

    Is there any error shown on the console at all? Are you aware that draw() will not fetch new data (unless you are using server-side processing) - reload.ajax() is used for that.

    However, there should really be no need to update the table in such a manner - Editor should be doing it for you. Is your Ajax request returning the data that Editor requires?

    Allan

  • sushmassushmas Posts: 22Questions: 5Answers: 2

    Thank you Allan. I got it.
    Earlier I had returned the old data( in case of no changes) but had not pushed it into data value.

This discussion has been closed.