Answer the question
In order to leave comments, you need to log in
Is it possible in Yii2 to asynchronously add data and update it to a ListView using PJAX?
Good afternoon,
Such a thing works fine with GridView
Example:
Controller
public function actionIndex()
{
$model = new Notes();
if ($model->load(Yii::$app->request->post()) && $model->save())
{
$model = new Notes();
}
$searchModel = new NotesSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'model' => $model,
]);
}
public function actionIndex()
{
$model = new Notes();
if ($model->load(Yii::$app->request->post()) && $model->save())
{
$model = new Notes();
}
$searchModel = new NotesSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'model' => $model,
]);
}
<?php
use yii\helpers\Html;
use yii\widgets\Pjax;
use yii\grid\GridView;
/* @var $this yii\web\View */
/* @var $searchModel app\modules\notes\models\NotesSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = Yii::t('app', 'Notes');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="notes-index">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form',[
'model' => $model,
]) ?>
<?php Pjax::begin(['id' => 'notes']) ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'note:ntext',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end() ?>
</div>
<?php
use yii\helpers\Html;
use yii\widgets\Pjax;
use yii\grid\GridView;
/* @var $this yii\web\View */
/* @var $searchModel app\modules\notes\models\NotesSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = Yii::t('app', 'Notes');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="notes-index">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form',[
'model' => $model,
]) ?>
<?php Pjax::begin(['id' => 'notes']) ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'note:ntext',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end() ?>
</div>
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model app\modules\notes\models\Notes */
/* @var $form yii\widgets\ActiveForm */
?>
<?php
$this->registerJs(
'$("document").ready(function(){
$("#new_note").on("pjax:end", function() {
$.pjax.reload({container:"#notes"}); //Reload GridView
});
});'
);
?>
<div class="notes-form">
<?php yii\widgets\Pjax::begin(['id' => 'new_note']) ?>
<?php $form = ActiveForm::begin(['options' => ['data-pjax' => true]]); ?>
<?= $form->field($model, 'note')->textarea(['rows' => 6]) ?>
<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 Pjax::end(); ?>
</div>
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model app\modules\notes\models\Notes */
/* @var $form yii\widgets\ActiveForm */
?>
<?php
$this->registerJs(
'$("document").ready(function(){
$("#new_note").on("pjax:end", function() {
$.pjax.reload({container:"#notes"}); //Reload GridView
});
});'
);
?>
<div class="notes-form">
<?php yii\widgets\Pjax::begin(['id' => 'new_note']) ?>
<?php $form = ActiveForm::begin(['options' => ['data-pjax' => true]]); ?>
<?= $form->field($model, 'note')->textarea(['rows' => 6]) ?>
<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 Pjax::end(); ?>
</div>
<?
Pjax::begin(['id' => 'notes',
'timeout' => false,
'enablePushState' => false,
'clientOptions' => ['method' => 'POST']
]);
echo ListView::widget([
'dataProvider' => $dataProvider,
'itemView' => '_list_item',
'layout' => "{items}\n{pager}"
]);
yii\widgets\Pjax::end();
?>
Answer the question
In order to leave comments, you need to log in
ListView with gjvjom
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question