Ajax delete Issue with pagination

Hi,

Issue: When pagination is set for the list view, ajax update/delete is not working when i am in 2nd page(or only working when i am in 1st page). I tried to find the solution in forum i could not find the related one. Please help me.

Code:

Active record Behaviour




    public function getCommentDataProvider()

	{

		return new CArrayDataProvider($this->getComments(),array(

			'pagination'=>array(

				'pageSize'=>5,

			),

		));

	}



Controller




public function actionDelete($id)

	{

		// we only allow deletion via POST request

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

		{

			$comment = $this->loadModel($id);

			if (!Yii::app()->user->isGuest && (Yii::app()->user->id == $comment->userId))

			{

				$comment->delete();


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

				if (!Yii::app()->request->isAjaxRequest) {

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

				}

			}

			else {

				throw new CHttpException(403,'Only comment owner can delete his comment.');

			}

		}

		else {

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

		}

	}




_form




   <div class="row buttons">

	    <?php if ($comment->isNewRecord) {


			echo $form->hiddenField($comment, 'type');

			echo $form->hiddenField($comment, 'key');

			

			echo CHtml::hiddenField('returnUrl',Yii::app()->request->requestUri);

		  

			echo CHtml::ajaxSubmitButton('Comment',

                array('/comment/comment/create'),

		        array(

                    'replace'=>'#ext-comment-form-new',

                    'error'=>"function(){

                        $('#Comment_message').css('border-color', 'red');

                        $('#Comment_message').css('background-color', '#fcc');

                    }"

		        ),

		        array('id'=>'ext-comment-submit' . (isset($ajaxId) ? $ajaxId : ''))

		    );

		} else {

			echo CHtml::ajaxSubmitButton('Update',

				array('/comment/comment/update', 'id'=>$comment->id),

				array(

					'replace'=>'#ext-comment-form-edit-'.$comment->id,

					'error'=>"function(){

						$('#Comment_message').css('border-color', 'red');

						$('#Comment_message').css('background-color', '#fcc');

					}"

		        ),

		        array('id'=>'ext-comment-submit' . (isset($ajaxId) ? $ajaxId : ''))

	        );

		}

		?>

	</div>




Thank you.

PS: Using Yii 1.1.8 / Windows / MySql

I think the problem is a classic: When initially rendering the view containing the listview the output is processed meaning js scripts are loaded and events are attached to the buttons on that page. However, on ajax requests (like pagination) the view may only be partially rendered and no scripts are processed now meaning that no events are attached to the new buttons being rendered after page 1.

You should be able to solve this problem either by:

  1. Process outputs on partial rendering like this:

$this->renderPartial("yourlistview",array('data'=>$data),false,true); //fourth parameter means "process outputs"

or

  1. Don’t use Yii’s ajaxXXX() methods but put all your javascript into a separate file that is included via Yii::app()->clientScript-registerScript() on the first request so you don’t have to think about partial rendering JS code.

Hope it helps,

Hannes