Olá pessoal,
Atualmente tenho um form (criado no yii2) com um campo dropdown de clientes. Quando preciso incluir um novo cliente, isso é feito por meio de uma modal. Para implementar usei esta dica (http://www.yiiframework.com/wiki/806/render-form-in-popup-via-ajax-create-and-update-with-ajax-validation-also-load-any-page-via-ajax-yii-2-0-2-3/).
Esta funcionando. O único problema é que quando clico no botão cadastrar da modal, ele fecha janela modal e da um redirect na pagina, e caso o usuário já tenha preenchido algum outro campo, ele apaga tudo, não está fazendo submit via ajax.
Qualquer ajuda será muito bem vinda!!
Abaixo os códigos:
views\frete\_form
<div class="col-md-2">
<?= $form->field($model['modelida'], '[modelida]cliente_idcliente')->widget(Select2::classname(), [
'data' => ArrayHelper::map(\app\models\Cliente::find()->where('status <> 0')->orderBy('nome')->asArray()->all(), 'idcliente', 'nome'),
'addon' => ['groupOptions' => ['style'=>'width:137%']],
'options' => ['placeholder' => 'Selecione Cliente...', 'id'=>'idclienteida'],
//'groupOptions'=>['style'=>'width:180px'],
'pluginOptions' => [
'allowClear' => true
]
]); ?>
</div>
<div class="col-md-1 text-right">
<div class="form-group field-frete-remetente">
<label class="control-label" for="frete-cliente_idcliente">Inclui</label>
<?= Html::button('+', ['value' => Url::to(['cliente/createfasi']), 'title' => 'Cadastrar Novo Cliente', 'class' => 'showModalButton btn btn-success']); ?>
</div>
</div>
ClienteController
public function actionCreatefasi()
{
$model = new Cliente();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
//return $this->renderAjax('//frete/_formfasi');
//return $this->redirect(['view', 'id' => (string) $model->idcliente]);
return $this->redirect(['frete/createfasi']);
} elseif (Yii::$app->request->isAjax) {
return $this->renderAjax('_form', [
'model' => $model,
]);
} else {
return $this->render('_form', [
'model' => $model
]);
}
}
ajax-modal-popup.js
(function($){
$(document).on('click', '.showModalButton', function () {
if ($('#modal').data('bs.modal').isShown) {
$('#modal').find('#modalContent')
.load($(this).attr('value'));
//dynamiclly set the header for the modal
document.getElementById('modalHeaderTitle').innerHTML = '<h4>' + $(this).attr('title') + '</h4>';
} else {
//if modal isn't open; open it and load content
$('#modal').modal('show')
.find('#modalContent')
.load($(this).attr('value'));
//dynamiclly set the header for the modal
document.getElementById('modalHeaderTitle').innerHTML = '<h4>' + $(this).attr('title') + '</h4>';
}
});
})(jQuery);
views\layouts\main.php
<?php
yii\bootstrap\Modal::begin([
'header' => '<span id="modalHeaderTitle"></span>',
'headerOptions' => ['id' => 'modalHeader'],
'id' => 'modal',
'size' => 'modal-sm',
//keeps from closing modal with esc key or by clicking out of the modal.
// user must click cancel or X to close
'clientOptions' => ['backdrop' => 'static', 'keyboard' => FALSE]
]);
echo '<div id="modalContent"><div style="text-align:center"><img src="'.Url::to('/images/ajax-loader.gif').'"></div></div>';
yii\bootstrap\Modal::end();
?>