Refresh datatable after method call in MVC

Refresh datatable after method call in MVC

tlnltlnl Posts: 13Questions: 0Answers: 0
edited October 2011 in General
I have a datatable that loads ex. all my customers. After you click create new customer I want to refresh my datatable- get the new data from the database.
I see i can call fnReloadAjax() to reload the hash.
1. Where do I put this call. I would need to put datatable code in the view but I want it executed based on the controller.
2. What is the complete code to call this on my datatable?
This is what originally loads the datatable.

Thank you

oTable = $('#salespeople').dataTable({
"bProcessing": true,
"sPaginationType" : "full_numbers",
"bJQueryUI": true,
"bServerSide": true,
"sAjaxSource": '/Manage/GetSales',
"aoColumns": [
null,
null,
null,
null,
null,
{ "bSortable": false, "bSearchable": false,
"fnRender": function (oObj) {
return '';
}
}


]
});

Replies

  • GregPGregP Posts: 500Questions: 10Answers: 0
    oTable.fnReloadAjax() should do it. But you have ServerSide set to true; you are likely better off with oTable.fnDraw() which will redraw the table with whatever data is available at /Manage/GetSales
  • tlnltlnl Posts: 13Questions: 0Answers: 0
    Ok in regards to the different method but I am still unclear as to where in the code this line goes.
    I am new to MVC and datatables.
    I would love to put in my controller after you add new refresh oTable.fnReloadAjax() but I know this code cant go in a controller it needs to go in the view.
    Can you tell me the exact code I need to add ot the view and what code in the controller I need to add to trigger that code?

    THank you again
  • GregPGregP Posts: 500Questions: 10Answers: 0
    Think of oTable.fnDraw() as a replacement for oTable.fnReloadAjax(). You do not need both. fnDraw() is the way to go when you're using server-side processing.

    I'm not sure how your MVC is set up. The function call oTable.fnDraw() is a controller. The button you bind it to is in the view.

    So somewhere in the view you have Add This User or something similar.

    And somewhere in the controller you should have something similar to this (using .delegate, .live, .bind, .click as I'm using, or whatever)

    [code]
    $('.add_new').click(function() {
    // Do all the stuff that happens after clicking "Add This User"
    oTable.fnDraw(); // do the table redraw
    });
    [/code]
  • tlnltlnl Posts: 13Questions: 0Answers: 0
    ok, thank you
This discussion has been closed.