刷新或重新加载数据表

时间:2012-08-02 20:47:03

标签: jquery datatable

我正在使用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"

 }
        ))
{
<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>

更新: * *

我找到了答案:

  • 清除表格(fnClearTable)

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

  • 重绘表格(fnDraw)

7 个答案:

答案 0 :(得分:19)

首先使用

获取数据表引用
var oTable = $("#someTable").dataTable();

之后做任何事,你想:

Clear the table : oTable.fnClearTable();

Redraw the table : oTable.fnDraw();

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

答案 1 :(得分:2)

如果答案是:

clear the table ( fnClearTable )

add new data to the table ( fnAddData)

redraw the table ( fnDraw )

答案 2 :(得分:2)

这对我有用:

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

答案 3 :(得分:0)

您只需再次在桌面上调用.dataTable(),无需任何参数。

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

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

然后您可以稍后刷新它:

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

请勿使用参数再次调用它;它不喜欢那样。

答案 4 :(得分:0)

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

答案 5 :(得分:0)

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

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

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

答案 6 :(得分:-1)

你确定这不是你想要的:

oTable.fnReloadAjax();

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