any() option

any() option

marisatrainimarisatraini Posts: 10Questions: 3Answers: 0

the any() option dosen't work because if the table is empty, however the table has one row for the message of attribute "emptyTable".
the same with count()

Answers

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949
    edited December 2023

    I just tried both the any() and count() with an empty table in this test case:
    https://live.datatables.net/xohutequ/1/edit

    They both seem to work as expected, for example:

    any() false
    count() 0
    

    Please provide more details about the issue you are seeing. Better is a link to a test case (update mine if you wish) to show the issue.

    Kevin

  • marisatrainimarisatraini Posts: 10Questions: 3Answers: 0

    Yes, the control if with the option count() works well, but I was deceived by the alert which returns the value 0 even when there is a valid row in the table. This is the code:

            let ttTable = $('#table_shifts').DataTable();   
    
            alert( ttTable.data().count() );
    
            if (  ttTable.data().count() == 0) 
            {
                alert( 'Empty table' );
                $('#btn_apri_tutto').attr('disabled','disabled');
            }
            else
            {
                alert( 'Full table' );
                $('#btn_apri_tutto').removeAttr('disabled');
            }
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    What alert? There are several possible error conditions.

    If you link to a test case as Kevin requested, we'd be happy to help.

    Allan

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Do you mean the alert in your code on line 3? As Kevin's test case shows, an empty table will give a count of 0 there.

    Allan

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

    deceived by the alert which returns the value 0 even when there is a valid row in the table

    Just to make sure I'm clear when you say a valid row in the table are you referring to the row that shows the No data available in table message? The Datatables API's like api data(), rows().data(), etc concern themselves with the actual table data. For this case Datatables doesn't consider the No data available in table row as table data. Its a row that Datatables inserts to show the message but is not counted as part of the table data. Note that the information element also shows 0 rows, ie Showing 0 to 0 of 0 entries.

    If the table has actual table data but data() returns 0 rows then likely you are populating the table after Datatables is initialized using Javascript or jQuery methods. Datatables won't know about this. See this FAQ for details of how to append rows to the table.

    If neither of these helps then please provide a link to your page or a test case showing the issue so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • marisatrainimarisatraini Posts: 10Questions: 3Answers: 0
    edited December 2023

    Thank you Allan and Kthorngren.
    Yes Allan, I was referring to line 3 when there is at least a valid record in the table no empty table.
    Now I see my problem: I initialize the table with ajax immediately before the alert statement and/or the if control that I reported in the code above.
    It seems that data().count() does not have enough time to realize that the table has valid rows. The code refers to the "btn_apri_tutto" button which I would like to enable/disable depending on whether the table has valid records or it is empty. What can I do? Thank you for your help.

                                var table = $('#table_shifts').DataTable({
                                  "ajax" : {
                                    url: "gestione_turni.php",
                                    cache: false,
                                    select:"single",
                                    data: {
                                    "job": "get_shifts_in_hour",                                
                                    "filter": function() {      
                                         return dataData;
                                         // $('#datetimepicker2').data('date');
                                    },
                                    "username" : "",
                                    "password" : "",
                                    "id" :       selID,
                                    "nome" :     selNome,
                                    "cognome" :  selCognome,
                                    "kaikan" :   selKaikan,
                                    "id_ruolo" : selId_Ruolo,
                                    "ruolo" :    selRuolo,
                                    "gerarchia" : selGerarchia,
                                    "staff"     : selStaff,
                                    "id_zona" : selId_Zona,
                                    "zona" :  selZona,
                                    "territorio" :  selTerritorio       
                                      },
                                      dataType:     "json",
                                      method:       "POST"
                                                                      
                                    },                           
                                  "columns": [
                                     { "data": "persona" },
                                     { "data": "staff" },                            
                                     { "data": "attivita" },
                                     { "data": "inizio" },
                                     { "data": "fine" },                                
                                     { "data": "locazione" },
                                     {
                                         "targets": 6,
                                         "data": "pulsanti",
                                         "render": function ( data, type, row, meta ) {
                                         
                                             if (data == "YES")
                                             {          
                                                var y = ++i;
                                                return "<button name='btn_mod_"+ y + "' type='button' title='Modifica turno' class='btn btn-sm'><i class='fa fa-edit'></i></button><span>&nbsp;</span><button name='btn_del_" + y + "' type='button' title='Elimina turno' class='btn btn-sm'><i class='fa fa-eraser'></i></button>"
                                             }
                                             else
                                             {                                
                                                  return "  ";
                                             }
                                          }   
                                                                 }                               
                                    ],
                                                               "idisplayLength":10,
                                   "lengthMenu":[[10, 25, 50, -1], [ 10, 25, 50, "Tutti"]],  
                                   "language": {
                                            "emptyTable": "Non ci sono turni inseriti per questa data",
                                        "lengthMenu": "Visualizza _MENU_ turni per pagina",
                                        "zeroRecords": "Nessun turno con questo criterio di ricerca",
                                        "info": "Mostra pagina _PAGE_ di _PAGES_",
                                        "infoEmpty": "",
                                        "infoFiltered": "(trovati _MAX_ sul totale dei turni)",
                                        "search":         "Cerca:",
                                        "paginate": {
                                                "first":      "Prima",
                                                "last":       "Ultima",
                                                "next":       "Successiva",
                                                "previous":   "Precedente"
                                            }
                                    }, 
                                     "columnDefs": [
                                            { responsivePriority: 1, targets: 0 },
                                            { responsivePriority: 1, targets: 1 },
                                            { responsivePriority: 10001, targets: 2 },
                                            { responsivePriority: 10001, targets: 3 },
                                            { responsivePriority: 10001, targets: 4 },
                                            { responsivePriority: 10001, targets: 5 },                                  
                                            { responsivePriority: 1, targets: 6 }                                   
                                        ],
                                      "order": [[1, 'asc']],
                                  "success" : function(data) {
                                        console.log(data);              
                                                                     },
                                          "error": function(error, x, y)    
                                           {
                                        //$('#message_container').show();
                                        //document.getElementById('signin_container').style.visibility = 'hidden';
                                        //document.getElementById('staff_container').style.visibility = 'hidden';
                                        //document.getElementById('table_container').style.display = 'block';
                                        //document.getElementById('message_container').style.display = 'none';  
                                                                    //document.getElementById('new_shift_container').style.display = 'none';     
                                                               },
                                  "destroy":    true,
                                  "responsive": true
                                  //"responsive" : {
                                      // details: {
                                      // mostrava subito le nested table aperte
                                      //       display: $.fn.dataTable.Responsive.display.childRowImmediate
                                      //   }
                                      //   }
                                  });                                             
                                            
    
    
                                if (  table.data().count() == 0) 
                                {
                                    alert( 'Empty table' );
    
                                    $('#btn_apri_tutto').attr('disabled','disabled');
                                }
                                else
                                {
                                    alert( 'Full table' );
    
                                    $('#btn_apri_tutto').removeAttr('disabled');
                                }
    

    Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Wait for the table to become ready with the initComplete callback function. Run your table.data().count() check in there.

    Allan

  • marisatrainimarisatraini Posts: 10Questions: 3Answers: 0

    Thanks for your help. I tried and it works fine.

Sign In or Register to comment.