Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating yii2 Select2 ID in yii2-dynamicform #306

Open
Spywavenoah opened this issue Jul 27, 2020 · 0 comments
Open

Updating yii2 Select2 ID in yii2-dynamicform #306

Spywavenoah opened this issue Jul 27, 2020 · 0 comments

Comments

@Spywavenoah
Copy link

Spywavenoah commented Jul 27, 2020

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', ], ]); ?>
        <div class="container-items"><!-- widgetContainer -->
        <?php $r=0; foreach ($BranchSales as $index => $SalesEntries): $r++;?>

            <div class="item card card-warning"><!-- widgetBody -->
                <div class="card-header">
                    <h3 class="card-title pull-left">Attendant <?= ($index + 1) ?></h3>

                    <div class="float-right">
                        <button type="button" class="add-item btn btn-success btn-xs"><i class="fa fa-plus"></i></button>
                        <button type="button" class="remove-item btn btn-danger btn-xs"><i class="fa fa-minus"></i></button>
                    </div>
                    <div class="clearfix"></div>
                </div>
                <div class="card-body">
                    <?php
                        // necessary for update action.
                        if (! $SalesEntries->isNewRecord) {
                            echo Html::activeHiddenInput($SalesEntries, "[{$index}]id");
                        }
                    ?>
                    <div class="row">


        <div class="col-sm-1">
        <?= $form->field($SalesEntries, "[{$index}]pump")->textInput(['maxlength' => true]) ?>
        </div>

        <div class="col-sm-4">
                            <?= $form->field($SalesEntries, "[{$index}]attendant")->widget(Select2::classname(), [
            'data' =>$data, 
                // ['prompt'=>'-Choose a Product-'],
                'language' => 'en',
                 'options' => [
        'prompt' => 'Select Pump Attendant']
]); ?>
                        </div>


                        <div class="col-sm-4">
                            <?= $form->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
                      
                    });
                ']
]); 

?> </div>
field($SalesEntries, "[{$index}]u_price")->textInput(['maxlength' => true]) ?>
                        <div class="col-sm-1">
                            <?= $form->field($SalesEntries, "[{$index}]qty")->textInput(['maxlength' => true,'onkeyup' => 'totales($(this))']) ?>
                        </div>

 </div><!-- .row -->
                </div>
            </div>
        <?php endforeach; ?>
        </div>
        <?php DynamicFormWidget::end(); ?>
    </div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant