Yii Framework Forum: [Solved]: New member question: AjaxLink inside a CListView - Yii Framework Forum

Jump to content

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

[Solved]: New member question: AjaxLink inside a CListView Rate Topic: ***** 1 Votes

#1 User is offline   Hussein 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 01-March 11

  Posted 13 March 2011 - 05:00 AM

Hello everyone :)

I have been using PRADO for 2.5 years now,earlier this month we decided to abandon it completely and migrate our projects on Yii, which is one of the best frameworks existing as I believe.

I have been able to fix mostly all problems I had, but I have small problem I couldn't fix in using ajax.

I have a clistview, which populates render items according to received data, inside each item there is an ajaxLink, this link sends a request to the controller with data unique to each rendered item.

it works fine on the first page, but when i change the page (ajax pager), it stops working.

the viewer
<?php echo CHtml::ajaxLink(
            $img,        
            array('shout/ajaxTest'), 
            array(
                'update'=>'#req_res'.$data->id,
                'data'=>array('fileurl'=>$data->Url(),'imageurl'=>$data->getRecord('ImageUrl')),
                'type'=>'post',
               
            )
            
        );
        ?>
	</div>
	

<div id="req_res<?php echo $data->id; ?>"><?php echo $data->id; ?></div>



What i figured out so far is that a unique javascript for each item is populated in the page , attached to id of the item container "yw0, yw1 ..etc", so when i change the page, the first new item in the new page will trigger the js for yw0, which is populated for the first page.

The question is, is there a way to update the js from CListView so it works even when i change the page? I saw several ajax problems used clientscript and other solutions that didn't work with me.

If there is no solution in yii, what would be the jquery method best for this scenario?

Thanks !
0

#2 User is offline   Hussein 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 01-March 11

Posted 14 March 2011 - 02:23 AM

No one can help a new member here ? :)
0

#3 User is offline   Anupam 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 90
  • Joined: 28-February 10
  • Location:INDB, India

Posted 14 March 2011 - 04:50 AM

View PostHussein, on 13 March 2011 - 05:00 AM, said:

it works fine on the first page, but when i change the page (ajax pager), it stops working.



U should provide a unique id to each of the ajaxlink.
echo CHtml::ajaxLink(
            $img,        
            array('shout/ajaxTest'), 
            array(
                'update'=>'#req_res'.$data->id,
                'data'=>array('fileurl'=>$data->Url(),'imageurl'=>$data->getRecord('ImageUrl')),
                'type'=>'post',),
			array('live'=>false, 'id'=>'shout-ajax-'.$data->id,)
        );



if u r returning the pager itself from the ajax request & experience multiple post problem each time u click the link keep the 'live'=>false there.
0

#4 User is offline   Hussein 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 01-March 11

Posted 14 March 2011 - 05:16 AM

View PostAnupam, on 14 March 2011 - 04:50 AM, said:

U should provide a unique id to each of the ajaxlink.
echo CHtml::ajaxLink(
            $img,        
            array('shout/ajaxTest'), 
            array(
                'update'=>'#req_res'.$data->id,
                'data'=>array('fileurl'=>$data->Url(),'imageurl'=>$data->getRecord('ImageUrl')),
                'type'=>'post',),
			array('live'=>false, 'id'=>'shout-ajax-'.$data->id,)
        );



if u r returning the pager itself from the ajax request & experience multiple post problem each time u click the link keep the 'live'=>false there.


Thanks Anupam for replying. I tried that but its no good.

The problem is that the javascript function that correlates with the ajaxlink stays the same even when the pager change, because the parameters are unique for each item, when i click it on the other pages (item 1 page 5 for example), it triggers the function of (item 1 page 1). So it only works for the first page.

registerScript also don't produce new script after the page is loaded and ajax is triggered (not when I tried it anyway).


This issue is critical for our project as we use ajax everywhere, and use ajax inside ajax, especially inside repeaters.
0

#5 User is offline   Anupam 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 90
  • Joined: 28-February 10
  • Location:INDB, India

Posted 14 March 2011 - 05:30 AM

View PostHussein, on 14 March 2011 - 05:16 AM, said:

Thanks Anupam for replying. I tried that but its no good.

The problem is that the javascript function that correlates with the ajaxlink stays the same even when the pager change, because the parameters are unique for each item, when i click it on the other pages (item 1 page 5 for example), it triggers the function of (item 1 page 1). So it only works for the first page.


i am running the pager in the same fashion with my custom portlets list view, all ajax, pager & each portlet data successfully.

can u post ur full code for the view in which u placed the CListView & these ajaxlink and also the renderPartial view u return for those ajax links.
0

#6 User is offline   zaccaria 

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

Posted 14 March 2011 - 06:46 AM

You fall in one of the few weak points of Yii.

I don't know a nice solution for this problem, but I can advice you to use my workaround.

As you noticed, the problem is that the new triggers are not set on the ajax link. The solution here is to use a normal link and "mask" as ajax:


echo CHtml::link(
            $img,        
            array('shout/ajaxTest'),
            array('onClick'=>' {'. CHtml::ajax(
 array(
                'update'=>'#req_res'.$data->id,
                'data'=>array('fileurl'=>$data->Url(),'imageurl'=>$data->getRecord('ImageUrl')),
                'type'=>'post',),
                        array('live'=>false, 'id'=>'shout-ajax-'.$data->id,)

).' return false; }'

), 
           
        );


This is just an example, in order to make it run you have to correct evenutally mistake.

The theory is to use CHtml::ajax for generate the js code for ajax, then attach this code like that:

