yii2-dynamicform
yii2-dynamicform copied to clipboard
Help, column sum
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
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
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!
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? Do you find solution? Please i'm in the same case.
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