Skip to content Skip to sidebar Skip to footer

How Sum Dynamic Fields Using Wbraganca-yii2-dynamicform?

I'm new here. I need help. I use a plugins called 'wbraganca / yii2-dynamicform'. I have a form that allows the sum of two fields (caja1 and caja2, and the result of the add is 'to

Solution 1:

_form.php

<tdclass="sortable-handle text-center vcenter"><?=$form->field($modelOptionValue, "[{$index}]horas")->textInput(['onkeyup' => 'totales($(this))'])->label(false); ?></td><tdclass="sortable-handle text-center vcenter"><?=$form->field($modelOptionValue, "[{$index}]valorhora")->textInput(['onkeyup' => 'totales($(this))'])->label(false); ?></td><tdclass="sortable-handle text-center vcenter"><?=$form->field($modelOptionValue, "[{$index}]total")->textInput()->label(false); ?></td>

JS

Use auto-generated field id instead of custom id. You can inspect element to find what is generated id for field. Assuming your field id. For ex. modelOptionValue-1-horas for first field.

functiontotales(item){   
   var index  = item.attr("id").replace(/[^0-9.]/g, "");   
   var horas = $('#modelOptionValue-' + index + '-horas').val();
   horas = horas == "" ? 0 : Number(horas.split(",").join(""));
   var valorhora = $('#modelOptionValue-' + index + '-valorhora').val();
   valorhora = valorhora == "" ? 0 : Number(valorhora.split(",").join(""));
   $('#modelOptionValue-' + index + '-total').val(horas + valorhora);     
}

Post a Comment for "How Sum Dynamic Fields Using Wbraganca-yii2-dynamicform?"