YII2 Form with GridView and Pjax

1.7k Views Asked by At

I have a view with to models: contact and phones, and I want to create a view to update this two models, my view is like this:

<?php $form = ActiveForm::begin(['id'=>'Contact']); ?>
<?= $form->field($model, 'name')->textInput() ?>
<?php Pjax::begin(); ?>
<?= \yii\grid\GridView::widget([
    'id' => 'phonesGrid',
    'dataProvider' =>  new ArrayDataProvider([
        'allModels' => $fones,
        'sort' => [
            'attributes' => ['number', 'ramal'],
        ],
        'pagination' => false,
    ]),
    'columns' => [
        'number',
        'ramal',
         ['class' => 'yii\grid\ActionColumn']
    ],
]); ?>
<?php Pjax::end(); ?>  <?php ActiveForm::end(); ?>

The problem is when i call $.pjax.reload the action (create ou update) of ContactController is call but the request not has the form data of contact and the data entering is clear. How can i do this in YII2?

Thanks.

1

There are 1 best solutions below

0
On

If I understand your question correctly, you get an empty response from the pjax call. This might be caused by the fact that you have your actual form(and it's models) are outside of the pjax call, causing it NOT to refresh them and making no connection whatsoever.

In my opionion it's best to contain ALL the data you have inside a pjax with queries/models. For instance:

<div class='resp-col col-12'>
    <?php Pjax::begin([
        'id'=>'all-tags',
        'timeout' => 5000,
    ]); ?>
<?php
$query = Tag::find();
$dataProvider = new ActiveDataProvider([
    'query' => $query,
    'pagination' => [
        'pageSize' => 20,
    ],
]);
echo GridView::widget([
    'dataProvider' => $dataProvider,
    'columns'=> [
      'view'=>[
        'header'=>'view',
        'options'=>[
          'width'=>'5%'
        ],
        'format'=>'raw',
        'value'=>function ($data) {
            return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', Url::to(['detail', 'id'=>$data->id]), ['data-type'=>'modal', 'data-title'=>'View item']).
            '<a><span class="glyphicon glyphicon-remove" onclick=DeleteTag("'.$data->id.'")></span></a>';;
        },
      ],
      'name',
      'content',
    ]
]);
 ?>
<?php Pjax::end()?>

When a pjax.reload is called on all-tags, it will also perform a new query and therefore return a new array of data.

Hopefully this is helpful to you.