代码之家  ›  专栏  ›  技术社区  ›  Ehsan Akbar

ASP.NET MVC中的数据表自动刷新

  •  0
  • Ehsan Akbar  · 技术社区  · 6 年前

    我有一张桌子,你可以看到:

    <table id="DataTable" class="table table-condensed">
        <thead>
            <tr class="headings">
                <th >نام و نام خانوادگی </th>
                <th></th>
                <th>وقت ملاقات </th>
                <th>ملاقات شونده </th>
                <th>زمان ورود </th>
                <th>زمان خروج </th>
                <th>عملیات</th>
            </tr>
        </thead>
        <tbody>
        @foreach (ReferralTrafficView item in Model)
        {
        }
        </tbody>
    </table>
    

    这是我的数据表配置:

    <script>
        $(document).ready(function () {
            $('#DataTable').DataTable({
                "paging": true,
                "pageLength": 10,
                "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "همه"]],
                "ordering": true,
                "processing": true,
                "info": true,
                "dom": "<'row'<'col-md-6 text-right'f><'col-md-6 text-left'>>"
                + "<'row'<'col-md-12'tr>>" +
                "<'row'<'col-md-5 text-right rtl'p> <'col-md-7 text-left'i>>",
                "language": {
                    "search": "_INPUT_",
                    "searchPlaceholder": "جستجو...", "sLoadingRecords": "در حال دریافت اطلاعات...", "sProcessing": "در حال پردازش...",
                    "lengthMenu": "نمایش _MENU_ ردیف در هر صفحه",
                    "zeroRecords": "موردی یافت نشد!",
                    "info": "نمایش صفحه _PAGE_ از _PAGES_",
                    "infoEmpty": "داده قابل نمایش موجود نیست.",
                    "infoFiltered": "(_TOTAL_ مورد از کل _MAX_ یافت شد)",
                    "oPaginate": {
                        "sNext": "<i class='fa fa-angle-left'></i>",
                        "sPrevious": "<i class='fa fa-angle-right'></i>"
                    }
                }
    
            });
        });
    
    </script>
    

    我在模型中的数据每次都被更改,我想刷新我的数据表而不刷新。这是我的控制器:

     public ActionResult MyReception(string Id)
            {
    
                List<ReferralTrafficView> rf = new List<ReferralTrafficView>();
    get data from database
    return rf
    }
    

    我可以在datatable配置中设置控制器以自动获取数据并刷新数据而不重新加载页面吗?

    1 回复  |  直到 6 年前
        1
  •  0
  •   sanfalero    6 年前

    您必须使数据表从ajax源收集数据DataTables允许您配置将检索表内容的服务器端终结点的URL。 然后添加一个按钮,onclick调用函数table.ajax.reload()另外,您可能希望使用setInterval()定期自动调用此函数。

    查看有关设置 ajax source ,以及 ajax reload function .

    另一个选项是更新表的数据部分并重新绘制网格。 https://datatables.net/reference/api/draw()