最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - DataTables.net programmatic refresh - Stack Overflow

programmeradmin0浏览0评论

I am using DataTables (jQuery plugin) to display my tabular data. If fetches data using AJAX from a JSON web service.

How do I force it to refresh using JavaScript/jQuery? I am looking through the API and cannot find the right function.

I am using DataTables (jQuery plugin) to display my tabular data. If fetches data using AJAX from a JSON web service.

How do I force it to refresh using JavaScript/jQuery? I am looking through the API and cannot find the right function.

Share Improve this question asked Oct 11, 2012 at 21:22 LouLou 4,4943 gold badges36 silver badges83 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

While waiting for the answers, I finally found it: fnDraw function is what I needed.

It's described at the beginning of the API section, but I just didn't bother to read it all:

You must make the required calls to the server to manipulate your data as required, and then simply redraw the table (fnDraw) to view the new data.

Try calling:

$("#Table1").fnDestroy().dataTable();

That should rebuild it...

Add this somewhere:

$.fn.dataTableExt.oApi.fnReloadAjax = function ( oSettings, sNewSource, fnCallback, bStandingRedraw ) {
    if ( typeof sNewSource != 'undefined' && sNewSource != null )
    {
        oSettings.sAjaxSource = sNewSource;
    }
    this.oApi._fnProcessingDisplay( oSettings, true );
    var that = this;
    var iStart = oSettings._iDisplayStart;
    var aData = [];

    this.oApi._fnServerParams( oSettings, aData );

    oSettings.fnServerData( oSettings.sAjaxSource, aData, function(json) {
        /* Clear the old information from the table */
        that.oApi._fnClearTable( oSettings );

        /* Got the data - add it to the table */
        var aData =  (oSettings.sAjaxDataProp !== "") ?
            that.oApi._fnGetObjectDataFn( oSettings.sAjaxDataProp )( json ) : json;

        for ( var i=0 ; i<aData.length ; i++ )
        {
            that.oApi._fnAddData( oSettings, aData[i] );
        }

        oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
        that.fnDraw();

        if ( typeof bStandingRedraw != 'undefined' && bStandingRedraw === true )
        {
            oSettings._iDisplayStart = iStart;
            that.fnDraw( false );
        }

        that.oApi._fnProcessingDisplay( oSettings, false );

        /* Callback user function - for event handlers etc */
        if ( typeof fnCallback == 'function' && fnCallback != null )
        {
            fnCallback( oSettings );
        }
    }, oSettings );
}

Then call it like this:

oTable = $(".dt").dataTable();
oTable.fnReloadAjax();

Credit: http://datatables/plug-ins/api

发布评论

评论列表(0)

  1. 暂无评论