CGridView sorting problem

Hi all,

First of, congratulations to the developers of this fantastic framework. This is my first time to actually enjoy programming with PHP, and Yii is quite addictive to use!

Now with my first question, I am having an issue with using CGridView. I am amazed how this simple widget can do the sorting and pagination, but when I use relationships to render the desired values, sorting becomes impossible because the header becomes unclickable. :(

This is my sample code:




<?php

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

        'id' => 'users-grid',

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

        'columns' => array(

            array(

                'name' => 'last_name',

                'value' => 'Profiles::fullName($data->id)'

            ),

            array(

                'name' => 'gender',

                'value' => 'Lookup::item("gender",$data->profiles->gender)'

            ),

            'role',

            array(

                'name' => 'municipality',

                'value' => 'Municipalities::model()->findByPk($data->profiles->present_municipality_id)->name'

            ),

             array(

                'name' => 'province',

                'value' => 'Provinces::model()->findByPk($data->profiles->present_province_id)->name'

            ),

        ),

    ));

?>



I have a very strong feeling that this issue has been already brought out to this very forum…it might be I’m that unfortunate to find that thread or I just don’t know what and where to look. Either way, I hope someone can shed some light regarding my issue.

Thanks in advance. :)

Hi emc,

Welcome to the forum!

Yes you are correct, this has been discussed a lot of times :)

link 1

link 2link 3

and so on…

Let me use your case as our example. In your case, you have to add ‘sort’=>true each columns that you want to be sortable. So your view would look like:




<?php

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

        'id' => 'users-grid',

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

        'columns' => array(

            array(

                'name' => 'last_name',

                'value' => 'Profiles::fullName($data->id)',

                'sortable'=>true

            ),

            array(

                'name' => 'gender',

                'value' => 'Lookup::item("gender",$data->profiles->gender)',

                'sortable'=>true

            ),

            'role',

            array(

                'name' => 'municipality',

                'value' => 'Municipalities::model()->findByPk($data->profiles->present_municipality_id)->name',

                'sortable'=>true

            ),

             array(

                'name' => 'province',

                'value' => 'Provinces::model()->findByPk($data->profiles->present_province_id)->name',

                'sortable'=>true

            ),

        ),

    ));

?>



And in your model->search(), add the sorting. For example with gender




$sort = new CSort();

$sort->attributes = array(

    'gender' => array(

                'asc' => 'profiles.gender',

                'desc' => 'profiles.gender desc',

            ),

    //add here your other columns to be sorted

)


$criteria = new CDbCriteria;

$criteria->with = array('profiles');

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

......


return new CActiveDataProvider(get_class($this), array(

            'criteria' => $criteria,

            'sort'=>$sort

        ));



Something like that.

Hi emc,

Is your problem already solved?

If things doesn’t work as expected, try adding $criteria->together in your search statement, okay?

Enjoy exploring Yii!