Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datatable - ajax success

When I add success function DataTable not fill up automaticly rows in table. When I remove success function everything is all right and datatable fill correctly data in table. I want to catch in response using getAccessMessageWithStatus message by status but if I do it like this datatable no filling rows. How I can do that?

$('#' + datatableName).DataTable({
  destroy: true,
  'bProcessing': false,
  'bServerSide': true,
  'ajax': {
    'url': URL,
    'data': filters,
    beforeSend: function() {
      loader.popup('show');
    },
    success: function(response) {
      getAccessMessageWithStatus(response);

    },
    complete: function() {
      $listContainer.show();
      $containerChoiseColumnsFilter.show();
      $(".containerRaportButtons").show();
      getLastSearches();
      getUses();
      loader.popup('hide');
    }
  },
  'sServerMethod': "POST",
  'columns': columns,
  'order': order,
  'responsive': true
});

Answers:

success: function(response) {
  getAccessMessageWithStatus(response);

},

Or:

"dataSrc": function(response) {

  if (response.status == false) {
    alert(response.msg);
    return [];
  }
  return response.aaData;
},
like image 640
rad11 Avatar asked Apr 11 '16 09:04

rad11


People also ask

What does success mean in Ajax?

What is AJAX success? AJAX success is a global event. Global events are triggered on the document to call any handlers who may be listening. The ajaxSuccess event is only called if the request is successful. It is essentially a type function that's called when a request proceeds.

What is data table in Ajax?

DataTables has the ability to read data from virtually any JSON data source that can be obtained by Ajax. This can be done, in its most simple form, by setting the ajax option to the address of the JSON data source.

What is drawCallback in DataTables?

Description. The partner of the drawCallback callback, this function is called at the very start of each table draw. It can therefore be used to update or clean the display before each draw (for example removing events), and additionally can be used to cancel the draw by returning false .


2 Answers

Remove ";" after the function name into the code.

success: function (response) {
      getAccessMessageWithStatus(response)
},
like image 149
Dharmesh Goswami Avatar answered Oct 23 '22 09:10

Dharmesh Goswami


There is an event from DatatTable called 'xhr.dt'. You can use it in that way.

$('#' + datatableName).on('xhr.dt', function(e, settings, json, xhr){
    getAccessMessageWithStatus(json);
}).DataTable({
    destroy: true,
    'bProcessing': false,
    'bServerSide': true,
    'ajax':
        {
            'url': URL,
            'data': filters,
            beforeSend: function () {
                loader.popup('show');
            },
            complete: function () {
                $listContainer.show();
                $containerChoiseColumnsFilter.show();
                $(".containerRaportButtons").show();
                getLastSearches();
                getUses();
                loader.popup('hide');
            }
       }
});

You shouldn't use success from ajax attribute because you will overwrite the success function from DataTable. See this piece of code from query.dataTables.js

"success": function (json) {
    var error = json.error || json.sError;
    if ( error ) {
        _fnLog( oSettings, 0, error );
    }

    oSettings.json = json;
    callback( json );
}

You can notice that they have a callback inside this function. This callback triggers the function _fnCallbackFire and this call the event xhr.dt

For more information, go to this page https://datatables.net/reference/event/xhr

like image 27
Carlos Huamani Avatar answered Oct 23 '22 10:10

Carlos Huamani