Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

407
Vistas
Pass DataTable reference to the callback function on load

My current code is:

var CommissionLogs = $("#CommissionLogs").DataTable({       
     ajax: {
         url: ajaxurl + '?action=pos&post_action=get_commissions'
      },
    'initComplete': function (settings, json){
        //possible to access 'this'
        this.api().columns(1);
    }
  });

I improved the code above as below with help :

var CommissionLogs = $("#CommissionLogs").DataTable({       
     ajax: {
         url: ajaxurl + '?action=pos&post_action=get_commissions'
      },
    'initComplete': function(settings, json){ 
        callbackFunction(settings);
     }
  });

 function callbackFunction(settings){
     var api = new $.fn.dataTable.Api( settings );
     // api is accessible here.
 }

Update :

Now I can access api from callback function. But I want use same callback with load() as below code.

CommissionLogs.ajax.url( newAjaxURL ).load( callbackFunction(), true);

But settings param is not accessible in load function.

I can clear and destroy datatable and re initialize always. But what will be the right way.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

I think you need settings:

https://datatables.net/reference/type/DataTables.Settings

$('#example').dataTable( {
    "initComplete": function(settings, json) {
        myFunction(settings);
    }
});

function myFunction(settings){
    var api = new $.fn.dataTable.Api( settings );
 
    // Output the data for the visible rows to the browser's console
    // You might do something more useful with it!
    console.log( api.rows( {page:'current'} ).data() );
}

Other option is re-use your var CommissionLogs variable throughout the code without using this, I recommend strongly this last option.

over 4 years ago · Santiago Trujillo Denunciar

0

The dataTable.ajax.url().load() has not access to settings.

So can not call a callback function with settings.

But possible to use callback function without settings.

So here is an alternative way to use settings.

 CommissionLogs.clear();// clear the table
 CommissionLogs.destroy();// destroy the table
 CommissionLogs = $("#CommissionLogs").DataTable({           
         ajax: {
            url: newAjaxUrl
         },
        'initComplete': function (settings, json){
            callbackDatatableFunciton(settings);
        }
    }); 
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda