我正在尝试使用带有删除按钮的 Pjax 制作 Ajax GridView。删除不使用 Ajax。我是 Yii2 的新手,所以我们将不胜感激。谢谢。
index.php
<?php Pjax::begin(['id' => 'countries']) ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'title',
['class' => 'yii\grid\ActionColumn',
'buttons' => [
'delete' => function ($url, $model, $key) {
return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [
'title' => Yii::t('yii', 'Delete'),
'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
'data-method' => 'post',
]);
},
]
],
],
]); ?>
<?php Pjax::end() ?>
Controller
public function actionDelete($id)
{
$model = new Category();
$this->findModel($id)->delete();
$dataProvider = new ActiveDataProvider([
'query' => Category::find(),
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
'model' => $model,
]);
}
这是 Controller 中的公共(public)函数 actionIndex()
public function actionIndex()
{
$model = new Category();
$dataProvider = new ActiveDataProvider([
'query' => Category::find(),
]);
if ($model->load(Yii::$app->request->post()) && $model->save())
{
$model = new Category();
}
return $this->render('index', [
'dataProvider' => $dataProvider,
'model' => $model,
]);
}
最佳答案
data-method
和 data-confirm
不允许您通过 pjax 创建 ajax 请求,您应该实现自己的确认对话框并删除 POST 动词过滤器,或者您可以使用确认对话框和指定 http 方法实现您自己的 ajax 插件。
此外,我认为,必须有通过确认对话框扩展 pjax 插件的方法,但 Yii2 默认不提供此功能。
关于php - Yii2 Pjax 删除不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29441026/