我正在尝试使用带有删除按钮的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() ?>
调节器
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',]);
}
数据方法和数据确认不允许您通过pjax创建ajax请求,您应该实现自己的确认对话框并删除POST动词过滤器,或者您可以使用确认对话框并指定http方法来实现您自己的ajax插件.
此外,我认为,必须有通过确认对话框扩展pjax插件的方法,但Yii2默认情况下不提供此功能.