【问题标题】:Example of Repopulating DataTables Grid After Searching搜索后重新填充 DataTables 网格的示例
【发布时间】:2016-07-26 14:28:47
【问题描述】:

我是使用 jQuery 的 DataTables Table 插件的新手,并且能够从后端数据库中检索数据并成功地将其显示在网格中。这是在 $(document.ready()) 期间完成的。我创建了一个页面,其中提供了一些搜索条件,当用户提交页面时,这些条件将重新填充表格。有人可以提供一个简单的示例,说明如何在提交后使用表格重新填充新结果?我也在使用 MVC,我也是第一次解决这可能是问题的一部分。

下面是我尝试过的 jQuery 代码,但它没有将结果绑定到已经存在的表。我还仅在选项中指定了 ajax 源,认为该表已经设置了其他选项,并且 ajax 源是所有需要更改的。

谢谢, 汤姆

$('#SubmitForm').on('submit', function (e) { 
    table = $('#grid').DataTable();
    table.sAjaxSource = "GetEmails";
    table.bServerSide = true;
    table.bProcessing = true;
    table.aoColumns = [
        { "sName": "Id" },
        { "sName": "Email" },
        { "sName": "Reason" },
        { "sName": "Name" },
        { "sName": "Organization" },
        { "sName": "Email_Verified_Date" }
    ];

    return true;
});

浏览器只显示ajax源的输出,如下图。

{"sEcho":null,"iTotalRecords":94,"iTotalDisplayRecords":94,"aaData":[]}

以下是我的观点摘录,展示了我如何使用 DataTable。

这可行 - 请注意 DataTable 在页面加载期间通过 document.ready 呈现和填充。

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>How to use jQuery Grid with ASP.NET MVC</title>

    <script type="text/javascript">
        $(document).ready(function () {
            var oTable = $('#grid').dataTable({
                "bServerSide": true,
                "sAjaxSource": "home/GetEmails1",
                "bProcessing": true,
                "aoColumns": [
                                { "sName": "Id" },
                                {"sName": "Email"},
                                {"sName": "Reason"},
                                { "sName": "Name" },
                                { "sName": "Organization" },
                                { "sName": "Email_Verified_Date" }
                ]
            });
    });

    </script>
</head>
<body>
    <div class="table-responsive">
        <table id="grid">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>Email</th>
                    <th>Reason</th>
                    <th>Name</th>
                    <th>Organization</th>
                    <th>Email Verified Date</th>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>
</body>
</html>

这是第二个不起作用的视图。在这里,我尝试采用搜索条件,提交并使用搜索结果填充表格。来自上述工作示例的 ajax 调用和此处的非工作示例都从 ajax 调用返回相同的结果。我在此视图中包含了页面加载示例,并认为这可能有助于 DataTable 在搜索完成并重新填充时已经初始化。感谢您的帮助!

@model MvcMovie.Models.ACORD360VerifiedEmail

@{
    ViewBag.Title = "Home Page";
}

<!DOCTYPE html>

<html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Search Page</title>

        <script type="text/javascript">
            $(document).ready(function () {

                var oTable = $('#grid').dataTable({
                    "bServerSide": true,
                    //"sAjaxSource": "GetEmails",
                    "sAjaxSource": "GetEmails",
                    "bProcessing": true,
                    "aoColumns": [
                                    { "sName": "Id" },
                                    { "sName": "Email" },
                                    { "sName": "Reason" },
                                    { "sName": "Name" },
                                    { "sName": "Organization" },
                                    { "sName": "Email_Verified_Date" }
                    ]
                });

        $('#SubmitForm').on('submit', function (e) { //use on if jQuery 1.7+
            table = $('#grid').DataTable();
            table.ajax.reload();

            return true;
        });
    })
        </script>
    </head>
    <body>
    @using (Html.BeginForm("GetEmails", "ACORD360VerifiedEmail", FormMethod.Post, new { id = "SubmitForm" }))
    {
        <div class="panel-body">
            <h2>Lyris - ACORD360 Integration</h2>
            <p class="lead">This allows you to modify Lyris and ACORD360 email data.</p>
        </div>

        <div class="row">
            <div class="col-md-3">
                @Html.LabelFor(m => m.EmailVerifiedStartDate)
                @Html.TextBoxFor(m => m.EmailVerifiedStartDate,
          new { @class = "form-control date-picker", placeholder = "Enter date here", id = "EmailVerifiedStartDate" })
            </div>
            <div class="col-md-3">
                @Html.LabelFor(m => m.EmailVerifiedEndDate)
                @Html.TextBoxFor(m => m.EmailVerifiedEndDate, new { @class = "form-control date-picker", placeholder = "Enter date here", id = "EmailVerifiedEndDate" })
            </div>
            <div class="col-md-3">
                @Html.LabelFor(m => m.OrganizationName)
                @Html.TextBoxFor(m => m.OrganizationName)
            </div>
            <div>
                <input type="submit" value="Search" />
            </div>
        </div>
        <br /><br /><br />
        <div class="table-responsive">
            <table id="grid">
                <thead>
                    <tr>
                        <th>Id</th>
                        <th>Email</th>
                        <th>Reason</th>
                        <th>Name</th>
                        <th>Organization</th>
                        <th>Email Verified Date</th>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
        <div class="row">
            <div class="col-md-12">
                <br /><br />
                <label id="Error"></label>
                <label id="Info"></label>
            </div>
        </div>
    }
    </body>
</html>

【问题讨论】:

    标签: javascript jquery ajax datatables


    【解决方案1】:

    在您的第二个示例中,您似乎没有更改任何数据:

    $('#SubmitForm').on('submit', function (e) { //use on if jQuery 1.7+
            table = $('#grid').DataTable();
            table.ajax.reload();
    
            return true;
    });
    

    提交时,您将重新加载表格,然后发布表单。如果您要使用 ajax 源数据,请使用 jquery 发布数据,然后在成功时重新加载表:

    $.ajax({
       url: url, type: 'POST',
       success: function() {
    
           table.ajax.reload();
       }
        }).fail(function() {
            alert("Sorry. Server unavailable.");
         }); 
    

    【讨论】:

    • 我试过这个,但得到了相同的结果。我已经验证从工作和非工作场景的 ajax 调用返回时结果是相同的。这是我使用的代码的 sn-p: table = $('#grid').DataTable(); table.ajax.reload();
    • 工作场景和非工作场景是什么意思?你是如何提交数据的?您希望它如何改变?
    • 我在主帖中为上面的工作和非工作视图添加了代码。感谢您的帮助。
    • 我发现了一篇关于在 Chrome 中使用 F12 的精彩文章,我过去曾使用过但不是以这种方式使用,这表明两个视图之间的响应存在差异。我明天将深入研究这个问题,并将用我的发现更新线程。
    • 感谢您跟进此事。在过去的几周里,我学到了很多关于主要在客户端工作的知识。我将实施您关于重新加载数据的建议。我还有一个额外的问题。在提交表单而不是仅仅处理点击事件并重新加载数据时,是否有最佳实践?
    【解决方案2】:

    事实证明,在 MVC、ajax 和 DataTables 之间我有很多东西要学。我最终使用按钮单击按钮而不是使用提交。效果很好。我不得不说,在花了这么多时间使用 ASP.Net Web Forms 之后,使用这种方法令人耳目一新。感谢所有提出建议的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-02
      • 1970-01-01
      • 2016-10-06
      • 2016-02-05
      • 1970-01-01
      • 1970-01-01
      • 2020-01-30
      • 2019-01-15
      相关资源
      最近更新 更多