Make Row Of Cgridview Become A Link

Hello,

I want to make each row in my CGridview become a link so I can get rid of the CButtonColumn.

I have tried this solution I found in another post




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

    ...

    'htmlOptions'=>array(style=>'cursor: pointer;'),

    'selectionChanged'=>"function(id){window.location='" . Yii::app()->urlManager->createUrl('controller/action', array('id'=>'')) . "' + $.fn.yiiGridView.getSelection(id);}",

    ...

)); ?>



The problem is that I don’t want the id(in the url) to be the primary key, I want to use another field in the database to be sent in the URL, but that does not seem to work.

Is there a fix in the solution I found or do I need another solution?

To get proper and better help… when you say "that does not seem to work" you need to explain what did you try, what did you expect and what happened…

The above code uses the getSelection() JS method to get the selected PKs… how did you get the values you want to send instead of them?

You may try this.


                'columns'=>array(

                    ...

                    array(

                        'header'=>'Header Name',

                        'name'=>'column_name',

                        'type'=>'raw',

                        'value'=>'CHtml::link($data->column_name,Yii::app()->createUrl("ercGen/update",array("id"=>"$data->pk")))',

                    ),

                    ...

Sorry if I explained it bad.

I want a whole row in the CGridView to become a link and send me to the same place as the view-button in CButtonColumn would. So I need to wrap each row in a link and in some way echo out the the url to that place.

When I use the code in my example above it does make each row a link, the problem is that it grabs the primary key from the db-table and puts that in the URL. I want to choose another db-field and put that in the URL instead.

Example:

this is how the url looks with the solution I found:

http://www.mywebsite.com/index.php?r=blog/view&id=11402

but I want it to look like this instead:

http://www.mywebsite.com/index.php?r=blog/view&name=abc123

Have you considered using a listview instead? It may be easier to customize your needs.

Has the listview slower performance then the gridview? the db-table contains over 100 000 rows. Also, is the listview easy to configure with search-functionality?

In terms of overhead, they’re the same - a single JS file. The only thing to slow the list view down is the complexity/details you add to it - extra css files, large images, complex JS computations etc.

I’ll assume you’re using pagination to display your grid/list - then there shouldn’t be any difference.

The search functions in the same way as the grid - IDataProvider.

Here’s a nice implementation of a listview - http://www.stay.com/new-york/attractions.

Matt

Thank you for the help! I guess I will switch to CListView then!

If you need to display tabular data, CGridView is a better option. With CListView, you would have to build the table by yourself.

If someone still knows the answer please share it with us.

Changing the code to


$.fn.yiiGridView.getSelection(field_different_than_id)

doesn’t seem to work.