Skip to content Skip to sidebar Skip to footer

Sum Of Textboxes In Yii2 Dynamic Form

I'm trying to get sum of all the 'Price' textboxes in another textbox 'Amount'. The textbox for price is ssgi_price and the textbox for Amount is ssg_amount. The intention is whene

Solution 1:

You can use the following:

First give a CSS class to your inputs:

<divclass="col-sm-3"><?=$form->field($modelSellitemsg, "[{$i}]ssgi_price")->textInput([
    'maxlength' => true,
    'class' => 'sumPart']) ?></div>

and

<?=$form->field($model, 'ssg_amount')->textInput(['class' => 'sum']) ?>

Then register the following jQuery:

<?php
/* start getting the totalamount */
$script = <<<EOD
    var getSum = function() {

        var items = $(".item");
        var sum = 0;

        items.each(function (index, elem) {
            var priceValue = $(elem).find(".sumPart").val();
            //Check if priceValue is numeric or something like that
            sum = parseInt(sum) + parseInt(priceValue);
        });
        //Assign the sum value to the field
        $(".sum").val(sum);
    };

    //Bind new elements to support the function too
    $(".container-items").on("change", ".sumPart", function() {
        getSum();
    });
EOD;
$this->registerJs($script);
/*end getting the totalamount */
?>

Post a Comment for "Sum Of Textboxes In Yii2 Dynamic Form"