Calculate 3 fields and display the result in the 4th in yii2 dynamic form

喜你入骨 提交于 2019-12-12 01:26:12

问题


I'm trying to calculate sum of 3 fields - si_mrp + si_discp + si_taxpc in yii2 dynamic form. This is the same solution of Calculate from 3 inputfield in dynamic form yii2. But the present solution is not working. form fields

<?= $form->field($modelsProductsales, "[{$i}]si_mrp")->label(false)->textInput(['maxlength' => true,'onchange' => 'getUdisc($(this))', 'onkeyup' => 'getUdisc($(this))','class' => 'mrp','placeholder' => 'MRP']) ?>
<?= $form->field($modelsProductsales, "[{$i}]si_discp")->label(false)->textInput(['maxlength' => true,'onchange' => 'getUdisc($(this))', 'onkeyup' => 'getUdisc($(this))','class' => 'discp','placeholder' => 'Disc %']) ?>
<?= $form->field($modelsProductsales, "[{$i}]si_taxpc")->label(false)->textInput(['maxlength' => true,'onchange' => 'getUdisc($(this))', 'onkeyup' => 'getUdisc($(this))','class' => 'taxpc','placeholder' => 'Tax %']) ?>
<?= $form->field($modelsProductsales, "[{$i}]si_rate")->label(false)->textInput(['maxlength' => true,'class' => 'rate','placeholder' => 'Rate']) ?>


javascript code - 
<?php
/* start getting the rate */
$script = <<< JS
function getUdisc(item) {
    var index  = item.attr("id").replace(/[^0-9.]/g, "");
    var total = current = next = previous = 0;

    var id = item.attr("id");
    var myString = id.split("-").pop();

    if (myString == "si_mrp") {
        fetch1 = index.concat("-si_discp");
        fetch2 = index.concat("-si_taxpc");
    } else if (myString == "si_discp") {
        fetch1 = index.concat("-si_mrp");
        fetch2 = index.concat("-si_taxpc");
    } else {
        fetch1 = index.concat("-si_discp");
        fetch2 = index.concat("-si_mrp");
    }

    temp1 = $("#sellitem-"+fetch1+"").val();
    temp2 = $("#sellitem-"+fetch2+"").val();

    if (!isNaN(temp1) && temp1.length != 0) {
        next = temp1;
    }

    if (isNaN(temp2) || temp2.length == 0) {
        previous = temp2;
    }

    current = item.val();
    if (isNaN(current) || current.length == 0) {
        current = 0;
    }

    if (!isNaN(current) && !isNaN(next) && !isNaN(previous)) {
        total = (parseFloat(current) + parseFloat(next) + parseFloat(previous)).toFixed(2);
    }

    udiscField = "sellitem-".concat(index).concat("-si_rate");

    $("#"+udiscField+"").val(total);
}
JS;
$this->registerJs($script, View::POS_END);
/* end getting the rate */
?>

Error The si_mrp field seems to be not adding up

But, When I do the following calculation

total = (parseFloat(10.5) + parseFloat(11.5) + parseFloat(12.5)).toFixed(2);

I get the correct result.

Again I can see, Either si_mrp + si_discp or si_discp + si_taxpc happening but not si_mrp + si_discp + si_taxpc Not sure what is wrong with this code. Please help.


回答1:


Update this :

if (isNaN(temp2) || temp2.length == 0) {
    previous = temp2;
}

to

if(!isNaN(temp2) && temp2.length != 0) {
    previous = temp2;
}


来源:https://stackoverflow.com/questions/41908640/calculate-3-fields-and-display-the-result-in-the-4th-in-yii2-dynamic-form

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!