Tushar Walzade Tushar Walzade - 2 months ago 62
jQuery Question

Custom fields in Yii2 dynamic form

I'm using wbraganca dynamic form in Yii2-advanced-app. Where I want to take a custom field. But I don't understand how to handle it with onchange. Because the id's of all fields are changing dynamically as we click on 'add' button.
Refer this image to clearly understand my problem

I want to change only the respected 'available qty' on changing the corresponding 'SKU'. That is if sku with id sku0 is selected, then it's available qty i.e. avlqty0 should be changed(...Sku1--->avlqty1 & so on...). But I can't get the id because it changes dynamically.

Here is my code -

<div class="col-lg-2">
<?= $form->field($model, "[{$i}]SKU")->dropDownList(ArrayHelper::map(CompItems::find()->where(['id' => 0])->all(),'id','SKU'),['prompt' => 'Select SKU',
'onchange' => '
$("#avlqty").val(100);']) ?>
</div>
<div class="col-lg-3">
<div class="form-group" style="margin-bottom: 10px;">
<label for="avlqty">Available Qty:</label>
<input type="text" class="form-control" id="avlqty" value="" readonly="true">
</div>
</div>

Ish Ish
Answer

Give class to the sku dropdown. Let say it is sku_class and give the id like sku_id (ski_0) and avlqty_0

$(document).on('change', '.sku_class', function() {
    ele_id = $(this).attr('id');
    ele_arr = ele_id.split("_");
    id = ele_arr.pop();  // this is your id
})
Comments