Please Help On Widget Per Database Row.

Hi All,

I hope someone can help. I am new to Yii and have been doing it for only a month. Best way to learn is to jump in and do.

I created a widget which will allow the user to enter his email and a comment to be submitted.

I then display a button on each row on my view page. When I have 1 record in the database, it works 100%, but anything more than 1 row, it breaks.

This is what I have done:




<div class="view">


	<div style="width: 100%; overflow: hidden;">

	    <div style="width: 700px; float: left;"><font size="3"><b><?php echo CHtml::encode($data->title); ?></b></font></div>

	    <div style="float: right;">

	    <?php

	    $this->widget('zii.widgets.jui.CJuiButton', array('buttonType'=>'button',

	    											'name'=>'btnContact',

	    											'caption'=>'Contact Poster', 

	    											'htmlOptions'=>array('onclick'=>'$("#postercontactwidget").dialog("open"); return false;')));

	    $this->widget('PosterContactWidget');	    

	    ?>

	    </div>

	</div>



My guess is that because the same thing/widget code gets generated for each row returned by the dataprovider.

Just not sure if my approach is totally wrong or am I just missing something?

I need it for each row as I am going to send the row id to the widget for further data processing.

Any help is much appreciated.

Thank You.

Hi

Are you using different id for different rows… it is important

Thanks chandran, I see my problem,

Each widget must also be created with its own id. So how do I set that id from my view?

I used this:


$this->widget('PosterContactWidget',array('id'=>'postercontactwidget'.$data->id));

And my widget code:




<?php 

   $this->beginWidget('zii.widgets.jui.CJuiDialog', array(

//     'id'=>'postercontactwidget'.$data->id,

     'cssFile'=>'jquery-ui-1.8.7.custom.css',

     'theme'=>'redmond',

     'themeUrl'=>Yii::app()->request->baseUrl.'/css/ui',

     'options'=>array(

         'title'=>'Contact Poster',

         'autoOpen'=>false,

         'modal'=>true,

         'width'=>480,

     	 'height'=>315,

     ),

   ));

?>



Now it is automatically setting an id for each widget with the name ‘yw1’, ‘yw2’, etc…

This is also not correct. The part $data->id return the primary key id from the database. The id can be anything.

So I need for this to work:


$this->widget('PosterContactWidget',array('id'=>'postercontactwidget'.$data->id));

but it seems to have no effect.

What am I doing wrong?

Did you put “<?php $this->endWidget(‘zii.widgets.jui.CJuiDialog’);?>” at end?

If you first one is working, and not working on the 2nd one, it’s mostly because you did not close the previous dialog.

You set ‘autoOpen’=>false, then you need code to open the dialog




'onclick'=>'$("#mydialog").dialog("open"); return false;',



and also close that dialog




$this->beginWidget('zii.widgets.jui.CJuiDialog', array(

'id'=>'publishDialog',

// additional javascript options for the dialog plugin

'options'=>array(

    'title'=>'Dialog Title',

    'autoOpen'=>false,

    'buttons' => array(

        array('text'=>'Route','click'=> 'js:function(){'.$target.'}'),

        array('text'=>'Cancel','click'=> 'js:function(){$(this).dialog("close");}'),

    ),

),

));



Thank You,

I got it working by doing this:





<div class="view">


	<div style="width: 100%; overflow: hidden;">

	    <div style="width: 700px; float: left;"><font size="3"><b><?php echo CHtml::encode($data->title); ?></b></font></div>

	    <div style="float: right;">

	    <?php

		    $this->widget('zii.widgets.jui.CJuiButton', array('buttonType'=>'button',

		    											'name'=>'btnContact'.$data->id,

		    											'caption'=>'Contact Poster', 

		    											'htmlOptions'=>array('onclick'=>'$("#postercontactwidget'.$data->id.'").dialog("open"); return false;')));

		    

		    $this->beginWidget('zii.widgets.jui.CJuiDialog', array(

		    	 	'id'=>'postercontactwidget'.$data->id,

		    		//'cssFile'=>'jquery-ui-1.8.7.custom.css',

		    		'theme'=>'redmond',

		    		'themeUrl'=>Yii::app()->request->baseUrl.'/css/ui',

		    		'options'=>array(

		    				'title'=>'Contact Poster',

		    				'autoOpen'=>false,

		    				'modal'=>true,

		    				'width'=>480,

		    				'height'=>315,

		    		),

		    ));

		    

		    $this->widget('PosterContactWidget');

			$this->endWidget('zii.widgets.jui.CJuiDialog');

		?>

	    </div>

	</div>



Good to know you solved the issue. :)