link in a CGridView

hello,

i’d like to insert a link in a CGridView in order to link my posts to their authors.

i tried with this code, but had no success:


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

	'id'=>'posts-grid',

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

	'filter'=>$model,

	'columns'=>array(

		//'id_posts',

		array(

                    'class'=>'CLinkColumn',

                    'label'=>'id_post_author',

                    'url'=>'users/view&id='.$model->id_post_author,

                    'header'=>'Author'

                    ),

		'date',

		'content',

		'title',

		'excerpt',

		/*

		'status',

		'url',

		'id_cliente',

		'immagine_fissa',

		'immagine',

		*/

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>

It looks like $model->id_post_author is always empty…

plus, but minor problem: this column can’t be searched… why?

thanks a lot

Try $data->id_post_author in place of $model… The model is a template for searching/filtering, but the dataProvider elements are called $data.

thanks a lot, but had no success.

looks like $data in this code is actually replaced by $model->search() than, indeed, returns:


return new CActiveDataProvider('posts', array(

			'criteria'=>$criteria,

		));

so i tried




<?php

var_dump($model->search()->data);

?>

and it is actually populated with data in my columns, but can’t find a way to access it:


$model->search()->data->id_post_author

is null.

Actually, i would like to do something even more complex: i’d like to link to the user page displaying user’s name, so i should also load the user model… is it probably too much? :D

thanks

I think what you require is to set the urlExpression property instead of the url property.

e.g :




 array(

        'class'=>'CLinkColumn',

        'label'=>'id_post_author',

        'urlExpression'=>'"users/view&id=".$data->id_post_author',

        'header'=>'Author'

      ),



Wow! thanks, the link now works like a charm…

And using LabelExpression i can also use the label… great. Do you have any idea how to make the column searchable?

thanks

I found that I had to use




'urlExpression'=>'Yii::app()->createUrl("topic/view",array("id"=>$data->topic_id))',



rather than




'urlExpression'=>'"topic/view&id=".$data->topic_id',



to get a working URL

The first gives: /index.php?r=topic/view&id=1

The second gives: /topic/view&id=1

Maybe the second works if you are using url rewrite

I’m having similar issues. I’m using the /views/song/admin.php (song is the name of my view) and using the created grid view. I wanted to insert a hyper link in the cell:




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

        'id'=>'song-grid',

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

        'filter'=>$model,

        'columns'=>array(

                'id',

                'title',

                array(

                        'class'=>'CLinkColumn',

                        'label'=>$model->filename,

                        'url'=>$model->url,

                        'header'=>'File name',




I’m finding that the $model values are all empty. How can I get the $model values from this grid view?




'label' => '$data->filename'



Hi Flavio -

I’ve tried $data and $row. I get this error:




Undefined variable: data



use labelExpression and urlExpression instead:




array(

    'class'=>'CLinkColumn',

    'header'=>'e-mail',

    'labelExpression'=>'$data->email',

    'urlExpression'=>'"mailto:".$data->email',

),



BINGO!! Thank you!!

One more question… I can’t sort with this new column nor can I do a search. How do I add the search/sort?

I found this link that helps me add the sort:

http://www.yiiframework.com/forum/index.php?/topic/9714-sort-user-defined-column-in-cgridview/

However I have attributes that I’ve added to the model that I want to sort. The sort only allows for actual table columns but I have attributes that are ADDED to the model. Is there a way to sort by those attribute values?

i´ve never used ClinkColumn before, to create links in a column i make this way (sorting works by default):




	array(

                'name'=>'column_name_from_model'

		'type'=>'raw',

		'header'=>'',

		'value'=>'CHtml::link($data->filename, $data->url, array("id"=>$data->id)',

        ),



Take a look at search function in your model class

Hi Guys, I tried my CGridView as your. but just now I have a problem. It is, the filter of the grid is not working. How could I solve it?

This is my view


<h1>Departments List</h1>

<div class="row">

Company<br />

	<?php			

    $records = Company::model()->findAll();

    $company_list = CHtml::listData($records, 'id', 'name');

    echo CHtml::dropDownList('company_id','', $company_list,

    array('prompt'=>'Please select a company',)); ?>

</div>


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

	'id'=>'department-grid',

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

	'filter'=>$model,

	'columns'=>array(

		 array(

                'name'=>'name',

                'type'=>'raw',

                'header'=>'Name',

                'value'=>'CHtml::link($data->name, $data->id)',

        ),

		//'id',

		'name',

		//'p_id',

		'created',

		'updated',

		//'company_id',

		array(            // display a column with "view", "update" and "delete" buttons

            'class'=>'CButtonColumn',

        ),

	),

)); 

?>


<?php


Yii::app()->clientScript->registerScript('search',

	"$('#company_id').change(function(){

	var companyId = $('#company_id option:selected').val();									 

	$.fn.yiiGridView.update(

	'department-grid',

	{ type: 'GET', 

	url: 'http://localhost/mmaig_ceo/ceo-control-system/index.php?r=department/dynamicdepartmentlist&ajax=department-grid&company_id=' + companyId

	

	}

	);

	});

")

?>

This is my model


public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('id',$this->id);

		$criteria->compare('name',$this->name,true);

		$criteria->compare('p_id',$this->p_id);

		$criteria->compare('created',$this->created,true);

		$criteria->compare('updated',$this->updated,true);

		$criteria->compare('company_id',$this->company_id);


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}



This is Controller


public function actionDepartmentlist()

	{

		$model=new Department('search');		

		$model->unsetAttributes();  // clear any default values		

		$model->p_id = 0;

				

		// $dataProvider->getData() will return a list of Post objects

		// $dataProvider=new CActiveDataProvider('Department');

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

			'model'=>$model,

		));

		

	}

Please help me

i added a link to gridView like this hope its usefull to someone ;


array(

                    'class'=>'CDataColumn',

                    'type'=>'raw',

                    'value'=>'CHtml::link($data->summary, array("../uploads/press/".$data->file_name),array("target"=>"_blank"))',

                    'name'=>'summary',

                    'header'=>_('Summary'),

                ),