AjaxLinks with RenderPartial

Hi i’m using 2 AjaxLinks to perform actionAddToProject and actionDelFromProject.

The objective is add and remove links to a project from a list of links.

My LinkController:




	public function actionIndex()

	{

		$dataProvider=new CActiveDataProvider('Link');

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

			'dataProvider'=>$dataProvider,

			'id_project'=>$id_project,

		),false,true);

	}


	public function actionAddToProject()

	{

		if(isset($_POST['id_link'],$_REQUEST['id_project']))

		{

			//save links to project

			$id_project = $_REQUEST['id_project'];

			$id_link = $_POST['id_link'];


			$modelprojectfile=new ProjectFile;

			$modelprojectfile->deleteAll('id_project = :project AND id_link = :link',array(':project'=>$id_project,':link'=>$id_link));

			$modelprojectfile->id_project=$id_project;

			$modelprojectfile->id_link=$id_link;

			$modelprojectfile->save();

			echo "Link ".$id_link." added with success to project ".$id_project;

		}

		$dataProvider=new CActiveDataProvider('Link');

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

			'dataProvider'=>$dataProvider,

			'id_project'=>$id_project,

		),false,true);

	}


	public function actionDelFromProject()

	{

		if(isset($_POST['id_link'],$_REQUEST['id_project']))

		{

			//remove links from project

			$id_project = $_REQUEST['id_project'];

			$id_link = $_POST['id_link'];


			$modelprojectfile=new ProjectFile;

			$modelprojectfile->deleteAll('id_project = :project AND id_link = :link',array(':project'=>$id_project,':link'=>$id_link));

			echo "Link ".$id_link." deleted with success from project ".$id_project;

		}

		$dataProvider=new CActiveDataProvider('Link');

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

			'dataProvider'=>$dataProvider,

			'id_project'=>$id_project,

		),false,true);

	}



On my index _view i got:




	<?php

	$verifyLinkProject=ProjectLink::model()->findByAttributes(

								array('id_link'=>CHtml::encode($data->id_link),

										'id_project'=>CHtml::encode($_REQUEST['id_project']))

								);

	?>


	<?php

	if(count($verifyLinkProject)>0)

		echo CHtml::ajaxLink(

			'Delete from project',

			array('link/DelFromProject'),

			array('update' => '#index','type'=>'POST', 'data'=>array('id_link'=>CHtml::encode($data->id_link),

					'id_project'=>CHtml::encode($_REQUEST['id_project'])))

		);

	else if (ctype_digit($_REQUEST['id_project']))

		echo CHtml::ajaxLink(

			'Add to project',

			array('link/AddToProject'),

			array('update' => '#index','type'=>'POST', 'data'=>array('id_link'=>CHtml::encode($data->id_link),

					'id_project'=>CHtml::encode($_REQUEST['id_project'])))

		);

	?>



Now the strange thing is that this works… but when i click for example: ‘Delete from project’ it deletes the link and changes the ajaxlink to “Add to project” now its ok… but if i click again “Add to project” it will repeat the actions till that moment… if i clicked 5 times on that button next time i click it will first repeat all my clicks and just then will be performed my last action… confusing!!!

You have to process your output, by calling




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

                        'dataProvider'=>$dataProvider,

                        'id_project'=>$id_project,

                        false,

                        true



The 4th parameter will make so that all clientscript will work fine.

The solution works perfectly, but has one side effect: after reloading the ClientScript code OUTSIDE the form loaded will not work.

That is still an open issue and we are all waiting for a solution.

Personally, I use this solution and I pay attention to reload all clientScirpted widgets.

Try with my solution, without false, true (that makes all click to be repeated).

Give a uniqueId to all buttons (like "add$i" - "delete$i").

Is always difficoult to debug ajax page, please note that I am just trying to advice you, I am not sure if it really will work.

with this aproach it works also with normal render… much thanks.