'onClick'=> { //ajaxCode// return false;}

The return false will prevent the link from submit the page.
2

#7 User is offline   zaccaria 

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

Posted 14 March 2011 - 06:51 AM

btw: Hussein, welcome to the forum!

I come too from prado (just 1,5 years before you) and I can say that you did the right choice, you will find Yii very simple and powerfull.

The main developer of Yii, qiang, was the main developer of prado, so you will find lot of very familiar stuff.

Take a look here.

Just a question: why you did the change now? Why not before or after? Just curiosity
0

#8 User is offline   Hussein 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 01-March 11

Posted 14 March 2011 - 07:14 AM

View Postzaccaria, on 14 March 2011 - 06:46 AM, said:

You fall in one of the few weak points of Yii.

I don't know a nice solution for this problem, but I can advice you to use my workaround.

As you noticed, the problem is that the new triggers are not set on the ajax link. The solution here is to use a normal link and "mask" as ajax:


echo CHtml::link(
            $img,        
            array('shout/ajaxTest'),
            array('onClick'=>' {'. CHtml::ajax(
 array(
                'update'=>'#req_res'.$data->id,
                'data'=>array('fileurl'=>$data->Url(),'imageurl'=>$data->getRecord('ImageUrl')),
                'type'=>'post',),
                        array('live'=>false, 'id'=>'shout-ajax-'.$data->id,)

).' return false; }'

), 
           
        );


This is just an example, in order to make it run you have to correct evenutally mistake.

The theory is to use CHtml::ajax for generate the js code for ajax, then attach this code like that:

'onClick'=> { //ajaxCode// return false;}

The return false will prevent the link from submit the page.


Thanks zaccaria, it works !

Thanks Anupam for your help too :)
0

#9 User is offline   Hussein 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 01-March 11

Posted 14 March 2011 - 07:21 AM

View Postzaccaria, on 14 March 2011 - 06:51 AM, said:

btw: Hussein, welcome to the forum!

I come too from prado (just 1,5 years before you) and I can say that you did the right choice, you will find Yii very simple and powerfull.

The main developer of Yii, qiang, was the main developer of prado, so you will find lot of very familiar stuff.

Take a look here.

Just a question: why you did the change now? Why not before or after? Just curiosity


Thanks, I am glad I switched too ! :)

Well, when I first knew about Yii, it was still in 0.3 version, so it wasn't stable and mature at time for our project, and PRADO development didn't die completely.

Months ago we determined that most of our projects delays were to fix PRADO problems, but our project advanced too much to be rewritten using Yii, or that what we thought until we figured we can't keep fixing our bugs and prado's bugs at the same time, it was apparent that it was dead and no version 4 will be released anytime soon !

So we took the BOLD decision of rewriting our whole project using yii ! the similarities helped me migrate our logical and DB entities in few days, and now we are rewriting the interfaces, hopefully will finish by the end of this month.

I would be glad to post our project once ready in the examples section of yii, I think it would make a great portfolio item for the community :)

Sorry for the long reply, thanks for your help again :)
0

#10 User is offline   Luis Carlos Otte Junior 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 02-April 11
  • Location:Brazil

Posted 16 May 2012 - 08:56 PM

View Postzaccaria, on 14 March 2011 - 06:46 AM, said:

You fall in one of the few weak points of Yii.

I don't know a nice solution for this problem, but I can advice you to use my workaround.

As you noticed, the problem is that the new triggers are not set on the ajax link. The solution here is to use a normal link and "mask" as ajax:


echo CHtml::link(
            $img,        
            array('shout/ajaxTest'),
            array('onClick'=>' {'. CHtml::ajax(
 array(
                'update'=>'#req_res'.$data->id,
                'data'=>array('fileurl'=>$data->Url(),'imageurl'=>$data->getRecord('ImageUrl')),
                'type'=>'post',),
                        array('live'=>false, 'id'=>'shout-ajax-'.$data->id,)

).' return false; }'

), 
           
        );


This is just an example, in order to make it run you have to correct evenutally mistake.

The theory is to use CHtml::ajax for generate the js code for ajax, then attach this code like that:

'onClick'=> { //ajaxCode// return false;}

The return false will prevent the link from submit the page.



Work's me too, thanks ... :)

view.php
<?php
     $this->widget('zii.widgets.CListView', array(
        'dataProvider'=>$dataProvider,
         'itemView'=>'_view',
         'template'=>"{items} \n {pager} {summary}",
     'id'=>'video_list',
     'summaryText'=>'Página {page} de {pages}',
)); ?>



_view.php
<?php
        echo CHtml::link($conteudo, '#',
                    array('onClick'=>' {'.CHtml::ajax(
                        array(
                                'update'=>'#video-div',
                                'url'=>CController::createUrl('CadVideo/GetVideo&id='.$data->id),
                                'type'=>'GET',),
                                        array('live'=>false, 'id'=>$data->id,)

                                            ).' return false; }'

                        ));
?>

1

#11 User is offline   siva.g 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 05-March 12

Posted 29 November 2012 - 02:04 AM

Thanks zaccaria
0

#12 User is offline   Jose H. Milán 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 21-July 11
  • Location:España

Posted 20 February 2013 - 10:56 AM

Really usefull stuff, it saved me a lot of time.

Thank you, Zaccaria
0

#13 User is offline   Sarteg 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 28-November 13

Posted 28 November 2013 - 10:29 PM

View PostHussein, on 14 March 2011 - 07:14 AM, said:

Thanks zaccaria, it works !

Thanks Anupam for your help too :)


Same here! Been pulling my hair out for the past few days using ajaxLink widget.
This solution works beautifully!!!!
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