Wrong item deleted with pagination

Hi All,

I’m having some problems deleting with pagination switched on.

I’ve added a delete link to a customised index page which works fine under normal circumstances but after adding pagination in the data provider, if I attempt to delete an item from page 2 it actually removes the equivalent item from the first page (deleting first item in page 2 deletes first item in page 1; deleting second item in page 2 deletes second item in page 1, etc).

Here are my controller’s index and delete actions:


public function actionIndex()

{

	$vehicles = Vehicle::model()->vehiclesOfLoggedInUser();

		

	$dataProvider = new CArrayDataProvider($vehicles, array(

		'sort'=>array(

			'attributes'=>array(

				'registration', 'make', 'model'),

		),

		'keyField' => 'vehicle_id',

		'pagination'=>array(

			'pageSize'=>10,

		),

	));

	$this->render('index',array(

		'dataProvider'=>$dataProvider,

	));

}


public function actionDelete($id)

{

	if(Yii::app()->request->isPostRequest)

	{

		// we only allow deletion via POST request

		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

		if(!isset($_GET['ajax']))

			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));

	}

	else

		throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');

}

And the delete link in my view:


<?php echo CHtml::Link('Delete Vehicle', '#', array('submit'=>array('vehicle/delete','id'=>$data->vehicle_id),'confirm'=>'Are you sure?')); 



I’m assuming it’s because the contents of the second page isn’t available to POST but is there away to fix this?

Any suggestions gratefully received!

Hi DanEdge,

What do you use in your index view, a CListView or a CGridView?

And how do you make your delete links?

We had a similar problem with the Yii blog demo, check this issue for more information - https://github.com/yiisoft/yii/issues/148

Hi Softark,

Thanks for replying. It’s a CListView:


$this->widget('zii.widgets.CListView', array(

	'dataProvider'=>$dataProvider,

	'itemView'=>'_view',

)); 

And the delete link in _view is:


<?php echo CHtml::Link('Delete Vehicle', '#', array('submit'=>array('vehicle/delete','id'=>$data->vehicle_id),'confirm'=>'Are you sure?')); 

Looks like it might be related to the issue mdomba mentions below.

Thanks for taking the time to reply and for the info - that looks like the same issue. I’m not clear what the issue tracker is saying - seems it’s closed but presumably wasn’t fixed in 1.1.10. Is it still outstanding then? If so, is there any approach I can try to get around it that you can suggest?

It is fixed… here is the change that was made - https://github.com/mdomba/yii/commit/38f20e0b529bde0cf29bfdca2dfe435694819a23

In short you need to write the needed jQuery call manually and use only the clean HTML link.

Ahh, okay. That’s great. Thanks very much!