数据表 \- ajax 成功

新手上路,请多包涵

当我添加成功函数 DataTable 时,不会自动填充表中的行。当我删除成功函数时,一切正常,数据表正确填写表中的数据。我想通过状态使用 getAccessMessageWithStatus 消息来响应,但是如果我这样做就像这个数据表没有填充行。我该怎么做?

 $('#' + 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
});

答案:

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

},

或者:

 "dataSrc": function(response) {

  if (response.status == false) {
    alert(response.msg);
    return [];
  }
  return response.aaData;
},

原文由 rad11 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 267
2 个回答

消除 ”;”在代码中的函数名之后。

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

原文由 Dharmesh Goswami 发布,翻译遵循 CC BY-SA 3.0 许可协议

DatatTable 中有一个名为 ‘xhr.dt’ 的事件。你可以那样使用它。

 $('#' + 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');
            }
       }
});

您不应该使用 ajax 属性中的成功,因为您将覆盖 DataTable 中的成功函数。请参阅 query.dataTables.js 中的这段代码

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

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

你可以注意到他们在这个函数中有一个回调。此回调触发函数 _fnCallbackFire 并调用事件 xhr.dt

有关更多信息,请转到此页面 https://datatables.net/reference/event/xhr

原文由 Carlos Huamani 发布,翻译遵循 CC BY-SA 3.0 许可协议

推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