没有不值得去解决的问题,也没有不值得去学习的技术!

Why ‘data-method’ => ‘post’ Sends GET Requests in Yii2 GridView::widget

1. In Yii2’s GridView::widget, setting ‘data-method’ => ‘post’ still resulted in a GET request.

PHP

 $dataProvider,
	'tableOptions' => ['class' => 'table table-centered table-nowrap table-striped mb-0'],
	'rowOptions' => function ($model, $key, $index, $grid) {
		return ['id' => "tr_{$key}"];
	},
	'options' => [
		'id' => 'grid',
		'class' => 'grid-view',
		'style' => "position: relative; overflow: auto; width: 100%;"
	],
	'filterModel' => $searchModel,
	'columns' => [
		['class' => 'yii\grid\SerialColumn'],
	
		
		[
			'class' => 'yii\grid\ActionColumn',
			'template' => '{messages} {abort}',
			'buttons' => [
				'abort' => function ($url, $model, $key) {
					if ($model->state == PhoneMessageTask::STATE_WAIT || $model->state == PhoneMessageTask::STATE_WAITING_FOR_SENDING) {
						return Html::a('', [
							'abort',
							'id' => $model->id
						], [
							'class' => 'action-icon remark-btn',
							'title' => '中止',
							'data-confirm' => '确定要中止该任务吗?',
							'data-method' => 'post',
						]);
					}
					return null;
				},
			],
		],//end ActionColumn
	],
	'emptyText' => '暂无信息', //没有数据时显示的信息
	'emptyTextOptions' => ['style' => 'text-align:center;'], //没有数据时显示信息的样式设置
	'showOnEmpty' => true, //没有数据时是否显示表格
	'summary' => '共{totalCount}条信息,共{pageCount}页,当前{begin}-{end}条',
	'pager' => [
		'options' => ['class' => 'hidden']
	]
]);
?>

2. I later found the cause was that yii.js was not loaded, even though the layout file did contain AppAsset::register($this);

PHP
<?php
 
namespace management\assets;
 
use yii\web\AssetBundle;
 
/**
 * Main management application asset bundle.
 */
class AppAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        'css/site.css',
    ];
    public $js = [
    ];
    public $depends = [
        'yii\web\YiiAsset',
        // 'yii\bootstrap4\BootstrapAsset',
    ];
}
 

3. Finally, I discovered the root cause: the layout file was missing

PHP
<?php $this->endBody() ?>

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系