I want to update gridview using Pjax, but somehow it doesn't work. Here is the code:
_search.php
<?php use yii\helpers\Html; use yii\widgets\ActiveForm; use yii\widgets\Pjax; $this->registerJs(" $('#btnAjaxSearch').click(function(){ $.ajax({ type: 'get', data: $('.bank-search form').serializeArray(), success: function (data) { $.pjax.reload({container:\"#bank\"}); }, error: function (XMLHttpRequest, textStatus, errorThrown) { alert('error'); } }); return false; }); ", \yii\web\View::POS_END, 'bank-search'); ?> <div class="bank-search"> <?php Pjax::begin(['id' => 'bank-form']); ?> <?php $form = ActiveForm::begin([ 'action' => ['index'], 'method' => 'get', ]); ?> <?= $form->field($model, 'bank_name') ?> <?= $form->field($model, 'state') ?> <?= $form->field($model, 'district') ?> <?= $form->field($model, 'city') ?> <div class="form-group"> <?= Html::Button('Search', ['class' => 'btn btn-primary','id' => 'btnAjaxSearch']) ?> </div> <?php ActiveForm::end(); ?> <?php Pjax::end(); ?> </div>
index.php
<?php use yii\helpers\Html; use yii\grid\GridView; use yii\widgets\Pjax; $this->title = 'Banks'; $this->params['breadcrumbs'][] = $this->title; ?> <div class="bank-index"> <h1><?= Html::encode($this->title) ?></h1> <?php echo $this->render('_search', ['model' => $searchModel]); ?> <p> <?= Html::a('Create Bank', ['create'], ['class' => 'btn btn-success']) ?> </p> <?php Pjax::begin(['id' => 'bank']); ?> <?= GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ ['class' => 'yii\grid\SerialColumn'], 'id', 'bank_name', 'state', 'district', 'city', <?php Pjax::end(); ?> </div>
controller
public function actionIndex() { $searchModel = new BankSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); }
A simple search works, but Pjax doesn't. I am new to Yii2, so any help would be appreciated. Thanks.