MULTIPLE DYNAMIC FORMS IN ONE VIEW

Source: http://yii-02.blogspot.com/2015/05/dynamic-forms-questions-and-answers-how.html

I have been getting few emails regarding dynamic forms so this post is going to be regarding that.

This answer is how to have two dynamic forms in one view


<div class="panel panel-default">
        <div class="panel-heading"><h4><i class="glyphicon glyphicon-th-list"></i> List Acara</h4></div>
        <div class="panel-body">
             <?php DynamicFormWidget::begin([
                'widgetContainer' => 'dynamicform_wrapper',
                'widgetBody' => '.container-items',
                'widgetItem' => '.item',
                'limit' => 10,
                'min' => 1,
                'insertButton' => '.add-item',
                'deleteButton' => '.remove-item',
                'model' => $modelmeliput[0],
                'formId' => 'dynamic-form',
                'formFields' => [
                    'id_acara',             
                ],
            ]); ?>
            <div class="container-items">
            <?php foreach ($modelmeliput as $i => $liput): ?>
                <div class="item panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title pull-left"><i class="glyphicon glyphicon-film"></i> Acara yang diliput</h3>
                        <div class="pull-right">
                            <button type="button" class="add-item btn btn-success btn-xs"><i class="glyphicon glyphicon-plus"></i></button>
                            <button type="button" class="remove-item btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button>
                        </div>
                        <div class="clearfix"></div>
                    </div>
                    <div class="panel-body">
                        <?php
                            if (! $liput->isNewRecord) {
                                echo Html::activeHiddenInput($liput, "[{$i}]id");
                            }
                        ?>
                      
                        <div class="row">
                             <div class="col-sm-6">
                                <?= $form->field($liput, "[{$i}]id_acara")->dropDownList(ArrayHelper::map(TblAcara::find()->all(),'id','nm_acara'),
                                        ['prompt'=>'Pilih nama acara ...']
                                       
                                     );?>
                            </div>
                        </div>
                       
                    </div>
                </div>
            <?php endforeach; ?>
            </div>
            <?php DynamicFormWidget::end(); ?>
        </div>
    </div>
   
   
    
    <div class="panel2 panel-default">
        <div class="panel-heading"><h4><i class="glyphicon glyphicon-th-list"></i> List Peminjaman</h4></div>
        <div class="panel-body">
             <?php DynamicFormWidget::begin([
                'widgetContainer' => 'dynamicform_wrapper2', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
                'widgetBody' => '.container-items2',
                'widgetItem' => '.item2',
                'limit' => 10,
                'min' => 1,
                'insertButton' => '.add-item2',
                'deleteButton' => '.remove-item2',
                'model' => $modeldetail[0],
                'formId' => 'dynamic-form',
                'formFields' => [
                    'id_alat',
                    'banyak_alat',
                  
                ],
            ]); ?>
            <div class="container-items2">


            <?php foreach ($modeldetail as $i => $pinjam): ?>
                <div class="item2 panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title pull-left"><i class="glyphicon glyphicon-wrench"></i> Alat Yang Dipinjam</h3>
                        <div class="pull-right">
                            <button type="button" class="add-item2 btn btn-success btn-xs"><i class="glyphicon glyphicon-plus"></i></button>
                            <button type="button" class="remove-item2 btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button>
                        </div>
                        <div class="clearfix"></div>
                    </div>
                    <div class="panel-body">
                        <?php
                            if (! $pinjam->isNewRecord) {
                                echo Html::activeHiddenInput($pinjam, "[{$i}]id");
                            }
                        ?>
                      
                        <div class="row">
                             <div class="col-sm-6">
                                <?= $form->field($pinjam, "[{$i}]id_alat")->dropDownList(ArrayHelper::map(TblAlat::find()->where('kondisi =:kondisi and status=:status',[':kondisi'=>'Baik', ':status'=>'Kembali'])->all(),'id','nm_alat'),
                                        ['prompt'=>'Pilih nama alat ...']
                                       
                                     );?>
                            </div>
                            <div class="col-sm-6">
                                <?= $form->field($pinjam, "[{$i}]banyak_alat")->textInput(['maxlength' => true]) ?>
                            </div>
                        </div>
                       
                    </div>
                </div>
            <?php endforeach; ?>
            </div>
            <?php DynamicFormWidget::end(); ?>
        </div>
    </div>

As you can see we have changed the item, add-item, remove-item and the container-item classes. The code was provided by Firman Yuspriyadi



Post a Comment

0 Comments