DataTable in partial view

ぐ巨炮叔叔 提交于 2019-12-14 03:06:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!