刷新或重新加载数据表

我正在使用Jquery Datatable,其中包括列的自定义渲染。 基于值,我将禁用其中的某些控件。 我希望在发布后重新加载/刷新/重新绑定我的jquery数据表。 我怎样才能做到这一点?

**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" } )) { 
col1
} var oTable = $('#myTbl').dataTable({ "sAjaxSource": "/ControllerName/MyAction", function updateSuccess(data, status, xhr) { //refresh datatable; }

更新: * *

我找到了答案:

  • 清除表(fnClearTable)

  • 向表中添加新数据(fnAddData)

  • 重绘表格(fnDraw)

首先获取数据表引用,使用

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

在那之后做任何事情,你想:

 Clear the table : oTable.fnClearTable(); Redraw the table : oTable.fnDraw(); Add new data to the table : oTable.fnAddData(); 

如果答案是:

 clear the table ( fnClearTable ) add new data to the table ( fnAddData) redraw the table ( fnDraw ) 

这对我有用:

 // 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(); 

现在只需执行您所做的调用最初重新启动表:

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

你只需再次调用表上的.dataTable() ,不带任何参数。

所以如果你做过这样的事情:

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

然后,您可以稍后执行此操作以刷新它:

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

不要再用参数调用它; 它不喜欢那样。

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

对于较新的数据表版本,这将执行作业:

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

在我的情况下,我只需要重绘它。 但您也可以在重绘之前清除并添加新数据。

你确定这不是你想要的:

 oTable.fnReloadAjax(); 

这对我有用。 它使用当前的ajax url配置重新加载网格。