Pass calculated data in a texbox not from any model in dynamic form yii2

懵懂的女人 提交于 2019-12-12 04:30:31

问题


I'm trying to get the product of two textboxes (qty, rate) from model productsales to a 3rd textbox (value) withing dynamic form yii2. It is exactly same as Yii2-dynamicforms and javascript, except, the 3rd textbox doesn't belong to any model. _form

<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))','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 ">
                                    <input type="text" class="form-control" id="value">
                                </div>

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 = parseInt(current) * parseInt(next);
    }

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

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

This is not giving any output. Please let me know if any additional piece of code needed.


回答1:


_form.php

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

JS

if(!isNaN(current) && !isNaN(next)) {
    total = parseInt(current) * parseInt(next);
}
valueField = "productsales-".concat(index).concat("-value");

$("#"+valueField+"").val(total);



回答2:


<?= $form->field($model, 'quantity')->textInput(['id' => "quantity"]) ?>

<?= $form->field($model, 'subtotal')->textInput(['id' => "price"]) ?>

<input type="text" id="total">

<?php
$script = <<<EOD
$(function() {
     $('#quantity').keyup(function() {
        updateTotal();
      });
    $('#price').keyup(function() {
       updateTotal();
     });   

    var updateTotal = function() {

    var quantity = parseInt($('#quantity').val());
    var price = parseInt($('#price').val());  
    var total = quantity * price;   

    $('#total').val(total);

    };  
 });
EOD;
$this->registerJs($script);
?>


来源:https://stackoverflow.com/questions/40557301/pass-calculated-data-in-a-texbox-not-from-any-model-in-dynamic-form-yii2

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