jQuery、DataTables、ajax 错误处理 : get rid of "Processing" message

标签 jquery ajax datatable

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

<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回调函数。这个“有效”是因为我可以在服务器端 ajax 代码中强制出错并触发回调。我可以根据需要处理返回的错误。

我的问题是,当 ajax 调用开始时,表主体被 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>");
    }
}

我有一个“全局”变量来保存 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 应该如何处理?消息被重置吗?

最佳答案

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

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

这可能已经改变,并且 id 可能不同,但概念仍然存在,您只需了解如何识别元素并隐藏它即可。如果需要,DataTables 将再次显示它。

另外,您是否希望在其他场合也显示处理消息?因为你可以通过使用processing: false让它永远不会出现在数据表的选项上(或者根本不设置它,因为默认行为不显示它)。

关于jQuery、DataTables、ajax 错误处理 : get rid of "Processing" message,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31594199/

相关文章:

javascript - 根据下拉值更改表单字段

javascript - 如何禁用由子元素触发的 mouseout 事件?

javascript - 如何从 javascript (ajax) 调用 Web 服务

jquery - DATATABLE jQuery TH 和 TD 对齐

primefaces - dtata 表中 rowStyleClass 的多个条件 PrimeFaces

javascript - iScroll 中可以同时使用 Probe 和 Zoom 功能吗?

javascript - jQuery UI 可靠吗?

jsf - 如何获取 p :datatable direclty on selecting the row? 的选定行

jquery - 使用 AJAX 进行 Parsley 自定义远程验证

jquery - 使用 Rails 3、jquery 和 ajax 的简单喜欢/不喜欢按钮