E
E
EVOSandru62017-08-11 13:12:32
Yii
EVOSandru6, 2017-08-11 13:12:32

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,
        ]);
    }

index.php templates

<?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>

_form.php
<?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>

Taken from here:
https://nix-tips.ru/yii2-primeryaem-pjax-na-active... I'm trying to replace the ListView
fragment with a GridView
<?
    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();
    ?>

And the page is reloaded.
Can this be fixed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-08-11
@EVOSandru6

ListView with gjvjom

did not met
if the GridView also works, then it will work with the listView. Both there and there data in activeDataProvider. So it's all the same
Should not. Perhaps the problem is elsewhere, check what's in debug. Where the requests go, it is possible that the redirect goes to the server.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question