Search code examples
phpgridviewyii2web-frameworks

Yii2. Sending data by ajax in GridVIew isn't works


I want to change status of order in line in GridView, by pushing buttons (check, cross) enter image description here

But when I see my $_POST in Controller it has only csrf token, when I make a mistake?

My GridView:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        [
                'attribute' => 'car_id',
                'value' => 'car.state_num'
        ],
        [
            'attribute' => 'driver_id',
            'value' => 'driver.name'
        ],
        'status',
        'first_date',
        'second_date'

        ['class' => 'yii\grid\ActionColumn',
        'template' => "{confirm} {decline}",
            'buttons' => [
                'confirm' => function ($url, $model, $key='new_stat') {
                    return Html::a('<span class="glyphicon glyphicon-ok"></span>', $url, [
                        'title' => Yii::t('yii', 'confirm'),
                        'data-confirm' => Yii::t('yii', 'Сдать авто?'),
                        'data-method' => 'post',
                        'data-pjax' => 1,
                    ]);
                },
                'decline' => function ($url, $model, $key='new_stat') {
                    return Html::a('<span style="color: red" class="glyphicon glyphicon-remove"></span>', $url, [
                        'title' => Yii::t('yii', 'decline'),
                        'data-confirm' => Yii::t('yii', 'Отказ?'),
                        'data-method' => 'post',
                        'data-pjax' => 2    ,
                    ]);
                }
            ],
            'urlCreator' => function ($action, $model, $key, $index) {
                if ($action === 'confirm') {
                    $url ='contracts';
                    return $url;
                }
                if ($action === 'decline') {
                    $url ='contracts';
                    return $url;
                }
            }
        ]
    ]
]); ?>

And my Controller:

public function actionContracts()
{

    if(Yii::$app->request->post())
    {
        print_r($_POST);
        die();
    }

}

Why it does not send data? When i push the button it has only:

Array ( [_csrf] => P-VomWJo5fwNwRi8uGnoMrw-uJPvLK9TM-k4k_Ka5SVQvF_VDR6Cj3unc_6BD6B-2Xf-27ln1RJHik32uf6RQA== )


Solution

  • What data are you trying to send? And how are you trying to send it, I cannot see that in the code sample you sent.

    You can add parameters to the request adding them to the data-params attribute of your link.

    For example:

    'template' => "{confirm} {decline}",
    'buttons' => [
      'confirm' => function ($url, $model, $key='new_stat') {
        return Html::a(
          '<span class="glyphicon glyphicon-ok"></span>', 
          $url, [
            'title' => Yii::t('yii', 'confirm'),
            'data-confirm' => Yii::t('yii', 'Сдать авто?'),
            'data-method' => 'post',
            'data-pjax' => 0,
            'data-params' => [
              'param_name_1' => 'param_value_1',
              'param_name_2' => 'param_value_2',
              'param_name_3' => 'param_value_3',
          ],
      ]);
      },
      'decline' => function ($url, $model, $key='new_stat') {
        return Html::a(
          '<span style="color: red" class="glyphicon glyphicon-remove"></span>', 
          $url, [
            'title' => Yii::t('yii', 'decline'),
            'data-confirm' => Yii::t('yii', 'Отказ?'),
            'data-method' => 'post',
            'data-pjax' => 0    ,
            'params' => [
              'param_name_1' => 'param_value_1',
              'param_name_2' => 'param_value_2',
              'param_name_3' => 'param_value_3',
          ],
        ]);
      }
    ],
    

    Those parameters will be sent as POST parameters.