[SOLVED] CHtml:linkButton in cGridView

Hi

Thanks to forums I managed how to pass $_POST data from cListView using CHtml::linkButton to controller:

_view.php:




<?php echo CHtml::beginForm(); ?>

<?php echo CHtml::hiddenField('items_saved',$data->items_saved); ?>

<?php echo CHtml::linkButton($data->name, array('submit' => array('showItemsSaved'))); ?>

<?php echo CHtml::endForm(); ?>	



Then in controller I have:




public function actionShowItemsSaved()

{	

	$items_saved=$_POST['items_saved'];

	$dataProvider=new CActiveDataProvider('ShowItems',array(

	'criteria'=>array(

	'condition'=>'id IN ('.$items_saved.')',

	),

	));

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

	'dataProvider'=>$dataProvider,

	));

	}



And it works OK.

Now I would like to attach the same CHtml::linkButton (to call controller method and $_POST some form hidden data) to cGridView’s column for example:




<?php echo CHtml::beginForm(); ?>

<?php echo CHtml::hiddenField('items_saved',$data->items_saved); ?>

<?php echo CHtml::endForm(); ?>	


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

	'id'=>'saved-items-grid',

	'dataProvider'=>$dataProvider,

	'filter'=>$model,

	'ajaxUpdate'=>false,

	'columns'=>array(

		array(

                'name'=>'name',

                'header'=>'Name',

                'value'=>CHtml::linkButton(name, array('submit' => array('showItemsSaved'))),

	        ),

	),

));

?>



But I get error in the column ‘name’:

Parse error: syntax error, unexpected ‘<’ in /yiiapp/framework/base/CComponent.php(616) : eval()'d code on line 1

Any suggestions how to display CHtml::linkButton properly?

If there is also possibility to make CHtml::linkButton the whole row I am open to this proposition.

Thanks

Value must be a string with PHP expression that later will be evaluated by widget:


'value'=>'CHtml::linkButton("name", array("submit" => array("showItemsSaved")))',

P.S. read manuals and documentation closer.

After your suggestion now the column’s values are:

Could you point me to this particular documentation which says that value must be string here?

You also need to set ‘type’ => ‘html’ or ‘type’ => ‘raw’ in your column’s configuration.

About value you may read here:

Thanks now it works. But I have a problem where to put <form> and <hiddenField> with some value so CHtml::linkButton submits it further.

When it is this way (linkButton outside the form):




<?php echo CHtml::beginForm(); ?>

<?php echo CHtml::hiddenField('items_saved',$data->items_saved); ?>

<?php echo CHtml::endForm(); ?> 


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

        'id'=>'saved-items-grid',

        'dataProvider'=>$dataProvider,

        'filter'=>$model,

        'ajaxUpdate'=>false,

        'columns'=>array(

                array(

                'name'=>'name',

                'header'=>'Name',

                'value'=>CHtml::linkButton(name, array('submit' => array('showItemsSaved'))),

                ),

        ),

));

?>



Or this way (CHtml::linkButton inside cGridView inside <form>):




<?php echo CHtml::beginForm(); ?>

<?php echo CHtml::hiddenField('items_saved',$data->items_saved); ?>


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

        'id'=>'saved-items-grid',

        'dataProvider'=>$dataProvider,

        'filter'=>$model,

        'ajaxUpdate'=>false,

        'columns'=>array(

                array(

                'name'=>'name',

                'header'=>'Name',

                'value'=>CHtml::linkButton(name, array('submit' => array('showItemsSaved'))),

                ),

        ),

));

?>


<?php echo CHtml::endForm(); ?> 



I get

[size="5"]Internal Server Error[/size]

I have no idea how to enclose hiidenField using cGridView. Any suggestions?

Can you describe what do you want to get as a result?

My current solution is built cListView. On of the columns of this cListView is CHtml::linkButton which nicely posts CHtml:hiddenField to controller:

This is _view.php:




<?php echo CHtml::beginForm(); ?>

<?php echo CHtml::hiddenField('items_saved',$data->items_saved); ?>

<?php echo CHtml::linkButton($data->name, array('submit' => array('showItemsSaved'))); ?>

<?php echo CHtml::endForm(); ?> 



And in controller method actionShowItemsSaved() I can read $POST[‘showItemsSaved’] and use it later for $dataProvider condition to render new view (you can look on upper posts).

I wanted to switch to cGridView instead of cListView to be able to search/filter.

Using:




'value'=>'CHtml::linkButton(CHtml::encode($data->items_saved), array("submit" => array("showItemsSaved")))',



displays CHtml::linkButton correctly in column, but when I click on it I get 500 error.

I think the $_POST data is not sent to controller.

How do I know this?

When I build dataProvider in controller method I use:




$items_saved=$_POST['items_saved'];

$dataProvider=new CActiveDataProvider('ShowItems',array(

'criteria'=>array(

'condition'=>'id IN ('.$items_saved.')',

),

));



It works for cListView but not for cGridView (I don’t know where to put form and hidden field since linkButton is in Grid…)

When I put statically any existing IDs in condition:




$items_saved=$_POST['items_saved'];

$dataProvider=new CActiveDataProvider('ShowItems',array(

'criteria'=>array(

'condition'=>'id IN (1000001,1000002)',

),

));



It works OK for cGridView but I want to pass this data using $_POST from previous screen.

You need to post items that user selects in grid or not?

I need to post something that is not in cGridView (but CHtml::linkButton is there) since I don’t know how to put form and hidden field close to CHtml::linkButtons label…

I hope this would work I think:




'value'=>'CHtml::linkButton($data->items_saved.<form><hidden_field value=$saved_items>, array("submit" => array("showItemsSaved")))',



Off course concatenating will not work that way.

I think I know the problem, I was analyzing HTML source and I found that putting hiddenField outside the grid is not working:




<?php echo CHtml::hiddenField('items_saved',$data->items_saved); ?>



HTML source:

<form method="post"><input type="hidden" value="" name="items_saved" id="items_saved" /></form>

I think this is because $data->items_saved is not calling to any dataProvider as it is initiated in the grid…

I got an idea and question to you…

How can I assign this saved_items values to hidden field?

This is before cGridView




<?php echo CHtml::beginForm(); ?>

<?php echo CHtml::hiddenField('items_saved',JS_transferred_data.....); ?>

<?php echo CHtml::endForm(); ?> 



I need to write onClick funtion which will assign saved_items values to hiddenField? So when user clicks in ‘Name’ column some script will assign saved_items to hiddenField and then CHtml::linkButton will work fine I think…

You can pass additional data in such way:


'value' => 'CHtml::linkButton("name", array("submit" => array("showItemsSaved"), "params" => array("items_saved" => $data->items_saved)))',

Yeah :rolleyes: no need to use <form> and hiddenField.

Thanks!

Anyway to learn all this. I was watching Yii reference on CHtml::linkButton and this "params" array that you attached is part of htmlOptions? (CHtml::linkButton takes 2 arguments: label and htmlOptions accroding to reference)

could you explain?

linkButton’s descriptions has an advise to see also clientChange method. There you can find all this options.