jQuery,DataTables,ajax错误处理:摆脱"处理"消息

时间:2015-07-23 17:24:19

标签: jquery ajax datatable

我在一个小项目中使用jQuery DataTables,ajax和Bootstrap。它主要是工作,我正在测试错误状态。我已经为DataTables表设置了我的html。 table-responsive周围有一个<table> div。

<div id="errorDiv"></div>
    <div id="resultTableDiv" class="table-responsive">
        <table id="resultTable"  class="table table-striped table-bordered table-hover table-condensed" cellspacing="0">
            <thead>
            <tr>
                <th>...</th>
                ...
            </tr>
            </thead>
            <tfoot>
            <tr>
                <th>...</th>
                ...
            </tr>
            </tfoot>
        </table>
    </div>
</div>

在DataTables init中,我设置了ajax调用并指定了error回调函数。这&#34;工作&#34;因为我可以强制服务器端ajax代码中的错误,并触发回调。我可以随意处理返回的错误。

我的问题是,当ajax调用开始时,表体被替换为&#34;通过Processing消息。没问题,但是当ajax通过回调报告错误时,处理消息仍然存在,我还没有弄清楚如何通过DataTables方法或API调用使其消失。

我的错误回调看起来像

function ajaxError(jqXHR, textStatus, errorThrown) {
    var obj = jQuery.parseJSON(jqXHR.responseText);
    if (obj && obj.Error) {
        $('#errorDiv').html("<p>" + obj.Error + "</p>");
    }
}

我有一个&#34;全球&#34;包含DataTables表定义的变量。它在jQuery ready函数中设置

var table;
$(function () {
    $('#errorDiv').empty();
    $('#resultTable').show();

    table = $('#resultTable').DataTable({
        paging: false,
        processing: true,
        autoWidth: false,
        ordering: false,
        ajax: {
            url: "ions.cgi",
            dataType: 'json',
            dataSrc: "LIST",
            error: ajaxError
        }, ...

在错误回调中,我尝试了以下所有方法:

table.clear().draw();    // does nothing
table.fnRedraw();        // says that function does not exist

在此错误状态下,如何重置Processing消息?

1 个答案:

答案 0 :(得分:10)

显示处理消息的元素获取id <idOfTable>_processing,因此您可以使用以下命令隐藏它:

$('#resultTable_processing').hide();

这可能已经改变,并且id可能不同,但是概念代表,您可以找到如何识别元素并隐藏它。如果必须,DataTables将再次显示它。

此外,您是否希望处理消息显示在其他ocassions中?因为您可以在DataTable的选项上使用processing: false来永远不会显示它(或者根本不设置它,因为它是默认行为,不显示它)。