Johny
Johny

Reputation: 531

Yii2, Javascript dont work only inside modal

I am using modal bootstrap for my crud forms and I am also using the yii2-ajaxcrud extension, which extends the gii generator for a single page crud.

Everything is workimng correctly, saving the models, validation, etc ... However, I'm trying to use a javascript plugin: (sticky-kit from Leafo) to stick my actions buttons at the top. Happens that when I access my 'create' view in modal the following error occurs:

"Uncaught TypeError: $ (...) stick_in_parent is not a function."

From the index view I access the 'create' view. When the modal opens I see in the chrome inspector the injection of jquery and the stick plugin. Moreover, with the ajax crud extension I can also access the views generated by direct URL , and thus the stick plugin works. Only accessing the view in modal is giving the error.

What could be? Below is my main related code.

//namespace app\modules\crm\controllers
 public function actionCreate()
    {
        $request = Yii::$app->request;
        $model = new Classes();  

        if($request->isAjax){
            /*
            *   Process for ajax request
            */
            Yii::$app->response->format = Response::FORMAT_JSON;
            if($request->isGet){
                return [
                    'title'=> "Create new Classes",
                    'content'=>$this->renderAjax('create', [
                        'model' => $model,
                    ]),
                    'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
                                Html::button('Save',['class'=>'btn btn-primary','type'=>"submit"])

                ];         
            }else if($model->load($request->post()) && $model->save()){
                return [
                    'forceReload'=>$this->htmlTableContainerId ,
                    'title'=> "Create new Classes",
                    'content'=>'<span class="text-success">Create Classes success</span>',
                    'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
                            Html::a('Create More',['create'],['class'=>'btn btn-primary','role'=>'modal-remote'])

                ];         
            }else{           
                return [
                    'title'=> "Create new Classes",
                    'content'=>$this->renderAjax('create', [
                        'model' => $model,
                    ]),
                    'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
                                Html::button('Save',['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,
                ]);
            }
        }

    }

The view

// create view
 <div class="panel-body" style="height: 600px">
    <div class="classes-create">
            <?= $this->render('_form', [
                'model' => $model,
            ]) ?>
    </div>

</div>

        $this->registerJsFile('http://leafo.net/sticky-kit/src/sticky-kit.js', ['depends' => [\yii\web\JqueryAsset::className()]]);

        $this->registerJs("$('.panel-heading').stick_in_parent();");

EDIT

The actual html generetade in the view, when the modal is open. I tried remove the duplicated inclusion of jquery (in modal and in index), I got it (with Yii::$app->assetManager->bundles = ['yii\web\JqueryAsset' => false,],...) but the error persist, so I keep it.

<html lang="pt-BR">
    <body>
        // omitted content, body content of index view, with gridview etc.
        <div id="ajaxCrudModal" class="fade modal in" role="dialog" tabindex="-1" style="display: block;">
            <div class="modal-dialog ">
                <div class="modal-content">
                    <div class="modal-body"><div class="panel">
                        <div class="panel-body" style="height: 600px">
                            // omitted content
                        </div>
                    </div>
                    <script src="/yii2/basic/assets/32e507bc/jquery.js"></script>
                    <script src="/yii2/basic/assets/9f23c46b/yii.js"></script>
                    <script src="/yii2/basic/assets/9f23c46b/yii.validation.js"></script>
                    <script src="/yii2/basic/assets/9f23c46b/yii.activeForm.js"></script>
                    <script src="http://leafo.net/sticky-kit/src/sticky-kit.js"></script>
                    <script type="text/javascript">
                        //$('body').on('load', function(){
                        $('.panel-heading').stick_in_parent();
                        //});
                    </script>
                    </div>
                    <div class="modal-footer"></div>
                </div>
            </div>
        </div>

        <script src="/yii2/basic/assets/32e507bc/jquery.js"></script>
        <script src="/yii2/basic/assets/9f23c46b/yii.js"></script>
        <script src="/yii2/basic/assets/fb64637f/js/bootstrap.js"></script>
        <script src="/yii2/basic/assets/9f23c46b/yii.validation.js"></script>
        <script src="/yii2/basic/assets/9f23c46b/yii.activeForm.js"></script>
        <script src="/yii2/basic/assets/28255cf7/dist/js/bootstrap-dialog.js"></script>
        <script src="/yii2/basic/assets/9f23c46b/yii.gridView.js"></script>
        <script src="/yii2/basic/assets/75fbd502/js/kv-grid-action.js"></script>
        <script src="/yii2/basic/assets/8d08c7cb/jquery.pjax.js"></script>
    </body></html>

Upvotes: 0

Views: 1547

Answers (1)

Wilson Ng
Wilson Ng

Reputation: 201

You should use renderAjax instead of renderPartial because renderPartial do not load jquery library.

Upvotes: 1

Related Questions