`
xianbin
  • 浏览: 212655 次
  • 来自: ...
社区版块
存档分类
最新评论

在Datatables中加入错误提示功能

阅读更多
经常用Datatables的童鞋一定碰到过当采用服务端请求的时候,一旦后台出现异常,Datatables的会一直卡在那里,中间的正在处理的提示一直停留着。

为了能给用户更好的体验,需要对Datatables进行扩展和自定义错误处理函数。

首先到Datatables官网获取一个插件:
http://datatables.net/plug-ins/api
插件很小,代码如下所示:
jQuery.fn.dataTableExt.oApi.fnProcessingIndicator = function ( oSettings, onoff )
{
    if( typeof(onoff) == 'undefined' )
    {
        onoff=true;
    }
    this.oApi._fnProcessingDisplay( oSettings, onoff );
};

该插件用于开启或关闭Datatables的正在处理提醒的消息框。
使用方法:
oTable.fnProcessingIndicator();      // On
oTable.fnProcessingIndicator(false); // Off


修改datatables创建时的options选项:
"fnServerData": function ( sSource, aoData, fnCallback ) {
    $.ajax( {
        "dataType": 'json',
        "type": "POST",
        "url": sSource,
        "data": aoData,
        "success": fnCallback,
        "timeout": 15000,   // optional if you want to handle timeouts (which you should)
        "error": handleAjaxError // this sets up jQuery to give me errors
    } );
},


定义处理错误的函数:
function handleAjaxError( xhr, textStatus, error ) {
    if ( textStatus === 'timeout' ) {
        alert( 'The server took too long to send the data.' );
    }
    else {
        alert( 'An error occurred on the server. Please try again in a minute.' );
    }

    $('.dataTable').dataTable().fnProcessingIndicator( false );
}
0
0
分享到:
评论
1 楼 nbh219 2015-12-08  
插件只有这几行代码吗jQuery.fn.dataTableExt.oApi.fnProcessingIndicator = function ( oSettings, onoff ) 

    if( typeof(onoff) == 'undefined' ) 
    { 
        onoff=true; 
    } 
    this.oApi._fnProcessingDisplay( oSettings, onoff ); 
}; 

相关推荐

Global site tag (gtag.js) - Google Analytics