7
votes

I'm using Jquery Datatable which includes customized rendering for columns. Based on values, I've to disable certain control in it. I want to reload/refresh/rebind my jquery datatable after post. How can I do that?

**Controller:**

    [HttpPost]
    public JsonResult PostAction(MyMOdel model)
    {
         //save changes to DB
        return Json(new
        {
            Success = result,
        });
    }

 public ActionResult MyAction()
   //grab records from DB and return JSON
 }

**View:**

@using (Ajax.BeginForm("PostAction", "ControllerName", null,
        new AjaxOptions
        {
            UpdateTargetId = "update-message",
            InsertionMode = InsertionMode.Replace,
            HttpMethod = "POST",
            OnSuccess = "updateSuccess"
        }, new { @id = "myForm"

 }
        ))
{
<table id="myTbl" class="display"><tr><td>col1</td></tr></table>
}

<script type="text/javascript">
        var oTable = $('#myTbl').dataTable({
                     "sAjaxSource": "/ControllerName/MyAction",
                      <!-- more config -->

    function updateSuccess(data, status, xhr) {
        //refresh datatable;

    }
</script>

Update:**

I found the answer:

  • clear the table ( fnClearTable )

  • add new data to the table ( fnAddData)

  • redraw the table ( fnDraw )

7
Please include your code here so we can see what's going on. Do try to pare it down to just the minimal code necessary to see what's going on.KRyan

7 Answers

19
votes

First just get datatable reference, using

var oTable = $("#someTable").dataTable();

After that do whatever, you want to :

Clear the table : oTable.fnClearTable();

Redraw the table : oTable.fnDraw();

Add new data to the table : oTable.fnAddData();
2
votes

Ifound the answer:

clear the table ( fnClearTable )

add new data to the table ( fnAddData)

redraw the table ( fnDraw )
2
votes

This worked for me:

// Initially
window.table = $('#TableID').dataTable({
                    ...
                });

// Later, when table needs to be refreshed
window.table.fnDestroy(); 
// depending on the version, maybe just .destroy() instead of .fnDestroy();

Now just do the call you did Initially to reinit the table:

window.table = $('#TableID').dataTable({
                    ...
                });
0
votes

You just call .dataTable() on the table again, without any arguments.

So if you had done something like this:

$('#someTable').dataTable({
    someAttribue: someValue
});

You can then later do this to refresh it:

$('#someTable').dataTable();

Don't call it again with the arguments; it doesn't like that.

0
votes
var oTable = $('#groups').dataTable();
oTable.fnClearTable();
oTable.fnDestroy();
groupsList();
0
votes

For newer Datatable Versions this will do the Job:

var table = $('#yourDataTable').DataTable({...});
table.columns.adjust().draw();

In my case I just needed to redraw it. But you can also clear and add new data before you redraw.

-1
votes

Are you sure this is not what you are looking for:

oTable.fnReloadAjax();

That worked for me. It reloads the grid using the current ajax url config.