Calculate from 3 inputfield in dynamic form yii2 -


in dynamic form need calculate data from 3 fields(qty,rate,discount) , pass unitdiscount. formula - unitdiscount = ((qty*rate*discount)/100) . have onchange event(getvalue) on inputfield qty , rate pass (qty*rate) value. when i'm adding new onchange(getunitdiscount), calcluted field value not passed. in stead, unitdiscount. also, not sure how can calculate 3 inputdields.

i calculated value of pass calculated data in texbox not model in dynamic form yii2

my present code looks -

_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 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 product value */ ?> <?php /* start getting 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 product unit discount */ ?> 

update - i've changed inptfields , put onchange(getunitdiscount) in front of onchange(getunitdiscount). i'm atleast getting ouput in both textbox unitdiscount , value.

i've tried following javascript not giving correct result.

<?php /* start getting 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 total udisc */ ?> 

by javascript if put rate = 50.50, qty = 100, discount = 10 should give result 160.50 it's giving 70.50. (i've taken simple formula unitdiscount = rate + qty + discount test if getting values correctly, can change formula complex one.)

try 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); } 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -