Yii Framework Forum: AjaxLinks with RenderPartial - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

AjaxLinks with RenderPartial Very strange bug Rate Topic: -----

#1 User is offline   koz 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 23-June 10

Posted 05 July 2010 - 01:17 PM

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???!!
0

#2 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 06 July 2010 - 01:50 AM

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.
0

#3 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 13 July 2010 - 07:17 AM

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.
0

#4 User is offline   koz 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 23-June 10

Posted 14 July 2010 - 09:15 AM

View Postzaccaria, on 13 July 2010 - 07:17 AM, said:

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users