yii2-dynamicform icon indicating copy to clipboard operation
yii2-dynamicform copied to clipboard

Help, column sum

Open ikeraldasoro opened this issue 9 years ago • 5 comments

Hi,

I have the dynamic form working well, with four fields. Two of them are, "hours" and "minutes".

Can i have other field in the page where it will be the sum of total hours of dynamic fields and other with minutes?

While i add new fields and i insert in then data it have to change value in the new field.

Sorry for my english, i hope you understand what i want.

Thanks

ikeraldasoro avatar Sep 11 '15 11:09 ikeraldasoro

Hope this help,

Use the event onChange from the dynamic field linked to a js function

<div class="col-md-2">
  <?= $form->field($modelQuotedItem, "[{$i}]cost")->textInput([
    'maxlength' => true,
    'style' => 'text-align: right',
    'onchange' => 'totalsCalculate( $(this) )'
  ]) ?>
</div>

The js function receives the object

function totalsCalculate(item) {
  var noItem     = item.attr("id").replace(/[^0-9.]/g, "");
  var cost       = amount($("#quoteditems-" + noItem + "-cost").val());
  var qty        = amount($("#quoteditems-" + noItem + "-quantity").val());   // etc...
  alert(cost);
}

this way you can get the value of any dynamic field an do what you want with this.

P.S. I'm far from an expert on js, any better way?. sorry for my goog-english

ircsasw avatar Sep 15 '15 23:09 ircsasw

Hi, I solved like this, thank you for the idea:

i use the onfocus event of dynamic fiel->

                            <?= $form->field($modelOrduak, "[{$i}]orduak")->textInput(['maxlength' => true,'onfocus'=>'sumar()', 'onBlur'=>'sumar()']) ?>

And then call to the js function->

function sumar(){
id = 0; suma = 0; sumaMIN = 0; existe = true; while(existe){
var idFull = "orduak-"+id+"-orduak"; var idFullMin = "orduak-"+id+"-minutuak"; try{campo = document.getElementById(idFull); if(document.getElementById(idFull).value!=''){ suma = suma + parseInt(document.getElementById(idFull).value); } if(document.getElementById(idFullMin).value!=''){ sumaMIN = sumaMIN + parseInt(document.getElementById(idFullMin).value); }

        id = id+1;
    }catch(e){
       existe = false;
    }  
}

if (sumaMIN > 59){ div = Math.floor(sumaMIN / 60); sumaMIN = sumaMIN % 60; suma = suma + div; } document.getElementById('Suma').innerHTML=suma + "h" + sumaMIN + "min";

} function NoSumar(){

}

Thank you very much!

ikeraldasoro avatar Sep 16 '15 07:09 ikeraldasoro

I need help getting a value for each row, I have two columns quantity and price and I need to get the value for a third column called total. how can I do it?

Sorry for my bad english.

MSaez avatar Feb 22 '16 01:02 MSaez

Msaez? Do you find solution? Please i'm in the same case.

melvsw avatar May 18 '20 16:05 melvsw

Msaez? Do you find solution? Please i'm in the same case.

I have a project where I did that, I can't exactly explain it as I did but I can share the link to what you need:

Js File: https://github.com/MSaez/EyV/blob/master/web/js/presupuesto.js _form file: https://github.com/MSaez/EyV/blob/master/views/presupuesto/_form.php

I hope that help you

MSaez avatar May 20 '20 23:05 MSaez