widget and renderpartial?

I have a problem around rendering a widget with ajax.

A concrete example. It’s a webshop. I want a little widget into a corner to show the basket summary.(ex: $15.30 ).

This widget should be on the main layout.

But it needs to be updated with ajax also because the ‘put the items to the basket’ link is also an ajaxlink.

I hope you understand what I want to do and you can help me.

Thanks

Hi,

I have a similar problem but not the same. I’m using the gridview widget, and i’d like to use render partial (like render the update form on the same page filled with the proper data), if i could have the id from that point it wont be a problem at all i guess =)




<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'projecten-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(

		'id',

		'userid',

		'project_code',

		'name',

		'description',

		'visible',

		array(

			'class'=>'CButtonColumn',

			'updateButtonOptions'=>array('title'=>'',

				'ajax'=>array(

					'update'=>'dataform',

					'url'=>Yii::app()->createUrl("update",array("id"=>$data->id)),

					'dataType'=>'html',

					'type'=>'POST',

				),

			),

		),

	),

)); ?>



but i can not get there the id. if im seeing the source, i got this:




jQuery('#yt1').click(function(){jQuery.ajax({'url':'/index.php?r=update&id=projecten','dataType':'html','type':'POST','cache':false,'data':jQuery(this).parents("form").serialize(),'success':function(html){jQuery("dataform").html(html)}});return false;});



somebody has an idea whats happening with the $data variable, why cant i retrieve the information from it?

thx

i think you need to use partial views for that little ‘basket’ like need to define an action in your controller which update that basket(div, span, etc.), and also u need to define the ajax options for your link like this:

your view should look like this:




..

<tr><td>product info</td><td><?php

    echo CHtml::ajaxLink('ajaxbutton',

             CController::createUrl('putIntoBasket'),

		array (

			'type' =>'POST',

			'update' => '#basket',

                        'data' => $product_id

		),

		array(

			     'class' => 'btn_basket',

			     'title' => 'put into basket',

			     'id' => 'ajaxlink'.$i,

		),

	).$eol;

?></td></tr>

..



for your controller you need toi define an action like this




public funcion actionPutIntoBasket(){

..

retrieving the product info from the id

..

echo $new_basket_content;

}



and also you need to have a div or span or something what you update




<div id="basket">

..

</div>



i think that should do ;)

Thanks it was clear for me, but how can I initialize this ‘little basket’ in the main layout?

I want to init it at ‘body onLoad’.

Any suggestion?

There must be an action in your controller which handles the stuff when the "main layout" can be seen. In this action you have to also have to return the basket data as well.

like:




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

	'data1' => $data1,

	'basketdata' => $basketdata,


));



and in your view you can render it in the first time as a partial view:




<div id="basket">

    $this->renderPartial('_basket', $basketdata);

</div>



if i got ur problem right

and this would be initialized at the very first time when your page loaded in your main layout.

Thanks I think it’ll be ok for me.

I know it’s an old topic, but I had the same problem, for some reason there is no renderPartial method in the widget class. So I copied the renderPartial method from /framework/web/CController.php into my Widget class and everything worked perfectly. Maybe there are better solutions, but for me this was good enough.

it a little rough…

but u could just add function renderPartial in your widget




    public function renderPartial($view,$data=null,$return=false,$processOutput=false)

    {

        $this->controller->renderPartial($view, $data, $return,$processOutput);

    } 



Hello guys!

I have solved this in a very simple way.

I posted the full explanation on another post in the yii forum.

http://www.yiiframework.com/forum/index.php/topic/31576-ajax-and-widgets/page__gopid__232844#entry232844

Best,

Andres

I have done according Andreas post. But it make my boostrap.min.js do not work. do u know why? ???

i have spent 5 hours to fix it but still no way out. Any help pls…?