问题
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
$('.dataTableList').DataTable({ responsive: true, "autoWidth": false });
Detail.cshtml
<div class="inventory" data-url=@Url.Action("Inventory")>...</div>
Load div table:
$('.inventory').each(function (index, item) {
var url = $(item).data("url");
if (url && url.length > 0) {
$(item).load(url);
}
});
Controller-Action
public ActionResult Inventory()
{
return PartialView("Inventory", inventoryService.Get());
}
Partial Inventory view
<table class="table table-striped dataTableList">
<thead><tr><th></th></tr></thead> <tbody>...</tbody></table>
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.
回答1:
after loading your partial view html, try to call something like $('#partialViewDataTable').DataTable().ajax.reload();
来源:https://stackoverflow.com/questions/45745074/datatable-in-partial-view