Yii2。在 GridVIew 中通过 ajax 发送数据无效

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

我想在 GridView 中通过按下按钮(勾号、叉号)更改订单状态

但是当我在 Controller 中看到我的 $_POST 时它只有 csrf 令牌,当我出错时?

我的网格视图:

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

我的控制器:

public function actionContracts()
{

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

}

为什么不发送数据?当我按下按钮时,它只有:

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

您要发送什么数据?你是如何尝试发送它的,我在你发送的代码示例中看不到。

您可以将参数添加到请求中,将它们添加到 link 的 data-params 属性中。

例如:

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

这些参数将作为 POST 个参数发送。