Port Yii1 delete behavior to Yii2

Default implementation of delete button / delete process in CGridView in Yii1 was done entirely on POST / AJAX and resulted in refresh of grid view after successful deletion of particular row.

In Yii2 this is a standard GET request, that redirects to given page after successful delete.

Is there an easy, core-based (without to much AJAX-JS digging) way of porting Yii1 behavior to Yii2 – to have AJAX-only delete and grid refresh after that delete?

You’re going to have to use Pjax or write the ajax yourself.

Quick guide

pjax docs

In your delete action controller you can do




if(Yii::$app->request->isPjax){

  	Yii::$app->session->setFlash('success', 'You have successfully deleted something!');

}else{

      Yii::$app->session->setFlash('success', 'You have successfully deleted something!');

      redirect('to-some-page');

}

1 Like