PJAX not working with Modal & GridView

Hi,

I am unable to get PJAX working when updating through popup Modal. I’ve followed a few helpful articles but still not having success.

index.php:


<?php


use yii\grid\GridView;

use yii\helpers\Html;

use yii\helpers\Url;

use yii\widgets\Pjax;


$this->title = 'Service Offerings';

$this->params['breadcrumbs'][] = $this->title;

?>

<div class="service-offering-index">

    <h1><?= Html::encode($this->title).' '.date("G:i:s a") ?></h1>

    <p>

        <?= Html::a('Create Service Offering', ['create'], ['class' => 'btn btn-success']) ?>

    </p>


<?php

    yii\bootstrap\Modal::begin([

        'header' => 'Update Service Offering',

        'id'=>'editModalId',

        'class' =>'modal',

        'size' => 'modal-md',

    ]);

	echo "<div class='modalContent'></div>";

    yii\bootstrap\Modal::end();


	//js code:

	$this->registerJs("$(document).pjax('a', '#some-id');");

	$this->registerJs(

	"$(document).on('ready pjax:success', function() {

	        $('.modalButton').click(function(e){

	           e.preventDefault(); //for prevent default behavior of <a> tag.

	           var tagname = $(this)[0].tagName;

	           $('#editModalId').modal('show').find('.modalContent').load($(this).attr('href'));

	       });

	    });

	");

	//$this->registerJs("$.pjax.reload('#some-id', options)");

?>


<?php \yii\widgets\Pjax::begin(['id'=>'some-id']);

    echo GridView::widget([

        'dataProvider' => $dataProvider,

        'columns' => [

            'title',

            'service_type',

            [ 'attribute' => 'description', 'options'=>['style' => 'width:70%'], ],

            'service_status',

            // Reference for AJAX implementation: http://stackoverflow.com/questions/34626334/bootstrap-modal-not-working-with-pjax-in-gridview-yii2

            ['class' => 'yii\grid\ActionColumn', 'template'=>'{custom_view}',

	            'buttons' => 

	            [	'custom_view' => function ($url, $model) {

	                	// Html::a args: title, href, tag properties.

		                return Html::a( '<i class="glyphicon glyphicon-new-window"></i>',

							['service-offering/update', 'id'=>$model['id']],

							['class'=>'btn btn-xs btn-default modalButton', 'title'=>'view/edit offering', ]

						);

	                },

	            ]

	        ]

        ],

    ]);

\yii\widgets\Pjax::end(); ?>


</div>

My form is:


<?php

use yii\helpers\Html;

use yii\widgets\ActiveForm;

?>

<div class="service-offering-form">

    <?php $form = ActiveForm::begin(['id'=>'service-offering-form-id', ]); ?>


    <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'description')->textarea(['rows' => 6]) ?>

    <div class="form-group">

        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

    </div>


    <?php ActiveForm::end(); ?>

</div>

Controller actions:


public function actionIndex()

    {

        $dataProvider = new ActiveDataProvider([

            'query' => VwServiceOffering::find(),

        ]);


        return $this->render('index', [

            'dataProvider' => $dataProvider,

        ]);

    }


public function actionUpdate($id)

    {

        $model = $this->findModel($id);


        if ($model->load(Yii::$app->request->post()) && $model->save()) {

            //return $this->redirect(['view', 'id' => $model->id]);

            return $this->redirect( Yii::$app->request->referrer );

        } else {

            return $this->renderAjax('update', [

                'model' => $model,

            ]);

        }

    }



The modal & functions work correctly but I’m still getting a page refresh on actionUpdate (as evidenced by the time function next to the title value (index.php - $this->title.’ '.date(“G:i:s a”)) above.

I appreciate any assistance. Thanks.

hi,

following links may help you

My link

it worked for me

1 Like

The link, http://stackoverflow.com/questions/34651018/yii2-pjax-on-activeform-and-gridview-got-it-working, was very helpful and I was able to get it working. For reference I did the following:

index.php:

  1. Ended up with my modal creation & 2 JS scripts: (see below)

  2. GridView wrapped into a Pjax container.


// JS: MODAL EDIT & PJAX BELOW.

    yii\bootstrap\Modal::begin([

        'header' => 'Update Lesson Learned',

        'id'=>'editModalId',

        'class' =>'modal',

        'size' => 'modal-md',

    ]);

	echo "<div class='modalContent'></div>";

    yii\bootstrap\Modal::end();


	$this->registerJs(

	"$(document).on('ready pjax:success', function() {

	        $('.modalButton').click(function(e){

	           e.preventDefault(); //for prevent default behavior of <a> tag.

	           var tagname = $(this)[0].tagName;

	           $('#editModalId').modal('show').find('.modalContent').load($(this).attr('href'));

	       });

	    });

	");


	// JS: Update response handling

	$this->registerJs(

    'jQuery(document).ready(function($){

        $(document).ready(function () {

            $("body").on("beforeSubmit", "form#lesson-learned-form-id", function () {

                var form = $(this);

                // return false if form still have some validation errors

                if (form.find(".has-error").length) {

                    return false;

                }

                // submit form

                $.ajax({

                    url    : form.attr("action"),

                    type   : "post",

                    data   : form.serialize(),

                    success: function (response) {

                        $("#editModalId").modal("toggle");

                        $.pjax.reload({container:"#lessons-grid-container-id"}); //for pjax update

                    },

                    error  : function () {

                        console.log("internal server error");

                    }

                });

                return false;

             });

            });

	    });'

	);


<?php Pjax::begin(['id' => 'lessons-grid-container-id']) ?>

    <?= GridView::widget([

        'dataProvider' => $dataProvider,

        

        'columns' => [

            ['class' => 'yii\grid\ActionColumn', 'template'=>'{custom_view}',

	            'buttons' => 

	            [	'custom_view' => function ($url, $model) {

	                	// Html::a args: title, href, tag properties.

		                return Html::a( '<i class="glyphicon glyphicon-new-window"></i>',

							['lesson/update', 'id'=>$model['id']],

							['class'=>'btn btn-xs btn-default modalButton', 'title'=>'view/edit', ]

						);

	                },

	            ]

	        ],

            [ 'attribute' => 'project','options'=>['style' => 'max-width:20%'], ],

            // ...

        ], // End columns

    ]); ?>

<?php Pjax::end() ?>

_form.php:

  1. Updated the ActiveForm options to:

<?php $form = ActiveForm::begin(['options'=>['id'=>'lesson-learned-form-id', 'data-pjax'=>true, ] ]); ?>

controller:

  1. Commented out the redirect.

public function actionUpdate($id)

    {	

        $model = $this->findModel($id);


        if ($model->load(Yii::$app->request->post()) && $model->save()) {

        	//return $this->redirect( Yii::$app->request->referrer );

            //return $this->redirect(['view', 'id' => $model->id]);

        } else {

            return $this->renderAjax('update', [

                'model' => $model,

            ]);

        }

    }

Thank you for the assistance.

good to hear that my link helped someone. An upvote to that question or answer would have been nice too

1 Like

upvoted :D

Thank you very much… It’s a Great Job!!