Here is a example on how to use Pjax with GridView (yii\grid\GridView) and ActiveForm (yii\widgets\ActiveForm) widgets in Yii2.
The example uses a db table "countries" with field id, name
Controller ¶
public function actionIndex()
{
$model = new Countries();
if ($model->load(Yii::$app->request->post()) && $model->save())
{
$model = new Countries(); //reset model
}
$searchModel = new CountriesSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'model' => $model,
]);
}
Views ¶
index.php
<?php
use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
/* @var $this yii\web\View */
/* @var $searchModel app\models\CountriesSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = Yii::t('app', 'Countries');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="countries-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a(Yii::t('app', 'Create {modelClass}', [
'modelClass' => 'Countries',
]), ['create'], ['class' => 'btn btn-success']) ?>
</p>
<!-- Render create form -->
<?= $this->render('_form', [
'model' => $model,
]) ?>
<?php Pjax::begin(['id' => 'countries']) ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'name',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end() ?>
</div>
_form.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model app\models\Countries */
/* @var $form yii\widgets\ActiveForm */
?>
<?php
$this->registerJs(
'$("document").ready(function(){
$("#new_country").on("pjax:end", function() {
$.pjax.reload({container:"#countries"}); //Reload GridView
});
});'
);
?>
<div class="countries-form">
<?php yii\widgets\Pjax::begin(['id' => 'new_country']) ?>
<?php $form = ActiveForm::begin(['options' => ['data-pjax' => true ]]); ?>
<?= $form->field($model, 'name')->textInput(['maxlength' => 200]) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php yii\widgets\Pjax::end() ?>
</div>
great
thanks for sharing this one
how can I update the grid-view on different page
I want to update the grid-view on different page then the form page that is without rendering the form on grid-view page. Thanks.
What about a widget ?
Hi, thank you for your example.
Is it possible to use this logic inside a widget ?
Excelent
Break out from my mind! Very thank u!
controller
use default gii controller
public function actionIndex() { $searchModel = new BonusSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); }
how can I update the grid-view on different page
hi,
As @Pawan Joshi said i also want to know how to use this pjax with gridview in a different page .
that right! it's worked, thks!
Hello i'm using your code but in my web is not working; All the page it's reload instade of just listview the whole page is reload
my controlle code
`
phppublic function actionView($chn)
{ $dataProvider = new ActiveDataProvider([ 'query' => Post::find() ->Where(['Channel_id' => $chn]) ->orderBy(['Post_crdate'=>SORT_DESC]), 'pagination' => [ 'pageSize' => 4, ], ]); $model_PstGlr = new PostGallery; $model_Permission = new Permission; $acc_PstGlr = new AxPostGallery; if ($model_PstGlr->load(Yii::$app->request->post()) ) { $acc_PstGlr->CreateGallery($model_PstGlr, $chn); } return $this->render('view', [ 'model' => $this->findModel($chn), 'dataProvider' => $dataProvider, 'model_Permission' => $model_Permission, 'model_PstGlr' => $model_PstGlr, ]);
and my view is : ```php <hr> <?= $this->render('/postgallery/_formUploadGalleryScroll', [ 'model_PstGlr' => $model_PstGlr, 'model_Permission' => $model_Permission, ]) ?> <hr> <?php Pjax::begin(['id' => 'posts']) ?> <?= ListView::widget([ 'dataProvider' => $dataProvider, 'summary'=>'', 'itemOptions' => ['class' => 'item'], 'itemView' => '/content/views/chnPosts', 'pager' => ['class' => ScrollPager::className(), ] ]); ?> <?php Pjax::end() ?>
and my _formUploadGalleryScroll
<?php $this->registerJs( '$("document").ready(function(){ $("#new_posts").on("pjax:end", function() { $.pjax.reload({container:"#posts"}); //Reload GridView }); });' ); ?> <div class="post-gallery-form"> <?php yii\widgets\Pjax::begin(['id' => 'new_posts']) ?> <?php $form = ActiveForm::begin(['options' => ['data-pjax' => true ]]); ?> <?= $form->field($model_PstGlr, 'PGalleryFile[]')->fileInput(['multiple' => true, 'accept' => 'image/*']) ?> <?= $form->field($model_PstGlr, 'post_text')->textarea(['rows' => 2]) ?> <?= $form->field($model_PstGlr, 'permission_id')->dropdownList($model_Permission->PermissionOn()) ?> <div class="form-group"> <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?> </div> <?php ActiveForm::end(); ?> <?php yii\widgets\Pjax::end() ?> </div>
I mean to ask, how exactly do you get to add and declare the pjax widget to the yii2 framework because i have tried using it but it says "Declaration of referenced class is not found in built-in library and projject file"
Any help rendered will be appreciated
I have set enablePushState to false, But its not working with form but working with gridview
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.