Sorting With Cgridview

I’m trying to setup a very simple table using CGridView. However, no matter what I do I cannot seem to make one of the columns in the table sortable.

Below is my code.

This is code inside the 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('keyword',$this->keyword,true);

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

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


        $sort = new CSort();

        $sort->attributes = [

            'id'=>[

                'asc'=>'id',

                'desc'=>'id desc'

            ],

            'nums'=>[

                'asc'=>'nums',

                'desc'=>'nums desc'

            ]

        ];

		

		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

            'sort'=>$sort,

			'pagination'=>array(

		        'pageSize'=>50,

		    ),

		

		));

	}



This is code inside the view which uses CGridView:




<?php 

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

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

    'columns' => array(

		array(

			'name' => 'id',

			'value' => '$data->id',

			'htmlOptions' => array('style' => 'text-align:center')

		),

		array(

			'name' => 'Keywords',

			'value' => '$data->keyword',

            'htmlOptions' => array('style' => 'text-align:center')

		),

		array(

			'name' => 'Total searches',

			'value' => '$data->nums',

            'sortable'=>true,

			'htmlOptions' => array('style' => 'text-align:center')

		),

		array(

			'name' => 'Search type',

			'value' => '$data->type',

			'htmlOptions' => array('style' => 'text-align:center')

		)	

	)

));


?>



I just want to get the nums column sortable, but I just can’t seem to get it working. The column is not even clickable.

Any idea what may be wrong?

Thanks!

Hi stinkytofu,

‘name’ property of a column must be exactly the same as the attribute name of the model. Use ‘header’ property instead to specify the column header text.




    'columns' => array(

        array(

            'name' => 'id',

            'htmlOptions' => array('style' => 'text-align:center')

        ),

        array(

            'name' => 'keywords',

            'htmlOptions' => array('style' => 'text-align:center')

        ),

        array(

            'name' => 'nums',

            'header' => 'Total searches',

            'htmlOptions' => array('style' => 'text-align:center')

        ),

        array(

            'name' => 'type',

            'header' => 'Search type',

            'htmlOptions' => array('style' => 'text-align:center')

        )	

    )



http://www.yiiframework.com/doc/api/1.1/CDataColumn