Because I do not calculate the rest of the inputs

1

I have a table and I am calculating the difference of hours but only calculates the first row of the table Here is my javascript code

 <script>
   //Creamos variables para recorrer fechas y agrupar total x día
    var afecha = new Array(); var fecha = ""; var i = 0; var firsttime = 1;
    var atotal = new Array(); var total = 0; var totaltotal = 0; var contartotal = 0; var multi =0;

  $(".td-calcular").each(function (){
     var HDesde = $(this).data("inicio");
     var HHasta = $(this).data("fin");

     //recoges el valor del día y en la primera entrada lo asignada a la var fecha
     var dia = $(this).data("dia");
     if (firsttime==1) { fecha = dia; firsttime = 0; }

     hora1 = (HDesde).split(":");
     hora2 = (HHasta).split(":");
     HoraDesde=(hora1[0]);
     MinutoDesde=(hora1[1]);
     HoraHasta=(hora2[0]);
     MinutoHasta=(hora2[1]);
     TotDesde=parseInt((HoraDesde*60)) + parseInt(MinutoDesde);
     TotHasta=parseInt(HoraHasta*60) + parseInt(MinutoHasta);
     RestaHoras=(TotHasta - TotDesde);
     TotHorasTrab=(RestaHoras / 60).toFixed(2);

     $(this).html(TotHorasTrab);

     //Ahora en el momento que cambies la fecha guardas los valores de fecha y total en dos arrays y pones total a 0
     if (fecha != dia) {
    afecha[i] = fecha;
    atotal[i] = total;
    i++;
    total = 0;
 }
 fecha = dia;
     //Incrementas total
     total = parseFloat(total) + parseFloat(TotHorasTrab);

  });
  //Añadimos el valor del último día y mostramos resultados alamacenados en el aray
  afecha[i] = fecha;
  atotal[i] = total;
  for (j=0; j<afecha.length; j++) {
     $( ".div-calcular" ).data( "calcular", atotal[j]); 
      totaltotal+=atotal[j];

    }

    contartotal = afecha.length;
    multi = afecha.length*9;
    $('#totaltotal').html(totaltotal);
    $('#countTotal').html(contartotal+' dias  ['+multi+']');

</script>

my table, I create a table for each different day.

 <?php if ($seguimientos): ?>
    <?php $fechas = array();
          foreach ($seguimientos as $seguimiento) {
             $fechas[$seguimiento-> fecha][] = $seguimiento;

          }
    ?>
    <?php foreach ($fechas as $fecha): ?>
    <table class="table table-hover">
    <thead>
        <tr>
            <th>Fecha</th>
            <th>Orden</th>

        </tr>
    </thead>
    <?php   foreach ($fecha as $seguimiento): ?>
    <tr>
        <td>
            <?php echo $seguimiento-> fecha; ?>
        </td>
        <td>
            <?php echo $seguimiento-> horaInicio; ?>
      <input type="hidden" id="horaInicio" value=" echo $seguimiento-> horaInicio;">
        </td>
        <td>
            <?php echo $seguimiento-> horaFin; ?>
    <input type="hidden" id="horaInicio" value=" echo $seguimiento-> horaFin;">
        </td>


    </tr>

    <?php endforeach; ?>
        </table>
      <div class="col-4 col-md-2">
              <h5> Total horas por fecha : <div class="div-calcular" data-calcular=""> </div> </h5>
              <h5> Horas turno :    9</h5>
              <h5> % Ocupacion por fecha: </h5>
            </div>
    <?php endforeach; ?>

 <div class="col-4 col-md-2">
      <h5>Total horas :  <p id="totalhrs" style="float:right;"></p>  </h5>
      <h5>Horas turno : <p id="countTotal" style="float:right;"></p> </h5>
      <h5>% Ocupacidad por fecha<p id="totalResultado" style="float:right;"></p> </h5>
            </div>
    <?php endif; ?>

    
asked by MoteCL 29.06.2018 в 15:47
source

