Answer the question
In order to leave comments, you need to log in
How to make a modal window open when clicking on the actionCreate link?
Hello everyone, please tell me, I have an article class, when creating an article (article) in the controller in actionCreate, I create an instance of the class redirect to update, now in the article form there is an associated field with the Group class, where by group_id in the drop-down list I select from the list of groups, and now I want to add a button so that when clicked, a modal window opens and I can immediately add a group without jumping through the pages, but the problem is that the modal window does not open, and instead I get some kind of hieroglyphs and something similar to gruoup / create (that is, there is an input and buttons to save and close)
the code of my article/_form form
<?php
use app\models\Group;
use app\models\Subgroup;
use kartik\select2\Select2;
use yii\bootstrap\Modal;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\widgets\Pjax;
/* @var $this yii\web\View */
/* @var $model app\models\Article */
/* @var $form yii\widgets\ActiveForm */
?>
<?php Pjax::begin(['id' => 'article-pjax-container','enablePushState' => false]) ?>
<div class="article-form">
<?php $form = ActiveForm::begin(); ?>
<div class="row"style="display: flex; align-items: center;">
<div class="col-md-11">
<?php
try {
echo $form->field($model, 'group_id')->widget(Select2::class, [
'data' => (new Group)->getList(),
'options' => [
'id' => 'group',
'prompt' => 'Выберите группу'
]
])->label('Наименование группы');
} catch (Exception $e) {
echo $e->getMessage();
} ?>
</div>
<div class="col-md-1">
<?= Html::a('<span class="fa fa-plus"></span>', [
'/group/create',
'caller' => '/article/create',
// 'pjaxReloadContainer' => '#article-pjax-container',
], [
'role'=>'modal-remote',
'title'=> 'Добавить группу',
'class' => 'btn btn-default btn-block',
]);?>
</div>
</div>
<?= $form->field($model, 'subgroup_id')->widget(Select2::classname(), [
'data' => ArrayHelper::map(Subgroup::find()->andFilterWhere(['company_id' => Yii::$app->user->identity->company_id])->all(), 'id', 'name'),
'language' => 'ru',
'options' => ['placeholder' => 'выберите тип ...'],
'pluginOptions' => [
'allowClear' => true
],
]);?>
<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'comment')->textarea(['rows' => 6]) ?>
<?php if (!Yii::$app->request->isAjax){ ?>
<div class="form-group">
<?= Html::submitButton('Сохранить',
['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php } ?>
<?php ActiveForm::end(); ?>
</div>
<?php Pjax::end(); ?>
<?php Modal::begin([
"id" => "ajaxCrudModal",
"footer" => "",// always need it for jquery plugin
]) ?>
<?php Modal::end(); ?>
public function actionCreate()
{
$request = Yii::$app->request;
$model = new Group();
$caller = $request->get('caller');
if($request->isAjax){
/*
* Process for ajax request
*/
Yii::$app->response->format = Response::FORMAT_JSON;
if($request->isGet){
return [
'title'=> "Добавить Группу",
'content'=>$this->renderAjax('create', [
'model' => $model,
]),
'footer'=> Html::button('Закрыть',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
Html::button('Добавить',['class'=>'btn btn-primary','type'=>"submit"])
];
}else {
if ($model->load($request->post()) && $model->save()) {
return [
'forceReload' => $caller ? '': '#crud-datatable-pjax',
'size' => 'md',
'title' => "Создание новогй группы",
'content' => '<span class="text-success">Группа добавлена</span>',
'footer' => Html::button('Закрыть',
['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
($caller != null ?
Html::a('Вернуться ', [$caller],
['class' => 'btn btn-primary']) :
Html::a('Создать ещё', ['create'],
['class' => 'btn btn-primary', 'role' => 'modal-remote']))
];
} else {
return [
'title' => "Добавить Группу",
'content' => $this->renderAjax('create', [
'model' => $model,
]),
'footer' => Html::button('Закрыть', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
Html::button('Добавить', ['class' => 'btn btn-primary', 'type' => "submit"])
];
}
}
}else{
/*
* Process for non-ajax request
*/
if ($model->load($request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question