issue when Api responsive.recalc() jquery load indeterminate numbers of responsives tables

issue when Api responsive.recalc() jquery load indeterminate numbers of responsives tables

fotosvinadelmarfotosvinadelmar Posts: 19Questions: 2Answers: 1
edited August 2018 in Free community support

I have a very strange issue, i'm able to use the great datatable plugin in many places.
But when i load the tables trought Jquery.load() ajax function the responsive characteristic do not work correctly until manualy change the size of the window.

I make a research and I can not make it work responsive.recalc() and columns.adjust().

This is my load function that bring me the tables when change an select input :

```

$('#idPresupuesto').change(function(event) {
    $('.table1').load('actions/manyTablesLoads.php?id='+$('#id').val(), function () {
        var table = $('table.display').DataTable({responsive: true}).responsive.recalc();
        $.fn.dataTable.tables( { visible: true, api: true } ).columns.adjust();

        $('#preloader').fadeOut('slow');
  });

```
What i make it wrong?
Best Regards!

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,329Questions: 26Answers: 4,949

    You might need to add .draw() to your columns.adjust() line as shown in the docs.

    Kevin

  • fotosvinadelmarfotosvinadelmar Posts: 19Questions: 2Answers: 1

    Thanks Kevin, but the result is the same.

  • fotosvinadelmarfotosvinadelmar Posts: 19Questions: 2Answers: 1

    now change my code to :

    $('.tabla1').load('acciones/cargarTablaPresupuestoAjax.php?idPresupuesto='+$('#idPresupuesto').val(), function () {
                     
              var table = $('table.display').DataTable().responsive.recalc();
               $(".collapse").on('shown.bs.collapse', function(){
                  var table = $('table.display').DataTable().responsive.recalc();
                  table.columns.adjust().draw()
              });
             
              $('#preloader').fadeOut('slow');
              toastr.success('Actualizando Presupuesto...', 'Presupuesto', {timeOut: 1500});
          });
          
    

    With no success

  • fotosvinadelmarfotosvinadelmar Posts: 19Questions: 2Answers: 1

    Now i change to this, with succes on load the tables:

     $(".collapse").on('shown.bs.collapse', function(){
                  var table = $('table.display').DataTable({'autoWidth':false}).responsive.recalc();
                  
              });
    

    But now when open the collapse component aggregate other search bar to datatable

    what's wrong?

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    It might also be worth adding a call to responsive.rebuild() before the responsive.recalc(). If that doesn't help, would you be able to link to the page so we can take a look.

  • fotosvinadelmarfotosvinadelmar Posts: 19Questions: 2Answers: 1

    Hi colin i use the responsive.rebuild with no effects.
    After the Jquery.load(). i put this code but when open a collapsable the search input and the select show entries repeat when reopen a collapsible

    $(".collapse").on('shown.bs.collapse', function(){
                  var table = $('table.display').DataTable({'autoWidth':false});              
              });
             
    

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @fotosvinadelmar ,

    Definitely something odd going on there - could you link to a test case that would demonstrate the problem?

    Cheers,

    Colin

  • fotosvinadelmarfotosvinadelmar Posts: 19Questions: 2Answers: 1

    Hi @colin , i made a reproduction in here,

    Issue reproduction

    Best regards.

  • fotosvinadelmarfotosvinadelmar Posts: 19Questions: 2Answers: 1
  • fotosvinadelmarfotosvinadelmar Posts: 19Questions: 2Answers: 1

    Note: Datatable Debugger shows no errors.

    15 tests complete. No failures or warnings found!
    
    
    
  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    Hi @fotosvinadelmar ,

    If you open a second group (or reopen a single one), it tries to initialise all tables again and gives a page error.

           //here is the issue
          $(".collapse").on('shown.bs.collapse', function(){
            var table = $('table.display').DataTable({'autoWidth':false}).responsive.recalc();
            
          }); 
    

    This is initialising all tables - as all tables have class .display. You need to explicitly initialise the table being expanded, or, destroy all the current tables with destroy() before initialising the others.

    The responsive aspects are working as expected. They seem to be collapsed when the space is narrow, and not when wider. I'd say fix the reinitialising issue and see if it's behaving as expected then,

    Cheers,

    Colin

  • fotosvinadelmarfotosvinadelmar Posts: 19Questions: 2Answers: 1
    edited November 2018

    i made more coding for this and finally resolved the issue creating an empty table and destroing initialization if exists .

    if ( $.fn.DataTable.isDataTable('#example') ) { $('#example').DataTable().destroy(); }

    And then filled with ajax json sourced and apply draw(true) and columns.adjust()

     $('#example').DataTable(
           {
                "responsive": true,
                "processing" : true,
                "ajax" : {
                     "url" : "source.php",
                     dataSrc : ''
                 }
     }).draw(true).columns.adjust();
    

    the source.php must be <?php echo json_encode($result); ?> as echoed result.

    All this in the action of open a class named collapse to call an ajax every time when collapse is opened:

    $(".collapseName").on('show.bs.collapse', function(){
      [...]
    });
    

    Thanks.

This discussion has been closed.