2 answers

1

The ids must be unique, you can do it for example by assigning data with the attribute data and going through all the tds where you want to fill. It is a way to avoid creating inputs that do not seem to be part of a form.

<?php foreach ($fecha as $seguimiento): ?>
    <tr>
        <td>
            <?php echo $seguimiento->fecha; ?>
        </td>
        <td>
            <?php echo $seguimiento->horaInicio;?>
        </td>
        <td>
            <?php echo $seguimiento->horaFin;?>
        </td>
        <td class="td-calcular" data-inicio="<?php echo $seguimiento->horaInicio;?>" data-fin="<?php echo $seguimiento->horaFin;?>" data-dia="<?php echo $seguimiento->fecha;?>" >    
        </td>
    </tr>

And now you go through all the boxes where you need to calculate:

    //Creamos variables para recorrer fechas y agrupar total x día
    var afecha = new Array(); var fecha = ""; var i = 0; var firsttime = 1;
    var atotal = new Array(); var total = 0;

        $(".td-calcular").each(function (){
           var HDesde = $(this).data("inicio");
           var HHasta = $(this).data("fin");

           //recoges el valor del día y en la primera entrada lo asignada a la var fecha
           var dia = $(this).data("dia"); 
           if (firsttime==1) { fecha = dia; firsttime = 0; }

           hora1 = (HDesde).split(":");
           hora2 = (HHasta).split(":");
           HoraDesde=(hora1[0]);
           MinutoDesde=(hora1[1]);
           HoraHasta=(hora2[0]);
           MinutoHasta=(hora2[1]);
           TotDesde=parseInt((HoraDesde*60)) + parseInt(MinutoDesde);
           TotHasta=parseInt(HoraHasta*60) + parseInt(MinutoHasta);
           RestaHoras=(TotHasta - TotDesde);
           TotHorasTrab=(RestaHoras / 60).toFixed(2);

           $(this).html(TotHorasTrab);

           //Ahora en el momento que cambies la fecha guardas los valores de fecha y total en dos arrays y pones total a 0
           if (fecha != dia) {
          afecha[i] = fecha;
          atotal[i] = total;
          i++;
          total = 0;  
       }
       fecha = dia;
       //Incrementas total
       total = parseFloat(total) + parseFloat(TotHorasTrab);

        });
        //Añadimos el valor del último día y mostramos resultados alamacenados en el aray
    afecha[i] = fecha;
    atotal[i] = total;
    for (j=0; j<afecha.length; j++) {
        alert(afecha[j] + " - " + atotal[j]);
    }

Now you have two arrays, one with dates and one with the accumulated by date in the same position as the corresponding date. This is an example to save the values, if you want to show them for example in an input, instead of saving the values in the array you assign them as the value of the corresponding input.

Edited to Add total x day.

Edit as show total x day

A very simple way is to give the fields as id the date to be filled in and filled in by traversing the array that stores this sum data per day.

html

<div style="border: 1px solid #444" class="div-calcular" id="<?php echo $seguimiento->fecha;?>"></div>
<div style="border: 1px solid #444" class="div-calcular" id="<?php echo $seguimiento->fecha;?>"></div>

And in the script

for (j=0; j<afecha.length; j++) {
        var fecid = "#"+afecha[j];
        $(fecid).html(atotal[j]);
        alert(afecha[j] + " - " + atotal[j]);
    }
    
answered by 29.06.2018 / 18:36
source
2

As you have been put in the comment, the ids of the elements are unique and can not be repeated. In that case, just take the first element with that id.

I would recommend you go through the tables and their element td I have to collect the data to do the calculations. Since I do not see well how is the table I put a code that you will have to adapt to your html:

  var table = $("table tbody");

  table.each(function() {

      this.find('tr').each(function () {
          //Aquí tendrias que recuperar los datos y hacer los cálculos
            y añadirlo donde lo necesites
      });
 });
    
answered by 29.06.2018 в 16:16