Hello again,
I was trying to call a modal popup from another modal popup but when i close the second modal i can’t seem to fallback into the original modal window.
Here is the scenario:
I have a simple gridview index page which has an add button which allows to add a new model (which is displayed in the gridview). This add btn calls a bootstrap modal window with the fields required to add a new modal. However this modal has one attribute which is a entered via a dropdown select input field. This dropdown is populated from the database. Here is where the second modal window comes in; I want to be able to add another option to the database which would be reflected in the dropdown. I managed to call a second modal from the first modal but when I submit I’m not sure how to redirect back to the first modal (preferably with the fields that have already been filled as they were).
My workflow is as follows:
In my index.php I have the following btn
<?=
Html::button(Yii::t('app','test'),[
'value' => Yii::$app->urlManager->createUrl('/users/test'),
'class' => 'btn btn-success',
'id' => 'test-modal-btn'
]);
?>
Here is the code for the Modal widget
<?php
Modal::begin([
'header' => "<h4>Modal in a modal</h4>",
'id' => 'test-modal',
'size' => 'modal-lg'
]);
echo "<div id='test-modal-content'></div>";
Modal::end();
?>
to capture the click action and show the modal window i have the following js code added from an external js file using the $this->registerJsFile(…) function
$(function () {
$('#test-modal-btn').click(function () {
$('#test-modal')
.modal('show')
.find('#test-modal-content')
.load($(this).attr('value'));
});
});
so far all good, in my UsersController I have the following action
public function actionTest(){
$model = new TcUser();
if($model->load(Yii::$app->request->post()) && $model->save()){
return $this->redirect('index');
}
return $this->renderAjax('test',[
'model' => $model
]);
}
with this my first modal window works just fine.
Following these same steps in my test.php view file I added a btn and modal widget with
<?=
Html::button('add',[
'value' => Yii::$app->urlManager->createUrl('/gender/test'),
'class' => 'btn btn-primary',
'id' => 'second-modal-btn'
]);
?>
<?php
Modal::begin([
'header' => "<h4>Second Modal</h4>",
'id' => "second-modal",
'size' => 'modal-lg'
]);
echo "<div id='second-modal-content'></div>";
Modal::end();
?>
the dropdown field is generated with
<?=
$form->field($model, 'gender_id')
->dropDownList(
ArrayHelper::map(Gender::find()->all(),'id','name'),
[
'prompt' => Yii::t('app','Select gender')
]
)
->label(Yii::t('app','Gender'))
?>
similarly a js file to handle the click action on the btn
$(function () {
$('#second-modal-btn').click(function () {
$('#second-modal')
.modal('show')
.find('#second-modal-content')
.load($(this).attr('value'));
});
});
And an action in the GenderController to handle the request
public function actionTest(){
$model = new Gender();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['/users/add-user']); //<-----What should i do here???
} else {
return $this->renderAjax('create', [
'model' => $model,
]);
}
}
here is where i’m stuck… Once i have completed the $model->save() how can i fallback into the original modal popup? Is it possible to retain the fields fields which were already inserted before the modal was called and have the new option in the dropdown field?
Hope I explained myself correctly and thanks for your time and input in advance