Update elements inside a for using jquery

0

Hello,

I'm starting in web programming, and my question I believe is simple, Today I have a certain page that contains a forech to display a list of a request for dynamically

   <tbody>
                  <?php $i =0; foreach($itens as $item): ?>
                    <tr>
                      <td>
                        <?=  $item->item_id ?>
                      </td>
                      <td>
                        <?= $item->getNomeProduto($item->item_prod) ?>
                      </td>
                      <td>
                        <?php  if($pedMagazine->maga_tolerancia==1){?>
                          <input type="number" name="item_vlruni" id="item_vlruni" value="<?= $item->item_vlruni?>" disabled/>
                          <input type="hidden" name="item_vlruni[]" value="<?= $item->item_vlruni?>" />
                          <?php }else echo  $item->item_vlruni  ;?>
                      </td>
                      <td>
                        <?php  if($pedMagazine->maga_tolerancia==1){?>
                          <input type="number" name="item_qtd" id="item_qtd" min="<?= round($item->item_qtd*(1-($pedMagazine->maga_toleranciaperc/100)))?>" max="<?= round($item->item_qtd*(1+($pedMagazine->maga_toleranciaperc/100)))?>" onchange='sePedidoItem.somar();' step="1"
                          value="<?= $item->item_qtd?>" />
                          <input type="hidden" name="item_id[]" value="<?= $item->item_id?>" />
                          <?php }else {echo $item->item_qtd; }?>
                      </td>
                      <td>
                        <?php  if($pedMagazine->maga_tolerancia==1){?>
                          <input type="number" name="item_vlrtot[]" id="item_vlrtot" value="<?= $item->item_vlrtot?>" disabled/>
                          <input type="hidden" name="item_vlrtot[]" value="<?= $item->item_vlrtot?>" />
                          <?php }else echo $item->item_vlrtot; ?>
                      </td>
                      <?php //$subTotal+=$item->item_vlrtot ;?>
                    </tr>
                    <?php $i++; endforeach;  ?>
                </tbody>

I'm using jquery to update the fields of vlrtotal (total value) that would be multiplying vlruni with qtd, my function;

   somar: function () {        


     $("#vlrTot").val($("#qtd").val()*$("#vlrUni").val());
      $("#item_vlrtot[]").val($("#item_qtd").val()*$("#item_vlruni").val());},

But since I'm using a foreach it is only updating the first field and the rest does not update.

Does anyone have any idea how to do it to update all fields regardless of the size of the for?

    
asked by anonymous 25.08.2016 / 14:26

2 answers

0

Thanks for the help,

  <tbody>
                  <?php  foreach($itens as $key=>$item): ?>
                    <tr>
                      <td>
                        <?=  $item->item_id ?>
                      </td>
                      <td>
                        <?= $item->getNomeProduto($item->item_prod) ?>
                      </td>
                      <td>
                        <?php  if($pedMagazine->maga_tolerancia==1){?>
                          <input type="number" name="item_vlruni" id="item_vlruni<?= $key ?>" value="<?= $item->item_vlruni?>" disabled/>
                          <input type="hidden" name="item_vlruni[]" value="<?= $item->item_vlruni?>" />
                          <?php }else echo  $item->item_vlruni  ;?>
                      </td>
                      <td>
                        <?php  if($pedMagazine->maga_tolerancia==1){?>
                          <input type="number" name="item_qtd" id="item_qtd<?= $key ?>" min="<?= round($item->item_qtd*(1-($pedMagazine->maga_toleranciaperc/100)))?>" max="<?= round($item->item_qtd*(1+($pedMagazine->maga_toleranciaperc/100)))?>" onchange='sePedidoItem.somar(<?= $key ?>);' step="1"
                          value="<?= $item->item_qtd?>" />
                          <input type="hidden" name="item_id[]" value="<?= $item->item_qtd?>" />
                          <?php }else {echo $item->item_qtd; }?>
                      </td>
                      <td>
                        <?php  if($pedMagazine->maga_tolerancia==1){?>
                          <input type="number" name="item_vlrtot[]" id="item_vlrtot<?= $key ?>" value="<?= $item->item_vlrtot?>" disabled/>
                          <input type="hidden" name="item_vlrtot[]" value="<?= $item->item_vlrtot?>" />
                          <?php }else echo $item->item_vlrtot; ?>
                      </td>
                      <?php $subTotal+=$item->item_vlrtd ;?>
                    </tr>
                    <?php  endforeach;  ?>
                </tbody>

