局部视图中的数据表 [英] DataTable in partial view
问题描述
我在_layout中有一个数据表,该表可以在所有其他表上正常工作,除了我在局部视图中具有的那个特殊表外,也许是因为数据加载的方式。
I have datatable in _layout that works fine all other tables except this particular one I have in a partial view perhaps because of the way the data is loaded.
_layout .cshtml
_layout.cshtml
$('.dataTableList').DataTable({ responsive: true, "autoWidth": false });
Detail.cshtml
Detail.cshtml
<div class="inventory" data-url=@Url.Action("Inventory")>...</div>
加载div表:
$('.inventory').each(function (index, item) {
var url = $(item).data("url");
if (url && url.length > 0) {
$(item).load(url);
}
});
控制器动作
public ActionResult Inventory()
{
return PartialView("Inventory", inventoryService.Get());
}
部分库存视图
<table class="table table-striped dataTableList">
<thead><tr><th></th></tr></thead> <tbody>...</tbody></table>
如何从_layout获取数据表初始化以在此表上工作?我看到如果我仅在局部视图而不是布局中初始化数据表,则可以正常工作。但是随后,我将不得不在所有其他局部视图上分别对其进行初始化。一定有更好的方法。谢谢您的帮助。
How do I get datatable initialization from _layout to work on this table? I see that if i initialize datatable only in partial view and not in layout, it works fine. But then I would have to initialize it on all my other partial views individually. There has to be a better way. Would appreciate your help.
推荐答案
加载部分视图html后,尝试调用类似 $( '#partialViewDataTable')。DataTable()。ajax.reload();
after loading your partial view html, try to call something like $('#partialViewDataTable').DataTable().ajax.reload();
这篇关于局部视图中的数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!