从 3 个输入字段以动态形式 yii2 计算 [英] Calculate from 3 inputfield in dynamic form yii2

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

问题描述

在我的动态表单中,我需要计算来自 3 个字段(数量、费率、折扣)的数据并将其传递给 unitdiscount.公式为 - unitdiscount = ((qty*rate*discount)/100) .当我将 (qty*rate) 传递给 value 时,我已经在输入字段数量和速率上设置了 onchange 事件(getValue).当我添加新的 onchange(getUnitdiscount) 时,不会传递计算字段 value.相反,我得到 unitdiscount.另外,不确定如何从 3 个 inputdields 计算.

我在 在 texbox 中传递计算数据,而不是来自动态形式 yii2 的任何模型

我现在的代码看起来像 -

_form

<?= $form->field($modelsProductsales, "[{$i}]rate")->label(false)->textInput(['maxlength' => true,'onchange' =>; 'getValue($(this))', 'onkeyup' => 'getValue($(this))', 'onchange' => 'getUnitdiscount($(this))', 'onkeyup' => 'getUnitdiscount($(this))','placeholder' => 'Rate']) ?>

<div class="col-xs-1 col-sm-1 col-lg-1 nopadding"><?= $form->field($modelsProductsales, "[{$i}]qty")->label(false)->textInput(['maxlength' => true,'onchange' =>; 'getTotal($(this))', 'onkeyup' => 'getTotal($(this))','onchange' => 'getValue($(this))', 'onkeyup' => 'getValue($(this))','onchange' => 'getUnitdiscount($(this))', 'onkeyup' => 'getUnitdiscount($(this))','placeholder' => 'Qty']) ?>

<div class="col-xs-1 col-sm-1 col-lg-1 nopadding"><?= $form->field($modelsProductsales, "[{$i}]free")->label(false)->textInput(['maxlength' => true,'onchange' =>; 'getTotal($(this))', 'onkeyup' => 'getTotal($(this))','placeholder' => '免费']) ?>

<div class="col-xs-1 col-sm-1 col-lg-1 nopadding"><?= $form->field($modelsProductsales, "[{$i}]discount")->label(false)->textInput(['maxlength' => true,'placeholder' =>; '折扣']) ?>

<div class="col-xs-1 col-sm-1 col-lg-1"><input type="text" class="form-control" id="productsales-<?= $i ?>-value">

<input type="text" class="form-control" id="productsales-<?= $i ?>-unitdiscount">

JS 函数

