Yii Framework Forum: Ajax link within Ajax-generated content - Yii Framework Forum

Jump to content

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

Ajax link within Ajax-generated content Rate Topic: -----

#1 User is offline   Favicon 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 27-January 12
  • Location:UK

Posted 06 March 2012 - 11:41 AM

I'm having some trouble getting ajax links to work when they're created as a result of an earlier Ajax query. I have a page which generates a bunch of content when a link is clicked. The content depends on which link is clicked, but it always contains an Ajax link. I'm trying to get a second Ajax call to simply return some plain text and put this text into an empty div. The code snippets below relate to the second Ajax link

The div to update:
<div id="dialogWrapper"></div>

The action (in my controller) called by my Ajax link:
public function actionAjax(){
	echo "test string";
}

The ajax link:
CHtml::ajaxLink('clickMe', array('controller/ajax'), array('update'=>'#dialogWrapper')

Now, if I place that Ajax link in the main view file for my page, then it works fine - clicking the link populates the div with the string 'test string'.

Next, if I leave that first link in the main view file, and put a copy of it inside a separate view file, which gets partially rendered by an earlier Ajax query, then both links will do the same thing, as expected.

Next, I tried changing the second link to run a different action:
CHtml::ajaxLink('clickMe', array('controller/anotherAjax'), array('update'=>'#dialogWrapper'));
This is where things get wierd because the link which should now point to the action 'anotherAjax' still in fact runs the exact same query as before - i.e. both links still update the div with the result of a query to the action 'ajax'.

Finally, I tried removing the ajax link from my main view file, so we are left with only the one in the partial view file. In this case, the link does nothing whatsoever. Is there something wierd about the way things are generated by partial view, or when content is produced via an Ajax request, that means I can't have another Ajax request defined in this way?

(Sorry if thats a confusing description - I'm really struggling to make it any clearer)
0

#2 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 698
  • Joined: 02-February 09

Posted 06 March 2012 - 11:44 AM

can you post the code of actionAnotherAjax
0

#3 User is offline   Favicon 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 27-January 12
  • Location:UK

Posted 06 March 2012 - 11:48 AM

The action anotherAjax is:
public function actionAnotherAjax(){
        echo "second test";
}


Edit: Accidentally posted actionAjax at first. Changed to the correct code snippet

This post has been edited by Favicon: 06 March 2012 - 11:50 AM

0

#4 User is offline   Favicon 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 27-January 12
  • Location:UK

Posted 06 March 2012 - 11:55 AM

I've just noticed something else that may be helpful; the URL that the Ajax link is now pointing to (after removing the first link) is the URL of the page I'm looking at, not the URL that I set in the Ajax link code.
0

#5 User is offline   Fábio Felicidade 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 14-March 12

Posted 19 March 2012 - 12:47 PM

Hi,

I have a similar problem with ajaxLinks.
I have two links in sidebar that refresh my content page by ajax. Until here, it's fine.
But if i put an ajaxLink in my content page, this links don't work.
My content page is loaded by renderPartial.

In model controller i have the following code:
public function actionCreate()
	{
		$model=new PropertyType;

		if(isset($_POST['PropertyType']))
		{
			$model->attributes=$_POST['PropertyType'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->ID));
		}

		$this->renderPartial('create',array(
			'model'=>$model,false,true
		));
	}


And in my sidebar i have two ajaxLinks:
<ul>
  <li><?php echo CHtml::ajaxLink(Yii::t('default','Property'),Yii::app()->createUrl('property/'), array('update'=>'#content-body'), array('class'=>'sidebar-menu-title')); ?></li>
  <li><?php echo CHtml::ajaxLink(Yii::t('default','Property types'),Yii::app()->createUrl('propertytype/'), array('update'=>'#content-body'), array('class'=>'sidebar-menu-title')); ?></li>
</ul>


Finally, in index content (that is loaded by any previous ajaxLinks) i have the following code with the ajaxLink(New button) that don't work:
   <?php
	echo CHtml::ajaxLink(Yii::t('default','New'),Yii::app()->createUrl('propertytype/create/'), array('update'=>'#obj-view'), array('class'=>'bt-new'));
	$this->widget('zii.widgets.CListView', array(
		'id'=>'obj-list',
		'dataProvider'=>$dataProvider,
		'itemView'=>'_view',
		'tagName'=>'table', 
		'sortableAttributes'=>array(
	        'name',
	        'status'
	    ),
	));
	?>
	<div id="obj-view"></div>


Can't figure where the problem is.
0

#6 User is offline   Favicon 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 27-January 12
  • Location:UK

Posted 20 March 2012 - 02:54 AM

I've worked around this issue now by using raw jquery to build the onclick events. In fact, the usual .click(function(){...}) or .on('click', function(){...}) methods in raw jquery didn't work, as these attach events on page load but don't attach to new objects that are added after page load. I had to use .live('click', function(){...}) to get the onclick events to be attached to new links as they are added to the page.

Perhaps this is where our problems with the Yii ajaxLink come from. Does anyone know whether Yii's ajaxLink uses .click, .on or .live to attach events to objects?
0

#7 User is offline   Fábio Felicidade 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 14-March 12

Posted 21 March 2012 - 02:44 PM

View PostFábio Felicidade, on 19 March 2012 - 12:47 PM, said:

Hi,

I have a similar problem with ajaxLinks.
I have two links in sidebar that refresh my content page by ajax. Until here, it's fine.
But if i put an ajaxLink in my content page, this links don't work.
My content page is loaded by renderPartial.

In model controller i have the following code:
public function actionCreate()
	{
		$model=new PropertyType;

		if(isset($_POST['PropertyType']))
		{
			$model->attributes=$_POST['PropertyType'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->ID));
		}

		$this->renderPartial('create',array(
			'model'=>$model,false,true
		));
	}


