Acceder Registrarme

SUMAR COLUMNA DE UN DATATABLE EN PHP Y JAVASCRIPT Y MOSTRAR EL TOTAL EN EL PIE DE PÁGINA


Por: Jersson Jorge Corilla Miranda Publicado el: 2021-07-21 20:01:23 Pregunta abierta

Buenas noches, quisiera saber como puedo sumar los valores de una columna de un datatable en PHP  con JavaScript y mostrar el total en el pie de página del Datatable le dejo el código a continuación:

Código javascript:

var tablegastos;

 

function lista_gastos(){

    tablegastos = $("#tabla_gastos").DataTable({

      "ordering":false,   

      "bLengthChange":false,

      "searching": { "regex": false },

      "lengthMenu": [ [10, 25, 50, 100, -1], [10, 25, 50, 100, "All"] ],

      "pageLength": 10,

      "destroy":true,

      "async": false ,

      "processing": true,

      "ajax":{

          "url":"../controlador/gastos/controlador_gastos_listar.php",

          type:'POST',       

      },

      "columns":[

          {"data":"Posicion"},

          {"data":"tipo_gasto",

          render: function (data, type, row ) {

            if(data=='ALQUILER'){

                return "<span class='badge badge-primary'>"+data+"</span>";               

            }else if(data=='MATERIAL'){

              return "<span class='badge badge-secondary'>"+data+"</span>";                 

            }else{

              return "<span class='badge badge-success'>"+data+"</span>";                 

            }

          }

        },

          {"data":"gasto"},

          {"data":"gasto_cantidad"},

          {"data":"gasto_fecharegis"},

          {"data":"monto"},   

          {"defaultContent":"<button style='font-size:13px;' type='button' class='editar btn btn-warning' title='Editar cita'><i class='fa fa-edit'></i></button>"}

        ],

      "language":idioma_espanol,

      select: true

  });

 

Código PHP:

 <table id="tabla_gastos"class="display responsive nowrap mt-0" style="width:100%" style="text-align: center">

            <thead>

                <tr>

                    <th>Nro</th>

                    <th>Tipo de Gasto</th>

                    <th>Descripción</th>

                    <th>Cantidad</th>

                    <th>Fecha</th>

                    <th>Monto</th>

                    <th>Acci&oacute;n</th>

                </tr>

            </thead>

            <tfoot>

                <tr>

                     <th>Nro</th>

                    <th>Tipo de Gasto</th>

                    <th>Descripción</th>

                    <th>Cantidad</th>

                    <th>Fecha</th>

                    <th>Monto</th>

                    <th>Acci&oacute;n</th>

                </tr>

            </tfoot>

            </table>




Javascript

Usuario de (Perú)

Buen día, Jersson.

Te dejo este enlace, donde, basado en la propia documentación, hay un ejemplo https://datatables.net/plug-ins/api/sum()

Saludos.

  • 2021-07-21 20:06:06
  • Me gusta(0)

  • Debes estar logueado para realizar comentarios