Load checkbox marked and calculate them

Asked

Viewed 99 times

0

I have an imputs field, with values. I made the page add like this:

HELP I NEED: On the edit page, when loading the page, list the checkbox options marked.

$(".c").on('change', function() {

  var values = $(this).val().split('|');
  if ($(this).prop('checked')) {

    var quantity = $("table[id^=tabelaIE]").length;

    $(this).data('qty', quantity);

    var table = $("#tabelaIE").clone(true)
      .attr('id', function() {
        return this.id + quantity;
      })
      .find(':text,:file')
      .attr('id', function() {
        return this.id + quantity;
      })
      .val("")
      .end();

    //VALUE -> 1|Taxa de locação|ENC|15.00|FIX|POR|VLT|%
    //$ID_ImpEnc|$nome|$tipo|$valor|$calculo|$valor_em|$aplicacao|$a

    //APLICAÇÃO: [SEG] Sobre seguro, [KML] KmLivre, [DIA] Diária, [IE] Impostos/Encargos, [VLT] no valor total
    //VALOR EM.: [MOT] Montante, [POR] Porcentagem.
    //CALCULO..: [DIA]Por dia, [FIX]Fixo
    //TIPO.....: [TXV]Taxa de Venda, [ENC]Encargos Extras

    aplicacao = values[6];
    valor_em = values[5];
    calculo = values[4];
    tipo = values[2];

    if (valor_em == "MON" && calculo == "FIX") {
      table.find('.tNome').text(values[1]);
      table.find('.tDias').text("1");
      table.find('.tValor').text(values[3]);
      tTotal = values[3] * 1;
      table.find('.tTotal').text(tTotal.toFixed(2));
    }

    if (valor_em == "MON" && calculo == "DIA") {
      table.find('.tNome').text(values[1]);
      table.find('.tDias').text($("#dias").val());
      table.find('.tValor').text(values[3]);
      tTotal = values[3] * $("#dias").val();
      table.find('.tTotal').text(tTotal.toFixed(2));
    }

    if (valor_em == "POR" && calculo == "FIX") {
      if (aplicacao == "SEG") {
        $V = (50 + 30) / 100;
        table.find('.tNome').text(values[1]);
        table.find('.tDias').text("1");
        table.find('.tValor').text(values[3]);
        tTotal = values[3] * $V;
        table.find('.tTotal').text(tTotal.toFixed(2));
      }
      if (aplicacao == "KML") {
        $V = values[3] / 100;
        table.find('.tNome').text(values[1]);
        table.find('.tDias').text("1");
        table.find('.tValor').text(values[3]);
        tTotal = values[3] - $V;
        table.find('.tTotal').text(tTotal.toFixed(2));
      }
      if (aplicacao == "DIA") {
        $V = (values[3] * $("#dias").val()) / 100;
        table.find('.tNome').text(values[1]);
        table.find('.tDias').text($("#dias").val());
        table.find('.tValor').text(values[3]);
        tTotal = values[3] * $V;
        table.find('.tTotal').text(tTotal.toFixed(2));
      }
      if (aplicacao == "VLT") {
        $V = $("#totalFatura").val() / 100;
        table.find('.tNome').text(values[1]);
        table.find('.tDias').text("1");
        table.find('.tValor').text(values[3].substring(0, 2) + "%");
        tTotal = values[3] * $V;
        table.find('.tTotal').text(tTotal.toFixed(2));
      }
    }



    values.forEach(function(valor, index) {
      table.find('[class="split' + (index + 1) + '"]').val(valor)
    });

    table.appendTo('#abc');

    var oldVal = $('#somaTabelaIE').val();
    $('#somaTabelaIE').val(eval(oldVal || 0) + eval(tTotal));

  } else {

    tTotal = values[3] * $("#dias").val();
    var oldVal = $('#somaTabelaIE').val();
    $('#somaTabelaIE').val(oldVal - eval(tTotal));

    //remove a table que pertence ao checkbox
    $("table#tabelaIE" + $(this).data('qty')).remove();
  }
  $(".recalcula").click();
}).change();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Dias: <input type="text" value="2" id="dias"> Total da Fatura<input type="text" id="totalFatura" value="100.00" />
<p>===============================================</p>
<strong>OPÇÕES:</strong>
<div id="D1">1.
  <input name="opcoes[]" class="c Obrig" type="checkbox" checked value="1|Taxa de locação|ENC|15.00|FIX|POR|VLT|%" alt="15.00" title="Taxa de locação" /> <b> Taxa </b>
