How to update jquery datatable after deleting row

Each row in the data table has a delete button.

When I click the delete button, I call this code:

$('.deleteButton').live('click', function () {

             var $this = $(this);
             var url = $this.attr("id");
             $("#example").fnReloadAjax();
             $.getJSON(url, Success());
         });

A URL is a controller action that accepts an identifier and deletes data from a database. It works. Now I want to call the refresh / redraw datatable function so that it can load new results, but it does not work.

Datatable:

 $("#example").dataTable({
             "aoColumns": [
                     { "sTitle": "Id" },
                      { "sTitle": "Name" }],


             "bProcessing": true,
             "bServerSide": true,

             "sAjaxSource": '@Url.Action("FetchData", "Home")',
             "sPaginationType": "full_numbers",

});

Can anyone advise?

+5
source share
4 answers

Quoted from the datatable API page - on fnReloadAjax()bullet:

Note. To reload data using server processing, simply use the built-in fnDraw API function, not this plugin.

, fnDraw.

[EDIT] , :

// you don't have to use $.json because you don't use the downloaded data
// first, ask the server to delete the data   
$.ajax({
   url: urlToDelete,
   success: function() {
       // if it worked, ask datatable to redraw the table with the new data
       $("#example").dataTable().fnDraw();
       // if this js function does anything useful (like deleting the row), then call it:
       Success();
   },
   error: function() {
       // display any error (like server couldn't be reached...), or at least try to log it
   }
});
+9

, .

Ajax

ajax.

frontend datatable

TR, , datatable fnDeleteRow, . , fnDraw .

//in my case its delete button which was clicked 
//so I got its parents parent which is TR row
var row = $(this).parent().parent();


$('DATA TABLE SELECTOR').dataTable().fnDeleteRow(row);

..: -)

+3

, :

http://datatables.net/plug-ins/api#fnReloadAjax

:

$.fn.dataTableExt.oApi.fnReloadAjax = function ( oSettings, sNewSource, fnCallback, bStandingRedraw )
{
    if ( sNewSource !== undefined && sNewSource !== null ) {
        oSettings.sAjaxSource = sNewSource;
    }

    // Server-side processing should just call fnDraw
    if ( oSettings.oFeatures.bServerSide ) {
        this.fnDraw();
        return;
    }

    this.oApi._fnProcessingDisplay( oSettings, true );
    var that = this;
    var iStart = oSettings._iDisplayStart;
    var aData = [];

    this.oApi._fnServerParams( oSettings, aData );

    oSettings.fnServerData.call( oSettings.oInstance, 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 ( bStandingRedraw === true )
        {
            oSettings._iDisplayStart = iStart;
            that.oApi._fnCalculateEnd( oSettings );
            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();
+1

dataTable datatable

dataTable.fnDeleteRow($(that).closest('tr').remove());
dataTable.fnDraw();
0

All Articles