registerJs($script, View::POS_END);/* 结束获取产品值 */?><?php/* 开始获取产品单位折扣 */$script = <<<JS函数 getUnitdiscount(item) {var index = item.attr("id").replace(/[^0-9.]/g, "");var 总计 = 当前 = 下一个 = 0;var id = item.attr("id");var myString = id.split("-").pop();如果(myString ==数量"){fetch = index.concat("-rate");} 别的 {fetch = index.concat("-qty");}temp = $("#productsales-"+fetch+"").val();if(!isNaN(temp) && temp.length != 0) {下一个 = 温度;}当前 = item.val();if(isNaN(current) || current.length == 0) {当前 = 0;}if(!isNaN(current) && !isNaN(next)) {总计 = parseFloat((parseFloat(current) * parseFloat(next))).toFixed(2);}unitdiscountField = "productsales-".concat(index).concat("-unitdiscount");$("#"+unitdiscountField+"").val(total);}JS;$this->registerJs($script, View::POS_END);/* 结束获取产品单位折扣 */?>

更新-我更改了输入字段并将 onchange(getUnitdiscount) 放在 onchange(getUnitdiscount) 前面.现在我至少在文本框 unitdiscountvalue 中都得到了输出.

我尝试了以下 javascript,但没有给出正确的结果.

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

通过这个 javascript 如果我输入 rate = 50.50, qty = 100, discount = 10 它应该给出结果 160.50 但它给出了 70.50.(我采用了一个简单的公式 unitdiscount = rate + qty + discount 来测试我是否正确获取了值,然后我可以将公式更改为复杂的公式.)

解决方案

试试这个:

function getUdisc(item) {var index = item.attr("id").replace(/[^0-9.]/g, "");var 总计 = 当前 = 下一个 = 上一个 = 0;var id = item.attr("id");var myString = id.split("-").pop();如果(myString ==数量"){fetch1 = index.concat("-discount");fetch2 = index.concat("-rate");} else if (myString == "折扣") {fetch1 = index.concat("-qty");fetch2 = index.concat("-rate");} 别的 {fetch1 = index.concat("-discount");fetch2 = index.concat("-qty");}temp1 = $("#productsales-"+fetch1+"").val();temp2 = $("#productsales-"+fetch2+"").val();如果 (!isNaN(temp1) && temp1.length != 0) {下一个 = temp1;}if (isNaN(temp2) || temp2.​​length == 0) {上一个 = temp2;}当前 = item.val();if (isNaN(current) || current.length == 0) {当前 = 0;}if (!isNaN(current) && !isNaN(next) && !isNaN(previous)) {总计 = (parseFloat(current) + parseFloat(next) + parseFloat(previous)).toFixed(2);}udiscField = "productsales-".concat(index).concat("-udisc");$("#"+udiscField+"").val(total);}

In my dynamic form I need to calculate data from from 3 fields(qty,rate,discount) and pass it to unitdiscount. The formula will be - unitdiscount = ((qty*rate*discount)/100) . I already have onchange event(getValue) on inputfield qty and rate as I pass (qty*rate) to value. When I'm adding the new onchange(getUnitdiscount), the calcluted field value is not passed. In stead, I get unitdiscount. Also, not sure how can I calculate from 3 inputdields.

I calculated value with the help of Pass calculated data in a texbox not from any model in dynamic form yii2

My present code looks like -

_form

<div class="col-xs-1 col-sm-1 col-lg-1 nopadding">
                                    <?= $form->field($modelsProductsales, "[{$i}]rate")->label(false)->textInput(['maxlength' => true,'onchange' => 'getValue($(this))', 'onkeyup' => 'getValue($(this))','onchange' => 'getUnitdiscount($(this))', 'onkeyup' => 'getUnitdiscount($(this))','placeholder' => 'Rate']) ?>
                                </div>
                                <div class="col-xs-1 col-sm-1 col-lg-1 nopadding">
                                    <?= $form->field($modelsProductsales, "[{$i}]qty")->label(false)->textInput(['maxlength' => true,'onchange' => 'getTotal($(this))', 'onkeyup' => 'getTotal($(this))','onchange' => 'getValue($(this))', 'onkeyup' => 'getValue($(this))','onchange' => 'getUnitdiscount($(this))', 'onkeyup' => 'getUnitdiscount($(this))','placeholder' => 'Qty']) ?>
                                </div>
                                <div class="col-xs-1 col-sm-1 col-lg-1 nopadding">
                                    <?= $form->field($modelsProductsales, "[{$i}]free")->label(false)->textInput(['maxlength' => true,'onchange' => 'getTotal($(this))', 'onkeyup' => 'getTotal($(this))','placeholder' => 'Free']) ?>
                                </div>
                                <div class="col-xs-1 col-sm-1 col-lg-1 nopadding">
                                    <?= $form->field($modelsProductsales, "[{$i}]discount")->label(false)->textInput(['maxlength' => true,'placeholder' => 'Discount']) ?>
                                </div> 
<div class="col-xs-1 col-sm-1 col-lg-1 ">
                                    <input type="text" class="form-control" id="productsales-<?= $i ?>-value">
                                </div> 
<input type="text" class="form-control" id="productsales-<?= $i ?>-unitdiscount">

JS Function

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

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

    if(myString == "qty") {
        fetch = index.concat("-rate");
    } else {
        fetch = index.concat("-qty");
    }

    temp = $("#productsales-"+fetch+"").val();

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

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

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

    valueField = "productsales-".concat(index).concat("-value");

    $("#"+valueField+"").val(total);
}
JS;
$this->registerJs($script, View::POS_END);
/* end getting the product value */
?>
<?php
/* start getting the product Unit discount */
$script = <<< JS
function getUnitdiscount(item) {
    var index  = item.attr("id").replace(/[^0-9.]/g, "");
    var total = current = next = 0;

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

    if(myString == "qty") {
        fetch = index.concat("-rate");
    } else {
        fetch = index.concat("-qty");
    }

    temp = $("#productsales-"+fetch+"").val();

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

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

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

    unitdiscountField = "productsales-".concat(index).concat("-unitdiscount");

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

Update - I've changed the inptfields and put onchange(getUnitdiscount) in front of onchange(getUnitdiscount). Now I'm atleast getting ouput in both textbox unitdiscount and value.

I've tried with the following javascript which not giving correct result.

<?php
/* start getting the total udisc */
$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 == "qty") {
        fetch2 = index.concat("-discount");
        fetch1 = index.concat("-rate");
    } else if(myString == "discount") {
        fetch3 = index.concat("-qty");
        fetch1 = index.concat("-rate");
    } else {
        fetch2 = index.concat("-discount");
        fetch3 = index.concat("-qty");
    }

    temp = $("#productsales-"+fetch1+"").val();

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

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

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

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

    udiscField = "productsales-".concat(index).concat("-udisc");

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

By this javascript if I put rate = 50.50, qty = 100, discount = 10 it should give result 160.50 but it's giving 70.50. (I've taken a simple formula as unitdiscount = rate + qty + discount to test if I am getting the values correctly, then I can change the formula to a complex one.)

解决方案

Try this way :

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 == "qty") {
        fetch1 = index.concat("-discount");
        fetch2 = index.concat("-rate");
    } else if (myString == "discount") {
        fetch1 = index.concat("-qty");
        fetch2 = index.concat("-rate");
    } else {
        fetch1 = index.concat("-discount");
        fetch2 = index.concat("-qty");
    }

    temp1 = $("#productsales-"+fetch1+"").val();
    temp2 = $("#productsales-"+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 = "productsales-".concat(index).concat("-udisc");

    $("#"+udiscField+"").val(total);
}

这篇关于从 3 个输入字段以动态形式 yii2 计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