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

时间:2022-04-29 20:19:29

I am using jQuery DataTables, ajax and Bootstrap in a small project. It is mostly working and I am testing error states. I have setup my html for the DataTables table. There is a table-responsive div surrounding the <table>.

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

周围有一个响应表的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>

In the DataTables init I setup the ajax call and specify an error callback function. This "works" in that I can force an error in the server side ajax code and the callback is fired. I can handle the returned error as I want.

在DataTables init中,我设置了ajax调用并指定了错误回调函数。这“工作”,我可以强制服务器端ajax代码中的错误,并触发回调。我可以按照自己的意愿处理返回的错误。

My issue is that when the ajax call starts the table body is "replaced" by a Processing message. That's fine except that when ajax reports the error via the callback the processing message is still there and I have yet to figure out how to make it go away via the DataTables methods or API calls.

我的问题是,当ajax调用启动时,表体被Processing消息“替换”。这很好,除非当ajax通过回调报告错误时,处理消息仍然存在,我还没有弄清楚如何通过DataTables方法或API调用使其消失。

my error callback looks like

我的错误回调看起来像

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

I have a "global" variable that holds the DataTables table definition. It is set in the jQuery ready function

我有一个“全局”变量,它包含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
        }, ...

In the error callback I have tried all of the following:

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

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

In this error state how should the Processing message be reset?

在此错误状态下,应如何重置处理消息?

1 个解决方案

#1


8  

The element which shows the processing message gets the id <idOfTable>_processing, so you could hide it using:

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

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

This might have changed, and the id could be different, but the concept stands, you can just find out how to identify the element and hide it. DataTables will show it again if it has to.

这可能已经改变了,id可能会有所不同,但概念就是这样,你可以找出如何识别元素并将其隐藏起来。如果必须,DataTables将再次显示它。

Also, do you want the processing message showing up in other ocassions? Because you can just make it never appear by using processing: false on the DataTable's options (or just not setting it at all, since it's the default behaviour not to show it).

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

#1


8  

The element which shows the processing message gets the id <idOfTable>_processing, so you could hide it using:

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

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

This might have changed, and the id could be different, but the concept stands, you can just find out how to identify the element and hide it. DataTables will show it again if it has to.

这可能已经改变了,id可能会有所不同,但概念就是这样,你可以找出如何识别元素并将其隐藏起来。如果必须,DataTables将再次显示它。

Also, do you want the processing message showing up in other ocassions? Because you can just make it never appear by using processing: false on the DataTable's options (or just not setting it at all, since it's the default behaviour not to show it).

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