Jquery DataTables-有没有办法运行initComplete两次?

Jquery DataTables-有没有办法运行initComplete两次?,jquery,datatable,datatables,Jquery,Datatable,Datatables,我有一个包含许多dataTables的应用程序,70+,因此我使用默认扩展来设置所有常见的配置设置。但是,当单个数据表有自己的initComplete函数时,我还没有找到一种在initComplete中运行函数的方法 以下是我的测试代码: $.extend( true, $.fn.dataTable.defaults, { "sDom": '<"top"i>rt<"bottom"lp><"clear">', "pageLength": 20,

我有一个包含许多dataTables的应用程序,70+,因此我使用默认扩展来设置所有常见的配置设置。但是,当单个数据表有自己的initComplete函数时,我还没有找到一种在initComplete中运行函数的方法

以下是我的测试代码:

$.extend( true, $.fn.dataTable.defaults, {
    "sDom": '<"top"i>rt<"bottom"lp><"clear">',
    "pageLength": 20,
    "stateSave": false,
    "bLengthChange": false,
    "oLanguage": {
        "sInfo": ""
    },
    initComplete: function () {
        defaultStuff();
    }
});

var sampleTable = $('#example').DataTable({
    "pageLength": 10,       
    "paging": true,
    initComplete: function () {
        customStuff();
    } 
});

function defaultStuff() {
    console.log('Default..');
}

function customStuff() {
    console.log('Custom..');
}
$.extend(true,$.fn.dataTable.defaults{
“sDom”:“rt”,
“页面长度”:20,
“stateSave”:false,
“bLengthChange”:false,
“语言”:{
“sInfo”:”
},
initComplete:函数(){
defaultStuff();
}
});
var sampleTable=$(“#示例”).DataTable({
“页面长度”:10,
“分页”:正确,
initComplete:函数(){
海关人员();
} 
});
函数defaultStuff(){
console.log('Default..');
}
函数customStuff(){
console.log('Custom..');
}

当运行该函数时,只运行自定义函数,而我希望同时运行默认函数和自定义函数

单个数据表的设置覆盖默认设置,这就是为什么
defaultStuff()
从未在
#example
上被调用的原因

要实现这一点,您需要在该表的
initComplete
中手动调用:

var sampleTable = $('#example').DataTable({
  pageLength: 10,       
  paging: true,
  initComplete: function () {
    defaultStuff(); // add this here too
    customStuff();
  } 
});

我建议您在初始化时,在需要额外准备/代码的表上收听以下内容:

$('#example').on('init.dt', function(e, settings, json) {
  //api can be retrieved by
  var api = $('#example').DataTable();
  ...
})