如何计算从 yii2 动态表单中的四个字段中获取输入 [英] How to calculate taking input from four fields in yii2 dynamic form

查看:30
本文介绍了如何计算从 yii2 动态表单中的四个字段中获取输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有五个字段-率,数量.折扣、cgst_percent、cgst_amount.我想计算 cgst_amount.公式应该是 -

cgst_amount = ((rate*qty - (rate*qty*discount)/100)*cgst_percent)/100

从简单的步骤开始,我尝试了 cgst_amount = rate*qty

javascript 部分如下 -

registerJs($script, View::POS_END);/* 结束获取 cgst */?>

当我输入速率和数量时,我将它们相乘作为 cgst_amount 文本框中的输出.到目前为止没有问题.只要我输入任何折扣,相同的文本就会以 cgst_amount 的形式写入作为输出.

我不太确定 javascript 部分.
它是 -

实际结果应该是 - 5.43.相反,我得到 -0.00

解决方案

你与字段和变量结合在一起,我已经稍微更新了一个脚本:

function getGst(item) {var index = item.attr("id").replace(/[^0-9.]/g, "");var qtyvar = ratevar = discvar = cgstpercentvar = cgstvar = 0;var id = item.attr("id");var myString = id.split("-").pop();数量 = index.concat("-qty");rate = index.concat("-rate");折扣 = index.concat("-discount");cgstRate = index.concat("-cgst_rate");temp1 = $("#productsales-"+quantity+"").val();temp2 = $("#productsales-"+rate+"").val();temp3 = $("#productsales-"+discount+"").val();temp4 = $("#productsales-"+cgstRate+"").val();if (isNaN(temp1) || temp1.length != 0) {qtyvar = temp1;}如果 (!isNaN(temp2) && temp2.​​length != 0) {ratevar = temp2;}if (isNaN(temp3) || temp3.length != 0) {discvar = temp3;}if (isNaN(temp4) || temp4.length != 0) {cgstpercentvar = temp4;}如果 (!isNaN(qtyvar) && !isNaN(ratevar) && !isNaN(discvar) && !isNaN(cgstpercentvar)) {cgstvar = (((((parseFloat(ratevar) * parseFloat(qtyvar)) - (parseFloat(ratevar) * parseFloat(qtyvar) * parseFloat(discvar))/100) * parseFloat(cgstpercentvar))/100).toFixed(2);}cgstField = "productsales-".concat(index).concat("-cgst_amount");$("#"+cgstField+"").val(cgstvar);}

I've five fields- rate, qty. discount, cgst_percent, cgst_amount. I want to calculate cgst_amount. The formula should be -

cgst_amount = ((rate*qty - (rate*qty*discount)/100)*cgst_percent)/100

To Start with simple steps, I tried cgst_amount = rate*qty

The javascript part is as below -

<?php
/* start getting the cgst */
$script = <<< JS
function getGst(item) {
    var index  = item.attr("id").replace(/[^0-9.]/g, "");
    var qtyvar = ratevar = discvar = cgstpercentvar = cgstvar = 0;

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

    if (myString == "rate") {
        fetch1 = index.concat("-qty");
        fetch2 = index.concat("-discount");
        fetch3 = index.concat("-cgst_rate");
    } else if (myString == "qty") {
        fetch1 = index.concat("-rate");
        fetch2 = index.concat("-discount");
        fetch3 = index.concat("-cgst_rate");
    } else if (myString == "discount"){
        fetch1 = index.concat("-qty");
        fetch2 = index.concat("-rate");
        fetch3 = index.concat("-cgst_rate");
    } else {
        fetch1 = index.concat("-qty");
        fetch2 = index.concat("-rate");
        fetch3 = index.concat("-discount");
    }

    temp1 = $("#productsales-"+fetch1+"").val();
    temp2 = $("#productsales-"+fetch2+"").val();
    temp3 = $("#productsales-"+fetch3+"").val();
    //alert (temp2);

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

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

    if (isNaN(temp3) || temp3.length != 0) {
        cgstpercentvar = temp3;
    }

    qtyvar = item.val();
    if (isNaN(qtyvar) || qtyvar.length == 0) {
        qtyvar = 0;
    }
    //alert (qtyvar);
    if (!isNaN(qtyvar) && !isNaN(ratevar) && !isNaN(discvar) && !isNaN(cgstpercentvar)) {
        cgstvar = (parseFloat(qtyvar) * parseFloat(ratevar)).toFixed(2);

    }

    cgstField = "productsales-".concat(index).concat("-cgst_amount");

    $("#"+cgstField+"").val(cgstvar);

}
JS;
$this->registerJs($script, View::POS_END);
/* end getting the cgst */
?>

Whn I key in rate and qty I get multiply of them as output in cgst_amount textbox. No problem so far. As soon as I key in anything in discount,the same texts are getting written in cgst_amount as output.

I'm not quite sure about the javascript part.
It is an extension of - Calculate from 3 inputfield in dynamic form yii2 and Calculate 3 fields and display the result in the 4th in yii2 dynamic form

If I work on the full formula, the javascript calculation part becomes - cgstvar = ((((parseFloat(ratevar) * parseFloat(qtyvar)) - (parseFloat(ratevar) * parseFloat(qtyvar) * parseFloat(discvar))/100) * parseFloat(cgstpercentvar))/100).toFixed(2);

And the example is as below image -

The actual result should have been - 5.43. Instead I'm getting -0.00

解决方案

You meshed up with fields and variables, i have updated a script a bit :

function getGst(item) {
    var index  = item.attr("id").replace(/[^0-9.]/g, "");
    var qtyvar = ratevar = discvar = cgstpercentvar = cgstvar = 0;

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

    quantity = index.concat("-qty");
    rate = index.concat("-rate");
    discount = index.concat("-discount");
    cgstRate = index.concat("-cgst_rate");

    temp1 = $("#productsales-"+quantity+"").val();
    temp2 = $("#productsales-"+rate+"").val();
    temp3 = $("#productsales-"+discount+"").val();
    temp4 = $("#productsales-"+cgstRate+"").val();

    if (isNaN(temp1) || temp1.length != 0) {
        qtyvar = temp1;
    }

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

    if (isNaN(temp3) || temp3.length != 0) {
        discvar = temp3;
    }

    if (isNaN(temp4) || temp4.length != 0) {
        cgstpercentvar = temp4;
    }

    if (!isNaN(qtyvar) && !isNaN(ratevar) && !isNaN(discvar) && !isNaN(cgstpercentvar)) {
        cgstvar = ((((parseFloat(ratevar) * parseFloat(qtyvar)) - (parseFloat(ratevar) * parseFloat(qtyvar) * parseFloat(discvar))/100) * parseFloat(cgstpercentvar))/100).toFixed(2);
    }

    cgstField = "productsales-".concat(index).concat("-cgst_amount");

    $("#"+cgstField+"").val(cgstvar);

}

这篇关于如何计算从 yii2 动态表单中的四个字段中获取输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