And in jquery it was like this

 somar: function (i) {

// soma para os itens editados no pedido
$('#vlrTot').val($('#qtd').val() * $('#vlrUni').val())

// logica para atualizar campos na tela confirmar 
var antigo = $('#item_vlrtot' + i).val()
// atualiza o campo vlrtot
$('#item_vlrtot' + i).val($('#item_qtd' + i).val() * $('#item_vlruni' + i).val())
var soma = $('#item_vlrtot' + i).val() - antigo
soma = soma + parseInt(document.getElementById('subTotal').textContent)
// atualiza valortotal
document.getElementById('subTotal').textContent = soma  },
Note that in my html I changed my foreach "oreach ($ items as $ key = > $ item)" and added to get a value in the add function of jquery "add: function (i) {" in case " i ", so in the onchange I call the function together with the key" onchange = 'sePedidoItem.somar ($ key "with the php tags");' "and I can perform the calculation dynamically.

    
25.08.2016 / 18:20
0

Ciro, I see that you have a table with inputs for unit value, quantity and total value.

The first problem I see is the repetition of id s in the elements. as% s of% s should be unique, consider changing the strategy used to name them, or even remove% with% (in the case above it seems to me that this should not affect your application).

Another point, I do not see the need for a id and id simultaneously. I believe input[type='hidden'] is only interesting if input[type='number'] , or rather, use input[type='hidden'] for everything and just put maga_tolerancia == 1 on inputs.

Finally, add a input[type='number'] to the line, we can do this to use the line ( readonly='readonly' ) as scope for your operations, as well as classes for class s.

<tbody>
  <?php $i =0; foreach($itens as $item): ?>
  <tr class="item">
    <td>
      <?=  $item->item_id ?>
    </td>
    <td>
      <?= $item->getNomeProduto($item->item_prod) ?>
    </td>
    <td>
      <input type="number" name="item_vlruni[]" class="item_vlruni" 
            value="<?= $item->item_vlruni?>" <?php if($pedMagazine->maga_tolerancia!=1){?>readonly='readonly' disabled='disabled'<?php }?>/>
    </td>
    <td>      
      <input type="number" name="item_qtd[]" class="item_qtd" step="1" 
            min="<?= round($item->item_qtd*(1-($pedMagazine->maga_toleranciaperc/100)))?>" 
            max="<?= round($item->item_qtd*(1+($pedMagazine->maga_toleranciaperc/100)))?>" 
            value="<?= $item->item_qtd?>" <?php if($pedMagazine->maga_tolerancia!=1){?>readonly='readonly' disabled='disabled'<?php }?> />
    </td>
    <td>
      <input type="number" name="item_vlrtot[]" class="item_vlrtot" 
            value="<?= $item->item_vlrtot?>" <?php if($pedMagazine->maga_tolerancia!=1){?>readonly='readonly' disabled='disabled'<?php }?>/>
    </td>
    <?php //$subTotal+=$item->item_vlrtot ;?>
  </tr>
  <?php $i++; endforeach;  ?>
</tbody>

Note that I am not a programmer tr , so there may be some basic error in the above suggestion.

Now let's go to input :

var itens = $('.item');
itens.each(function (indice) {
  var item = $(this);
  var item_vlruni = $(".item_vlruni", item);
  var item_qtd = $(".item_qtd", item);
  var item_vlrtot = $(".item_vlrtot", item);

  var onInput = function (event) {
    var vlruni = parseFloat(item_vlruni.val());
    var qtd = parseFloat(item_qtd.val());
    item_vlrtot.val(vlruni * qtd);
  }

  item_vlruni.on("input", onInput);
  item_qtd.on("input", onInput);  
});

Note that I added PHP to JavaScript , I made it so that it was possible to select all the rows, then I go through each row selecting only the inputs inside them, so I pass the row as the second argument in the selector , as in the example class='item' , finally I make the tr of the event $(".item_vlruni", item) , which is where the calculation will be performed.

    
25.08.2016 / 15:42