Chtml::link In Cgridview

Hello,

yii version is yii-1.1.14

I know this is very simple and posted many places, but I can’t get it done. I have tried anything to get it displayed but the $model->CallNumber just return null.

         array(


	    'header'=>'Call #',


                'name'=>'CallNumber',


                'value'=>"CHtml::link('$model->CallNumber)','test')",


	    'type'=>'raw',


		),

Please help

how about this




'value'=>"CHtml::link($data->CallNumber),'test')",



p.s. I would assume the above is CGridView

Thank you for your reply. It is in gridview. And looks like you have a typo, missing an open ( . But it still show in null.

‘value’=>“CHtml::link(($data->CallNumber),‘test’)”,

Should be:




'value'=>"CHtml::link($data->CallNumber, 'url')",



and make sure $data->CallNumber is not empty.

try this for e.g


'value'=>'CHtml::link($data["available_artist"] ? \'active\': \'inactive\' ,"javascript:void(0);",array("class"=>"status11","onclick"=>"aritstsetchanges({$data["id"]})"))' ,

also simple


array(

            	'name'=>'url',

            	'value'=>'CHtml::link($data->url, $data->url, array(\'target\' => \'_blank\'))',

            	'type'=>'raw',

        	),

I hope it’s some help

Try this


array(

	'header'=>'Call #',

	'value'=>'(($model->CallNumber!='')?(CHtml::link($model->CallNumber,"url-link"))<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' />"Not available"))',

	'type=>'raw',

)

Thank you all for your help, but none working. If there is no syntax error, then the $model->CallNumber return null, even though the same $model->CallNumber used in different column shows data. That is what really bugging me. I tried \ escape the $ ,but still no result. Any thoughts please.

It should be $data->CallNumber, not $model->CallNumber. Does it work with other fields, like $data->id?

Yes it does. ($model or $data not mater here, in my code it is $model, but the others helped me used $data)

if doing this:

       array(


	    'header'=&gt;'Call #',


                'name'=&gt;'CallNumber',


                'value'=&gt;&#036;model-&gt;CallNumber,


             )

the value will show in column. But if adding “CHtml::link(’$model->number’,’$myurl’)” it just show null.

If I change to use class ‘class’=>‘CLinkColumn’, and urlExpression , same thing happen. By the value of $model->CallNumber does not show.

Also, any field will be the same if used in this link creation.

It DOES matter.

With this code:




'value'=>$model->CallNumber,



all grid rows will display the same value ($model->CallNumber)

With this code:




'value'=>'$data->CallNumber'



each row will contain a CallNumber of a corresponding model. I did a mistake in my first post. There should be single quotes, because this statement will be executed per each row later (using eval(), where a row model is represented by $data variable). Hope it helps :)

thanks Andy,

I just can’t get it going. Why $data->CallNumber show null? If put at another place it shows.

What is another place? Can you post a complete code of that grid or whole view file?

thanks,

this is the controller:




public function actionAdmin()

	{

		

		$model=new Callinfo('search');

		$model->unsetAttributes(); 

		if(isset($_GET['Callinfo']))

			$model->attributes=$_GET['Callinfo'];

		

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

			'model'=>$model

		));

	}




and this is the view:




<?php

/* @var $this CallinfoController */

/* @var $model Callinfo */


$this->breadcrumbs=array(

	'Callinfos'=>array('index'),

	'Manage',

);


$test = '123';

$myurl = 'thisistheone';

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

	'id'=>'callinfo-grid',

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

	'filter'=>$model,

	'columns'=>array(

		

		array(

		    'header'=>'Call #',

            'name'=>'CallNumber',

            //'value'=>$model->CallNumber,   // THIS WILL SHOW DATA

			//'value' => "CHtml::link('$myurl','$test')",

			'value' => "CHtml::link('$model->CallNumber','$test')", // THIS RETURN NULL

			'type' => 'raw',

			),

		array(

			'class'=>'CLinkColumn',

			'label'=>'viewCall',

			'urlExpression'=>"callinfo/view/id='$model->CallNumber",

			'header'=>'Author'

			),

		array(

		    'header'=>'Status',

            'name'=>'CallStatus',

            'value'=>$model->CallStatus,

			'filter'=>$model->callstatus()

			),

		

	),

)); ?>




http://www.yiiframework.com/doc/api/1.1/CDataColumn#value-detail

If you have PHP 5.3+, then I hope anon functions will help you faster to understand how grid work:




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

        'id'=>'callinfo-grid',

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

        'filter'=>$model,

        'columns'=>array(

                

                array(

                    'header'=>'Call #',

                    'name'=>'CallNumber',

                    'value'=>function($data) { return CHtml::link($data->CallNumber, 'link'); },

                    'type' => 'raw',

                ),

                array(

                    'class'=>'CLinkColumn',

                    'label'=>'viewCall',

                    'urlExpression'=>function($data) { return 'callinfo/view/id='.$data->CallNumber; },

                    'header'=>'Author'

                ),

                array(

                    'header'=>'Status',

                    'name'=>'CallStatus',

                    'value'=>function($data) { return $data->CallStatus; },

                    'filter'=>$model->callstatus()

                ),

                

        ),

)); ?>



thank you Andy. That is the one that I was suspecting, the version of either php or yii, but not getting to the right documents. I was working with yii on and off (mostly off) for last 2 years, and did not get my knowledge updated.

Thanks again.