Updating Grid-view using Pjax

Following this Wiki Yii 2.0: Pjax on ActiveForm and GridView - Yii2 instructions carefully,I have tried to update my gridview page on creation of new record, but surely missing something, as the page in grid-view is not getting updated on creation of new entry. I am putting below my code for form, index page and controller.

Controller Code:




public function actionIndex()

    {

        $model = new Medicine();


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

        {

            $model = new Medicine(); //reset model

        }

        $searchModel = new MedicineSearch();

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);


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

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,

            'model' => $model,

        ]);

    }



_form.php code




<?php


$this->registerJs(

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

        $("#new_medicine").on("pjax:end", function() {

            $.pjax.reload({container:"#medicine"});  //Reload GridView

        });

    });'

);

?>




<?php


use yii\helpers\Html;

use yii\widgets\ActiveForm;

use kartik\grid\GridView;

//use yii\grid\Gridview;

use yii\data\ActiveDataProvider;


/* @var $this yii\web\View */

/* @var $model app\models\Medicine */

/* @var $form yii\widgets\ActiveForm */

?>

<!-- <div class="row">

    <div class="col-lg-6 col-lg-offset-3"> -->

<div class="medicine-form">

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


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


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


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


    <div class="form-group">

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

        <?= Html::submitButton($model->isNewRecord ? 'Save & New' : '',$option=['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary','name'=>'save_and_new']) ?>

    </div>


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

    <?php yii\widgets\Pjax::end() ?>


</div>



code in index.php




use yii\helpers\Html;

use yii\grid\GridView;


/* @var $this yii\web\View */

/* @var $searchModel app\models\MedicineSearch */

/* @var $dataProvider yii\data\ActiveDataProvider */


$this->title = 'Medicines';

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

?>

<div class="medicine-index">


    <h1><?= Html::encode($this->title) ?></h1>

    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>


    <p>

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

    </p>

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

    <?= GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],


            'id',

            'medicine_id',

            'medicine_name',


            ['class' => 'yii\grid\ActionColumn'],

        ],

    ]); ?>

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

</div>



I am really struggling and can’t make out what I am missing here. It will be so nice, if someone can point out the short-coming, what I am doing wrong.

Thanks.

I think put your


 index.php (gridview)

code in


_form.php

file

Hi Amit, is there no way, to update the grid-view with Pjax keeping it on a separate page?

I have One solution for that register js of Pjax in index.php file

like this…

[color=#008800][size=2]


 $.pjax.reload({container:"#medicine"}); 

[/size][/color]

Hi Amit, how index.php will know the change event in _form.php. Can you tell me the full code to be included in index.php

I was also trying a variation in _form.php like


 $.pjax.reload({container:"index.php #medicine"}); 

but that is also not working.