How to access to each rows().nodes() with $.fn.dataTable.Api ?

How to access to each rows().nodes() with $.fn.dataTable.Api ?

trucmuche2005trucmuche2005 Posts: 71Questions: 22Answers: 2

Hello,

I have the following :

function initCompleteFunction(settings, json){
  // I had this and it was working : 
// var api = this.api();
// $( '.select2', api.rows().nodes() ).select2();  

// now I have that :
   var api = new $.fn.dataTable.Api( settings );
  $( '.select2', api.rows().nodes() ).select2();    // and this line does not work anymore
   // ... skipped ...
}
 
$(document).ready(function(){
    $('#matable').on('xhr.dt', function ( e, settings, json, xhr ) {
        initCompleteFunction(settings, json);
    } );
 
    var tableDONNEES = $("#matable").DataTable( {
        // ... skipped ...
    });
});

How can I modify the $( '.select2', api.rows().nodes() ).select2(); to get it working when the table is initialized and when reloaded using tableDONNEES.ajax.reload(null, false); ?

Many thanks in advance for your help,

T.

Answers

  • allanallan Posts: 61,864Questions: 1Answers: 10,136 Site admin

    $('#matable').on('xhr.dt'

    That looks like the perfect way to me.

    If that isn't working for you, can you link to a test case showing the issue, as requested in the forum rules please?

    Allan

  • trucmuche2005trucmuche2005 Posts: 71Questions: 22Answers: 2

    Thank you Allan ! Yes of course. I send to you by PM login & password to access my website...

  • allanallan Posts: 61,864Questions: 1Answers: 10,136 Site admin

    Ah! I see. Thanks for the link. Try this:

        $('#matable').on('xhr.dt', function ( e, settings, json, xhr ) {
            new $.fn.dataTable.Api( settings ).one( 'draw', function () {
              initCompleteFunction(settings, json);
            } );
        } );
    

    Since we need to have the elements in the DOM before they can be manipulated, we have to wait for the draw event.

    You could possibly change it to just listen for draw.dt rather than xhr.dt. I think its really up to yourself. It would probably be easier that way:

        $('#matable').on('draw.dt', function ( e, settings, json, xhr ) {
              initCompleteFunction(settings, json);
        } );
    

    You just need to be a little more careful since that will be executed on every draw.

    Allan

  • trucmuche2005trucmuche2005 Posts: 71Questions: 22Answers: 2

    Waw. I used your first solution and it works perfectly ! :-) Now I have a subsequent question here Many many thanks again !!

This discussion has been closed.