How to reload the datatable(jquery) data?

后端 未结 10 2480
萌比男神i
萌比男神i 2020-12-05 05:39

How to reload the selected tab? Actually I have problem with reloading part. When I add my data I\'ll successfully saved in datatable but in id field in database it shows pr

相关标签:
10条回答
  • 2020-12-05 05:45

    You can use a simple approach...

    $('YourDataTableID').dataTable()._fnAjaxUpdate();
    

    It will refresh the data by making an ajax request with the same parameters. Very simple.

    0 讨论(0)
  • 2020-12-05 05:45
    // Get the url from the Settings of the table: oSettings.sAjaxSource
    
    function refreshTable(oTable) {
        table = oTable.dataTable();
        oSettings = table.fnSettings();
    
        //Retrieve the new data with $.getJSON. You could use it ajax too
        $.getJSON(oSettings.sAjaxSource, null, function( json ) {
            table.fnClearTable(this);
    
            for (var i=0; i<json.aaData.length; i++) {
                table.oApi._fnAddData(oSettings, json.aaData[i]);
            }
    
            oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
            table.fnDraw();
        });
    }
    
    0 讨论(0)
  • 2020-12-05 05:51

    If anything works! Do this:

    example:

    <div id="tabledisplay"><table class="display" id="table"></table><table </div>
    

    how to reload the table:

    $('#tabledisplay').empty();
    $('#tabledisplay').append("<table class=\"display\" id=\"table\"></table>");
    initTable( "tablename");  
    

    initTable is just a method, that initialized the Table with getJSON.

    0 讨论(0)
  • 2020-12-05 05:54

    I'm posting this just in case someone need it..

    Just create a button:

    <button type="button" href="javascript:void(0);" onclick="mytable.fnDraw();">Refresh</button>
    

    but don't forget to add this when calling the datatable:

    mytable = $("#mytable").dataTable();
    
    0 讨论(0)
  • 2020-12-05 05:54

    For the newer versions use:

    var datatable = $('#table').dataTable().api();
    
    $.get('myUrl', function(newDataArray) {
        datatable.clear();
        datatable.rows.add(newDataArray);
        datatable.draw();
    });
    

    Taken from: https://stackoverflow.com/a/27781459/4059810

    0 讨论(0)
  • 2020-12-05 05:57

    Use the fnReloadAjax() by the DataTables.net author.

    I'm copying the source code below - in case the original ever moves:

    $.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 );
    }
    
    /* Example call to load a new file */
    oTable.fnReloadAjax( 'media/examples_support/json_source2.txt' );
    
    /* Example call to reload from original file */
    oTable.fnReloadAjax();
    
    0 讨论(0)
提交回复
热议问题