我正在使用 yii2框架,我尝试使用 wbraganca 中的动态表单,但添加按钮无法正常工作即使我将 formId 放在 ActiveForm :: begin 中,也可以帮我解决这个问题。
检查代码 _form in view:
<div class="compra-form">
<?php $form = ActiveForm::begin(['id' => 'dynamic-form']); ?>
<?= $form->field($model, 'id_utilizador')->textInput() ?>
<?= $form->field($model, 'id_fornecedor')->dropDownList(
ArrayHelper::map(Fornecedor::find()->all(), 'id_fornecedor', 'nome'),
['prompt'=>'Seleciona um Fornecedor']
); ?>
<?= $form->field($model, 'id_armazem')->dropDownList(
ArrayHelper::map(Armazem::find()->all(), 'id_armazem', 'endereco'),
['prompt'=>'Seleciona um Armazem']
); ?>
<?= $form->field($model, 'data_compra')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'obs')->textarea(['rows' => 6]) ?>
<div class="panel panel-default">
<div class="panel-heading"><h4><i class="fa fa-th-large"></i> Itens da Compra</h4></div>
<div class="panel-body">
<?php DynamicFormWidget::begin([
'widgetContainer' => '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' => $modelsItem[0],
'formId' => 'dynamic-form',
'formFields' => [
'id_item',
'unidade',
],
]); ?>
<div class="container-items">
<?php foreach ($modelsItem as $i => $modelItem): ?>
<div class="item panel panel-default">
<div class="panel-heading">
<h3 class="panel-title pull-left">Item</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">
<div class="col-sm-6">
<?= $form->field($modelItem, '[{$i}]id_item')->dropDownList(
ArrayHelper::map(Item::find()->all(), 'id_item', 'nome'),
['prompt'=>'Seleciona um item']
); ?>
</div>
<div class="col-sm-6">
<?= $form->field($modelItem, "[{$i}]unidade")->textInput(['maxlength' => true]) ?>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php DynamicFormWidget::end(); ?>
</div>
</div>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>