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

javascript - Datatable - ajax success - Stack Overflow

programmeradmin0浏览0评论

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;
},

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;
},
Share Improve this question edited Jul 27, 2017 at 19:54 Matheus Miranda 1,8052 gold badges23 silver badges38 bronze badges asked Apr 11, 2016 at 9:57 rad11rad11 1,5613 gold badges15 silver badges31 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 6

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

success: function (response) {
      getAccessMessageWithStatus(response)
},

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

You can use it this way:

"complete": function (json, type) { //type return "success" or "parsererror"
    if (type == "parsererror") {
        alert("parsererror");
    }
     ... 
}
发布评论

评论列表(0)

  1. 暂无评论