i need execute function after draw table

i need execute function after draw table

drodriguezdrodriguez Posts: 8Questions: 2Answers: 0

Hi! Thanks in advance for the help! I am needing to execute my own function once the table is finished drawing

I add a section of code that was used to load the data from the table (this method because the rows and columns are dynamic that when arriving at the table I do not know how many rows or columns arrive)

$('#res_x_emp').DataTable( {
                        "language": {
                        "url": "//cdn.datatables.net/plug-ins/1.10.21/i18n/Spanish.json"
                        }, "ordering": false,
                        data: data.data,
                        columns: columns
                    });

Answers

  • kthorngrenkthorngren Posts: 13,606Questions: 25Answers: 3,204

    Are you looking for this function to run once after initialization (use initComplete) or each time the table draws (use either drawCallback or draw)?

    Kevin

  • drodriguezdrodriguez Posts: 8Questions: 2Answers: 0

    I really need it once the table is finished drawing, the rows and columns are loaded, as you can see with the options you gave me, it launches the alert but the rows or columns are not drawn yet

  • kthorngrenkthorngren Posts: 13,606Questions: 25Answers: 3,204

    Does the table load columns and row data without initComplete?

    Can you provide a link to your page or a test case replicating the issue so we can see what is happening and help debug?
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • drodriguezdrodriguez Posts: 8Questions: 2Answers: 0
    edited September 13
    $.ajax({
                    type: "POST",
                    url: "funciones_gh_horas_extras.php",
                    data: { opcion: 'BuscarRegEmpl', fecha_inicio : fecha_inicio,fecha_fin:fecha_fin,proyecto:proyecto,Empleado : emp },
                    beforeSend: function () { },
                    success: function (response) {        
    
                        if(response!="[]"){
                            var columns = [];
                            data = JSON.parse(response);
                                 // console.log(data);
                            columnNames = Object.keys(data.data[0]);
                            for (var i in columnNames) {
                              columns.push({data: columnNames[i], title: columnNames[i]});
                            }
                            $('#res_x_emp').DataTable( {
                                "language": {
                                "url": "//cdn.datatables.net/plug-ins/1.10.21/i18n/Spanish.json"
                                }, "ordering": false,
                                data: data.data,
                                columns: columns, initComplete: function( settings ) {
                                    alert( 'DataTables has redrawn the table' );
                                }
                            });
    
    
                        
                            $('#res_x_emp_copy').DataTable( {
                                "language": {
                                "url": "//cdn.datatables.net/plug-ins/1.10.21/i18n/Spanish.json"
                                }, "ordering": false,
                                data: data.data,
                                columns: columns , "ordering": false,dom: 'Bfrtip', 
                                buttons: [{
                                      extend: 'excel',
                                      filename:'Tiempo  Laborado Detalle Día',title:'Tiempo  Laborado Detalle Día',sheetName:'Resumen'
                                  }
                                ]
                            });
                        
                        }else{
    
                             $("#result").html("No se encontro resultado");
                        }
    
                        $(".form-control").removeClass("requerido");
    
                            
                        apagarload();
    
                    },error: function(response){
                        console.log(response);
                        apagarload();
                }
            });
    

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

  • kthorngrenkthorngren Posts: 13,606Questions: 25Answers: 3,204

    Thanks for the code snippet but the question still remains - is the table columns and rows populated properly?

    You showed a screenshot with an alert. What exactly did you use to generate the alert?

    Either please provide more details of what is happening and what you have you have done to troubleshoot or a link to a test case showing the issue. Without that we won't have the information needed to help debug.

    Kevin

  • drodriguezdrodriguez Posts: 8Questions: 2Answers: 0

    Yes, the table is filled correctly, at the moment the alert is launched I need to invoke another function but the grid must already be loaded for that

  • kthorngrenkthorngren Posts: 13,606Questions: 25Answers: 3,204

    Use initComplete to run a function after all the data has been loaded.

    Kevin

Sign In or Register to comment.