我正在尝试使用带有删除按钮的Pjax创建Ajax GridView.删除没有Ajax.我是Yii2的新手,所以任何帮助都将不胜感激.谢谢.
原文链接:https://www.f2er.com/php/130839.html的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',]); }