Changing values of a table row by row

Asked

Viewed 108 times

1

I have a little problem. I have a table with stock values, and I need to change these values as follows. The customer will insert a quantity that he wishes to manufacture of a certain product, and in the system need to inform the quantity that will be used of each raw material. But what’s wrong with me? the JS created is returning the value of the first line *(times) the amount reported, for each line, when the value of each line should be *(times) the amount reported. Follow the script if someone has a LIGHT =D

    $('#calcular').click(function(){


    var qtd = $('#qtd').val();

    $.ajax({
        beforeSend: function(){
            waitingDialog.show('Calculando',{dialogSize: 'sm'});
        },
        success: function(){
            waitingDialog.hide();
            $('.comp_qtd').html(parseInt($('.comp_qtd').html())*parseInt(qtd));
        }
    })

});

Follow the button created to "Calculate":

                                        <div class="col-sm-2">
                                            <?= $this->Form->input('qtd',[
                                                'label' => 'Quantidade',
                                                'type' => 'text',
                                                'placeholder' => 'Digite a qtd',
                                                'id' => 'qtd',
                                                'name' => 'qtd',
                                                'class' => 'form-control',
                                                'title' => 'Digite a quantidade a ser produzido na OS',
                                                'required' => true
                                            ]);
                                            ?>
                                        </div>
                                        <div class="col-sm-2">
                                            <button id="calcular" class="btn btn-primary pula" type="button">Calcular <i class="glyphicon glyphicon-sort"></i></button>
                                        </div>

and the table to be amended:

                                        <thead>
                                        <tr>
                                            <th>Codigo</th>
                                            <th>Produto</th>
                                            <th>Unidade</th>
                                            <th>Quantidade Necessaria</th>
                                            <th>Quantidade em Estoque</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach($arrayComposicao as $composicao) :?>
                                        <tr>
                                            <td id="comp_codigo"><?= $composicao['Produto']['pro_codigo_id']?></td>
                                            <td id="comp_descricao"><?= $composicao['Produto']['pro_descricao']?></td>
                                            <td id="comp_medida"><?= $composicao['Composicao']['comp_mp_medida']?></td>
                                            <td class="comp_qtd"><?= $composicao['Composicao']['comp_mp_qtd']?></td>
                                            <td id="comp_estoque"><?= $composicao['Produto']['pro_qtd_estoque']?></td>
                                        </tr>
                                        <?php endforeach; ?>
                                    </tbody>

2 answers

1


tries to create an id(dynamic or item id) for each Row, and do the calculation based on the id of the Row. For example pseudo-code:

...
(parseInt($('idItem'))*parseInt(qtd)
...

It’s not a light, but I hope it’s a spark ehehhe

  • I understood the idea, but how would you do for each <td> search a dynamic id by Row?

  • Conssegui generate dynamic ID, now I will search on how to make JS take this generated id and make the change according to the generated id

1

I solved my problem this way

JS:

    $('#calcular').click(function(){


    var qtd = $('#qtd').val();
    for (i = 0; i < 5; i++){
        var id = '#Item' + i;
    }

    $.ajax({
        beforeSend: function(){
            waitingDialog.show('Calculando',{dialogSize: 'sm'});
        },
        success: function(){
            waitingDialog.hide();
            for (i = 0; i < 50; i++){
                var id = '#Item' + i;
                $(id).html(parseInt($(id).attr( "value" ))*parseInt(qtd));
            }
        }
    })

});

and the HTML to be changed

<td class="comp_qtd" id="Item<?=$composicao['Composicao']['comp_mp_item']?>" value="<?php echo $composicao['Composicao']['comp_mp_qtd'];?>"><?php echo $composicao['Composicao']['comp_mp_qtd'];?></td>

Browser other questions tagged

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