B
B
baetov2020-12-07 08:32:09
Yii
baetov, 2020-12-07 08:32:09

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(); ?>

code of my controller group action create
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,
                ]);
            }
        }
       
    }

when I click on the plus sign, I want the modal to open,
5fcdbd9ac0509844069209.png
this is what I get
5fcdbddbe326c708810505.png

, and if I change the entry from the article/index page, then everything works
5fcdc03b4557e873930023.png5fcdc043747e3771230069.png5fcdc04ca7c8c615261511.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question