我正在尝试使用带有删除按钮的Pjax制作一个Ajax GridView。删除没有Ajax。我是Yii2的新手,如有任何帮助,我们将不胜感激。非常感谢。
index.php
<?php Pjax::begin(['id' => 'countries']) ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yiigridSerialColumn'],
'id',
'title',
['class' => 'yiigridActionColumn',
'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() ?>
控制器
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中的公共函数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默认情况下不提供这一功能。
首先删除"data confirm"one_answers"data method"=>"post"。pjax无法工作。如果你想实现一个带有操作按钮的确认框,下面是我在视图index.php文件中要做的。。
<?php Pjax::begin(['id' => 'pjax-container']);
echo GridView::widget([
'test' => function ($url, $dataProvider) {
return Html::a('Test',
['/site/test'],
['title'=>'Test',
'onclick' => "if (confirm('ok?')) {
$.ajax('/site/test', {
type: 'POST'
}).done(function(data) {
$.pjax.reload({container: '#pjax-container'});
});
}
return false;
",
]);
},
])
Pjax::end();
?>
在我的控制器
public function actionTest()
{
if (!Yii::$app->request->isAjax) {
return $this->redirect(['index']);
}
}
这样你就可以得到确认等。如果你愿意,你可以使用其他第三方引导程序确认等,并且会很好地工作。
<?php Pjax::begin(['id' => 'model-grid']);
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
//...
[
'class' => 'yiigridActionColumn',
'template' => '{update} {delete}',
'contentOptions' => ['class' => 'action-column'],
'buttons' => [
'delete' => function ($url, $model, $key) {
return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [
'title' => 'Delete',
'data-pjax' => '#model-grid',
]);
},
],
],
],
]);
Pjax::end(); ?>
控制器内
public function actionDelete($id)
{
$this->findModel($id)->delete();
$searchModel = new ModelSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
请尝试修改actionDelete()
public function actionDelete($id)
{
$this->findModel($id)->delete();
return yiiwebResponse::redirect(['index'] , 302 , false);
// return $this->redirect(['index']);
}
因为Controller->redirect()不能禁用ajaxCheck,所以需要使用Response来执行此操作。
我在中创建了相同的问题https://github.com/yiisoft/yii2/issues/11058.
可以这样使用:
视图中:
'delete' => function ($url, $model, $key) {
$options = [
'title' => Yii::t('common', 'delete'),
'aria-label' => Yii::t('common', 'delete'),
'data-pjax' => 'w0',//id
'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
'data-method' => 'post',
'class' => 'btn btn-xs btn-danger'
];
return Html:: a('<i class="fa fa-fw fa-trash"></i>', [
'delete',
'id' => $model -> id
], $options);
}
在控制器中:
$this -> findModel($id) -> delete ();
$searchModel = new AdminSearch();
//get the referer url
$url = Yii::$app -> request -> referrer;
$arr = parse_url($url, PHP_URL_QUERY);
parse_str($arr, $output);//get the $_GET array
$dataProvider = $searchModel -> search($output);
return $this -> render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
首先在网格视图的末尾添加Pjax::end();
然后指定:
'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',
]);
},
请注意,您不需要指定'data-pjax' => '0'
,因为它会禁用pjax链接。
有关更多详细信息,请查看此链接
您的控制器应该是:
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
不要设置data-method
和data-confirm
,因为Pjax
不支持。
删除两者后仍然不起作用,是的,因为下面的代码您的控制器不允许Pjax-get-Request。
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'], // **remove this**
],
],
];
您需要使用Pjax-Post方法将其应用于您的Pjax
'clientOptions' => ['method' => 'POST']