wbraganca / yii2-dynamicform

It is widget to yii2 framework to clone form elements in a nested manner, maintaining accessibility.
Other
436 stars 438 forks source link

Updating yii2 Select2 ID in yii2-dynamicform #306

Open Spywavenoah opened 4 years ago

Spywavenoah commented 4 years ago

Hello Guys, I have a Sales form that was created using yii2-dynamicform which is forming fine. but I want to be able to pass the unit price of a product selected to corresponding input field that is also added dynamically, how do i add the id of input element to select2 dynamically base on the numbers of record? from what i ahve done so far, it keeps updating the unit price of the first row only

'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_] 'widgetBody' => '.container-items', // required: css class selector 'widgetItem' => '.item', // required: css class 'limit' => 4, // the maximum times, an element can be cloned (default 999) 'min' => 1, // 0 or 1 (default 1) 'insertButton' => '.add-item', // css class 'deleteButton' => '.remove-item', // css class 'model' => $BranchSales[0], 'formId' => 'dynamic-form', 'formFields' => [ 'pump', 'product_material_name', 'attendant', 'amount', 'qty', ], ]); ?>
$SalesEntries): $r++;?>

Attendant

isNewRecord) { echo Html::activeHiddenInput($SalesEntries, "[{$index}]id"); } ?>
field($SalesEntries, "[{$index}]pump")->textInput(['maxlength' => true]) ?>
field($SalesEntries, "[{$index}]attendant")->widget(Select2::classname(), [ 'data' =>$data, // ['prompt'=>'-Choose a Product-'], 'language' => 'en', 'options' => [ 'prompt' => 'Select Pump Attendant'] ]); ?>
field($SalesEntries, "[{$index}]product_material_name")->widget(Select2::classname(), [ 'data' =>ArrayHelper::map(GoodsProducts::find()->where(['branch_code'=>Yii::$app->user->identity->branch_code])->asArray()->all(), 'product_material_code', 'product_material_name'), // ['prompt'=>'-Choose a Product-'], 'language' => 'en', 'id' => '[{$index}]product_material_name', 'options' => [ 'prompt' => 'Select Product', 'onchange'=>' $.post( "'.urldecode(Yii::$app->urlManager->createUrl('branch-sales/list&id=')).'"+$(this).val(), function( data ) { $( "Input#branchsales-'.$index.'-u_price" ).val(data); //here is where am having issues, it keeps updating the first one only }); '] ]); ?>
field($SalesEntries, "[{$index}]u_price")->textInput(['maxlength' => true]) ?>
field($SalesEntries, "[{$index}]qty")->textInput(['maxlength' => true,'onkeyup' => 'totales($(this))']) ?>