</div>
<div id="D2">2.
  <input name="opcoes[]" class="c " type="checkbox" checked value="2|GPS|ENC|5.00|DIA|MON|DIA|" alt="5.00" title="GPS" /> GPS
</div>
<div id="D3">3.
  <input name="opcoes[]" class="c " type="checkbox" value="3|Lavagem do carro|ENC|30.00|FIX|MON|VLT|" alt="30.00" title="Lavagem do carro" /> Lavagem
</div>
<div id="D4">4.
  <input name="opcoes[]" class="c " type="checkbox" checked value="4|Translado aéreo retirada|ENC|35.00|FIX|MON|VLT|" alt="35.00" title="Translado aéreo retirada" /> Translado
</div>


<p>===============================================</p>
<strong>LISTAS DAS OPÇÕES SELECIONADAS</strong>
<div id="abc">
  <table id="tabelaIE" width='400' border='0'>
    <tr>
      <td class="tNome" width='190px'></td>
      <td class="tDias" width='50px' align='center'></td>
      <td class="tValor" width='60px' align='right'></td>
      <td class="tTotal" width='100px' align='right'></td>
      <td class="tValores">
        <input type="hidden" class="split1" value="">
        <input type="hidden" class="split2" value="">
        <input type="hidden" class="split3" value="">
        <input type="hidden" class="split4" value="">
        <input type="hidden" class="split5" value="">
        <input type="hidden" class="split6" value="">
        <input type="hidden" class="split7" value="">
        <input type="hidden" class="split8" value="">
      </td>
    </tr>
  </table>
</div>
<p>===============================================</p>

Soma das opções:
<input type="text" class="somaTabelaIE" id="somaTabelaIE" value="0">

  • Can you explain better what you mean by "List options with checkbox marked"?

  • Hello, Sergio. When you execute the code you will see that you have checkbox marked. You can see?

  • 1

    I don’t get it, that’s not what you wanted?

  • Tiago, it is not yet clear what you need... when this code that you have in question loads I see already some checkbox marked. What you lack?

  • @Sergio Currently the code only works if we click on the checkbox. I would like when loading the page, check the checkbox that are marked, load the list and already make the total sum in imput somaTabelaIE

  • @Leandroangelo Currently the code only works if we click on the checkbox. I would like when loading the page, check the checkbox that are marked, load the list and already make the total sum in imput somaTabelaIE

  • James, but I did it in the other question you had... on that last line where }).change(); // este ultimo change é só para correr o código no inicio

  • @Sergio In that previous question I was days breaking my head and decided to make a ganbiarra, and then I realized that I would have problem in the future. That code only makes the calculations, I need to be listed when loading the page the options that have already been selected. Look at this link I think you’ll get a better view https://jsfiddle.net/zoareu4v/

  • @Tiago Look, their checkboxes are already marked by what they have the attribute checked avowed. If you want to perform the calculation routine on page loading, simply refactor and shift the code that is in the click event to a new scope.

  • @Sergio Following this example (https://jsfiddle.net/zoareu4v/) when loading the page appears like this https://prnt.sc/mu1j5d, I would like it to appear like this http://prntscr.com/mu1r7j

  • @Leandroangelo Following this example (https://jsfiddle.net/zoareu4v/) when loading the page appears like this https://prnt.sc/mu1j5d, I would like it to already appear like this http://prntscr.com/mu1r7j

  • @Sergio Entendeu?

  • @Leandroangelo Got it?

  • Help me please,

  • @Sergio Help me, please.

  • Hi Tiago. I don’t understand why you unchecked the answer as right/accepted. This is not the best way to get my attention. I’m on a tight schedule now, so I’ll take a look later.

  • @Sergio my friend, I’m sorry I had to do I had to cancel! I didn’t know! I really need your help, because I don’t know how to do it. That’s all I need to finish the code.

  • @Sergio good morning. Please help me today

  • @Sergio give me that strength, please.

  • @Sergio You can help me?

  • @Good morning, could you help me today?

  • @Sergio you can help me?

Show 17 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.