jQuery 刷新或重新加载数据表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11785494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
refresh or reload datatable
提问by user1480864
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?
我正在使用 Jquery 数据表,其中包括对列的自定义呈现。根据值,我必须禁用其中的某些控件。我想在发布后重新加载/刷新/重新绑定我的 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>
Update:**
更新:**
I found the answer:
我找到了答案:
clear the table ( fnClearTable )
add new data to the table ( fnAddData)
redraw the table ( fnDraw )
清除表格( fnClearTable )
向表中添加新数据 (fnAddData)
重绘表格 ( fnDraw )
采纳答案by user1480864
Ifound the answer:
我找到了答案:
clear the table ( fnClearTable )
add new data to the table ( fnAddData)
redraw the table ( fnDraw )
回答by Prateek
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();
回答by Serj Sagan
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 Initiallyto reinit the table:
现在只需执行您最初执行的调用以重新初始化表:
window.table = $('#TableID').dataTable({
...
});
回答by joy
var oTable = $('#groups').dataTable();
oTable.fnClearTable();
oTable.fnDestroy();
groupsList();
回答by Yush0
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.
就我而言,我只需要重绘它。但是您也可以在重绘之前清除和添加新数据。
回答by KRyan
You just call .dataTable()
on the table again, without any arguments.
你只需.dataTable()
再次调用表,没有任何参数。
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.
不要用参数再次调用它;它不喜欢那样。
回答by coderama
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.
那对我有用。它使用当前的 ajax url 配置重新加载网格。