[Solved]: New member question: AjaxLink inside a CListView

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 !

No one can help a new member here ? :)

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.

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.

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.

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 zaccaria, it works !

Thanks Anupam for your help too :)

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 :)

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; }'


                        ));

?>



Thanks zaccaria

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

Thank you, Zaccaria

Same here! Been pulling my hair out for the past few days using ajaxLink widget.

This solution works beautifully!!!!