【问题标题】:How can I refresh jQuery DataTable without Page Refresh?如何在不刷新页面的情况下刷新 jQuery DataTable?
【发布时间】:2017-12-21 15:44:39
【问题描述】:

我在我的数据表中使用了多列搜索功能,我还有一个重置按钮,用于清除所有搜索并将数据表恢复到默认状态。

效果很好。

但我想知道如何在不刷新页面的情况下重置数据表..??

请帮忙。 提前致谢。

以下是重置按钮的html:

<button class="Reset form-control" id="reset">Reset table to Original State</button>

下面是重置表的状态

 oTable.fnDraw();

【问题讨论】:

    标签: jquery datatables reset


    【解决方案1】:

    在谷歌快速搜索后,我发现了一个可以使用重置/重新加载表格的功能。您可以使用 AJAX 和 datatables 插件中的 ajax.reload() 函数来完成。

    var table = $('#example').DataTable();
    
    table.ajax.reload( function ( json ) {
        $('#myInput').val( json.lastInput );
    } );
    

    文档:datatables ajax.reload()

    【讨论】:

    • 有没有要添加的cdn来使用ajax.reload? @gwesseling
    • 我不这么认为。如果您有 JQuery 和数据表,ajax.reload() 应该可以工作。以我在解决方案中添加的链接为例。
    • ajax.reload() 仅在 v1.10 中可用,由于 OP 使用 v1.9 语法,我假设 v1.10 功能不可用。
    【解决方案2】:

    需要在按钮点击事件中调用数据表fnDraw()函数:

    $('#reset').on('click', function(e){
        e.preventDefault()
        oTable.fnDraw();
    });
    

    这假定您已将数据表对象分配给 var oTable。此外,您必须在调用 fnDraw() 之前重置搜索字段,否则您将只执行另一个搜索。

    【讨论】:

    • 这很好用。但我不希望页面重新加载,它应该在客户端。 @markpsmith
    • 按钮是否在表单中?通常,重置按钮用type="reset" 声明,这将阻止刷新。你还没有这样做,所以你需要添加e.preventDefault()
    • 这只会重置文本框的值。但是在执行搜索之前数据表的默认值不会在重置按钮单击时保留..
    • 老实说,您并没有给我们太多的合作机会 - 您能发布更多代码吗?表单和数据表代码会很有用。
    【解决方案3】:

    数据表代码:

    var oTable;
    var asInitVals = new Array();
    
    $(document).ready(function () {
        oTable = $('#webgrid').dataTable({
        //"sDom": 'C<"clear">lfrtip',
            sDom: 'Bfrtip',
            buttons: [
                {
                    extend: 'copyHtml5',
                    exportOptions: {
                        columns: [0, ':visible']
                    }
                },
                {
                    extend: 'excelHtml5',
                    exportOptions: {
                        columns: ':visible'
                    }
                },
                'colvis'
            ],
            "sSearch": "Search all columns:",
            "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
            colReorder: true,
        });
        //To reset table to original state
        $('#reset').on('click', function (e) {
            e.preventDefault();
            oTable.fnDraw();
        });
        //oTable.fnDraw();
    
        $("tfoot input").keyup(function () {
            /* Filter on the column (the index) of this element */
            oTable.fnFilter(this.value, $("tfoot input").index(this));
        });
        /*
         * Support functions to provide a little bit of 'user friendlyness' to the textboxes in
         * the footer
         */
        $("tfoot input").each(function (i) {
            asInitVals[i] = this.value;
        });});
    

    这是包含 WebGrid 和重置按钮的表单:

    @using (Html.BeginForm("Persons", "Welcome", FormMethod.Get, new { @class = "Search-form", @id = "form1" }))
    
    {
    <div id="DivGrid">
        @{
    var grid = new WebGrid(source: Model, canPage: false,
            defaultSort: "Employee_ID", columnNames: new[] { "Employee_ID", "First_Name", "Last_Name", "Date_Of_Birth" });
    if (Model.Count() > 0)
    {
            @grid.Table(tableStyle: "PGrid", headerStyle: "Header", footerStyle: "Footer", alternatingRowStyle: "altRow", htmlAttributes: new { id = "webgrid" }, columns: grid.Columns(
                     grid.Column("Employee_ID", "Employee ID",
                format: @<text>  <span class="display-mode">@item.Employee_ID </span>
                <label id="EmployeeID" class="edit-mode">@item.Employee_ID</label> </text>, style: "col2Width EmployeeID"),
    
                grid.Column("First_Name", "First Name", format: @<text>  <span class="display-mode">
                    <label id="lblFirstName">@item.First_Name</label>
                </span> <input type="text" id="FirstName" value="@item.First_Name" class="edit-mode" name="firstname" /></text>, style: "col2Width"),
    
                grid.Column("Last_Name", "Last Name", format: @<text> <span class="display-mode">
                    <label id="lblLastName">@item.Last_Name</label>
                </span>  <input type="text" id="LastName" value="@item.Last_Name" class="edit-mode" name="lastname" /> </text>, style: "col2Width"),
    
                grid.Column("Date_Of_Birth", "Date Of Birth", format: item => ((item.Date_Of_Birth == null) ? "" : item.Date_Of_Birth.ToString("MM/dd/yyyy")), style: "DateOfBirth"),
                grid.Column(header: "Action", canSort: false, style: "action", format: @<text>
                    <button class="edit-user display-mode glyphicon glyphicon-edit"> Edit</button>
                    <button class="display-mode delete-item glyphicon glyphicon-trash"> Delete</button>
                    <button class="save-user edit-mode glyphicon glyphicon-save"> Save</button>
                    <button class="cancel-user edit-mode glyphicon glyphicon-remove-sign"> Cancel</button></text>)));
        <table class='container'>
            <tfoot class='filters multipleSearch col-md-12'>
                <tr class="tBoxes">
                    <th class="txtBoxWidth">
                        <input class='txtBox1 form-control' placeholder='Employee Id' />
                        @*<input type="text" name="Employee Id" placeholder='Employee Id' class="search_init" />*@
                    </th>
                    <th class="txtBoxWidth">
                        <input class='txtBox2 form-control' placeholder='First Name' />
                    </th>
                    <th class="txtBoxWidth">
                        <input class='txtBox3 form-control' placeholder='Last Name' />
                    </th>
                    <th class="txtBoxWidth">
                        <input class='txtBox4 form-control' placeholder='Date of Birth' />
                    </th>
                    <th>
                        <input type="reset" value="Reset table to Original State" class="Reset btn btn-sm"  />
                               @*<button type="reset" class="Reset form-control" id="reset">Reset table to Original State</button>*@
                    </th>
                </tr>
            </tfoot>
        </table>
    </div>
            <br>
    }}
    

    【讨论】:

      【解决方案4】:

      你可以使用这个代码:

      $(tableId).dataTable().fnDraw();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-11
        • 2019-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-15
        • 2023-04-01
        • 1970-01-01
        相关资源
        最近更新 更多