1

I have some dynamic forms working in Yii2 and it works fine for me, but I need to add a little function to get cost price and sale price. It is work in the first dynamic form but the other one it dose not.

My Form code :

<div class="row">
    <div class="col-sm-12">
         <?= $form->field($modelPoItem, "[{$i}]po_item_no")->dropDownList(
                            arrayhelper::map(Itemes::find()->all(),'itemes_id','item_name'),
                            ['prompt'=>'select Item', 'onchange' => 'getProduct($(this))', 'onkeyup' => 'getProduct($(this))']
                            ) ?>
    </div>  
</div>
<div class="row">
     <div class="col-sm-4">
          <?= $form->field($modelPoItem, "[{$i}]quantity")->textInput(['maxlength' => 128 ])  ?>
     </div>
     <div class="col-sm-4">
        <?= $form->field($modelPoItem, "[{$i}]cost_price")->textInput(['maxlength' => 128]) ?>
      </div>
      <div class="col-sm-4">
          <?= $form->field($modelPoItem, "[{$i}]sale_price")->textInput(['maxlength' => 128]) ?>
      </div>
</div>

And here is my JS code :

function getProduct(item) {
 var index  = item.attr("id").replace(/[^0-9.]/g, "");
 var itemes_id =$('#poitem-'+index+'-po_item_no').val();
 $.get('index.php?r=itemes/price',{ itemes_id : itemes_id },function(data)
  {
   var data=$.parseJSON(data);
   alert('#poitem-'+index+'-sale_price'); // i used it just to check if the id Is correct 
   $('#poitem-'+index+'-sale_price').attr('value',data.sale_price);
   $('#poitem-'+index+'-cost_price').attr('value',data.cost_price);
  });
}

Update - 1

Update - 2

Update - 3

18
  • it is not very clear what is the problem. do you have error messages of some sort? "it does not work" is not very helpful Commented Nov 7, 2016 at 8:45
  • @Elzo Valugi i updated my question Sir ! Commented Nov 7, 2016 at 8:51
  • Isn't this id: $('#poitem-0-po_item_no') for only 1st form that you generate? You should have a common class in all forms in order to run your javascript code. Commented Nov 7, 2016 at 8:53
  • @Chinmay yes it is . Commented Nov 7, 2016 at 8:58
  • You can use something like this Commented Nov 7, 2016 at 9:03

1 Answer 1

1

Try this:

function getProduct(item) {
   var index  = item.attr("id").replace(/[^0-9.]/g, "");
   var itemes_id =$('#poitem-'+index+'-po_item_no').val();
   $.get('index.php?r=itemes/price',{ itemes_id : itemes_id },function(data)
   {
      var data=$.parseJSON(data);

     $('#poitem-'+index+'-sale_price').val(data.sale_price);
     $('#poitem-'+index+'-cost_price').val(data.cost_price);
   });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.