And in my sidebar i have two ajaxLinks:
<ul>
  <li><?php echo CHtml::ajaxLink(Yii::t('default','Property'),Yii::app()->createUrl('property/'), array('update'=>'#content-body'), array('class'=>'sidebar-menu-title')); ?></li>
  <li><?php echo CHtml::ajaxLink(Yii::t('default','Property types'),Yii::app()->createUrl('propertytype/'), array('update'=>'#content-body'), array('class'=>'sidebar-menu-title')); ?></li>
</ul>


Finally, in index content (that is loaded by any previous ajaxLinks) i have the following code with the ajaxLink(New button) that don't work:
   <?php
	echo CHtml::ajaxLink(Yii::t('default','New'),Yii::app()->createUrl('propertytype/create/'), array('update'=>'#obj-view'), array('class'=>'bt-new'));
	$this->widget('zii.widgets.CListView', array(
		'id'=>'obj-list',
		'dataProvider'=>$dataProvider,
		'itemView'=>'_view',
		'tagName'=>'table', 
		'sortableAttributes'=>array(
	        'name',
	        'status'
	    ),
	));
	?>
	<div id="obj-view"></div>


Can't figure where the problem is.



Ok...... the following code
$this->renderPartial('create',array(
			'model'=>$model,false,true
		));


has an error. Instead it, should be:
$this->renderPartial('create',array(
			'model'=>$model
		),false,true);

0

#8 User is offline   Favicon 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 27-January 12
  • Location:UK

Posted 22 March 2012 - 04:37 AM

View PostFábio Felicidade, on 21 March 2012 - 02:44 PM, said:

Ok...... the following code
$this->renderPartial('create',array(
			'model'=>$model,false,true
		));


has an error. Instead it, should be:
$this->renderPartial('create',array(
			'model'=>$model
		),false,true);



Fábio, did that solve your problem completely?
0

#9 User is offline   Fábio Felicidade 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 14-March 12

Posted 22 March 2012 - 06:25 AM

View PostFavicon, on 22 March 2012 - 04:37 AM, said:

Fábio, did that solve your problem completely?


Yes... note that the 3rd and 4th parameter was inside the model array instead in renderPartial parameters... Just the correction solved my problem, until now its working fine.
0

#10 User is offline   Favicon 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 27-January 12
  • Location:UK

Posted 22 March 2012 - 09:56 AM

Interesting. Unfortunately I've already had to just move on and write the jQuery by hand (see above) and didn't keep a copy of how I had it with renderPartials, so I can't check whether I'd made the same mistake. If I have to make another similar page in the future, I'll remember this and give it another go.
0

#11 User is offline   edwaa 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 17-May 09
  • Location:Seattle, WA

Posted 30 April 2012 - 12:47 AM

@Favicon: I'm having the same issue now. I will try your custom jQuery method and see if that works.

@Yii dev's + community: why doesn't ajaxLink() work when it comes from an ajax-generated response? What is the best work-around?
0

#12 User is offline   Luiz Celso 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 13-April 12

Posted 21 June 2012 - 12:38 PM

When I got this error, I simply gave to ajaxLink an id in htmlOptions().

echo CHtml::ajaxLink($representative->firstname.' '.$representative->lastname,
		$this->createUrl('/representantes/representantes/ver/id/'.$representative->representative_id),
		array(
				'update' => '#cidades',
		),
		array('id' => 'representante'.$representative->representative_id)
);


Happy coding.
0

#13 User is offline   Favicon 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 27-January 12
  • Location:UK

Posted 27 June 2012 - 01:25 AM

View PostLuiz Celso, on 21 June 2012 - 12:38 PM, said:

When I got this error, I simply gave to ajaxLink an id in htmlOptions().


What a great idea! I take the problem then is actually that Yii generates IDs automatically and so when running the Ajax request, the second Ajax link is assigned an ID that's already taken by something else on the page? Whereas overriding it with a custom ID prevents this from happening..

Thanks for the tip.
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