Sort user defined column in CGridView

I have a data model I want to present with a "grid view", I borrow some data from another table so I have to define the columns manually:


	'columns'=>array(

		array(

			'header' => 'Kundnummer',

			'name' => 'aux_kundnummer',

			'filter' => CHtml::activeTextField($model, 'aux_kundnummer'),

			'value' => '$data -> kund -> Kundnummer',

		),

                ...

I want that column to be sortable how can I do that?

Tried to add the ‘sortable’ => true to the array list but that don’t do it.

You have to set the property "attribute" of $dataProvider->sort

How do you mean? Show me some code plz.

This is the widget code:


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

	'id'=>'abonnemang-grid',

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

	'filter'=>$model,

	'columns'=>array(

                ...

	),

)); ?>





<?php 

    $dataProvider=$model -> search()

    $dataProvider->sort->attributes=array(

        'all other field',

        'kund.kundnummer',

    );

    $dataProvider->criteria->with('kund');

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

        'id'=>'abonnemang-grid',

        'dataProvider' => $dataProvider,

        'filter'=>$model,

        'columns'=>array(

                ...

        ),

)); ?>



The title of the column becomes a link if everything look like this:


<?php

	$dataProvider = $model -> search();

	$dataProvider -> sort -> attributes = array(

		'kund.Kundnummer',

	);


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

		'id'=>'abonnemang-grid',

		'dataProvider' => $dataProvider,

		'filter' => $model,

		'columns'=>array(

                        ...


			array(

				'name' => 'kund.Kundnummer',

			),


                        ...

		),

	));

?>

But the link does not sort the list so it’s of no use. And now that field is NOT searchable. If I want it to be searchable by the ‘kund.Kundnummer’ column, the code need to be changed to this:


<?php

	$dataProvider = $model -> search();

	$dataProvider -> sort -> attributes = array(

		'kund.Kundnummer',

	);


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

		'id'=>'abonnemang-grid',

		'dataProvider' => $dataProvider,

		'filter' => $model,

		'columns'=>array(

                        ...


			array(

				'header' => 'Kundnummer',

				'name' => 'aux_kundnummer',

				'filter' => CHtml::activeTextField($model, 'aux_kundnummer'),

				'value' => '$data -> kund -> Kundnummer',

			),


                        ...

		),

	));

?>

But now the title is not even a link…

Anyone???

hi

maybe this help you … or not :(

in the model





	public function search()

	{




  $sort = new CSort();

  $sort->attributes = array(


   'date'=>array(

      'asc'=>'t.date',

      'desc'=>'t.date desc',

    ),


   'name'=>array(

      'asc'=>'t.name',

      'desc'=>'t.name desc',

    ),


   'id_sector'=>array(

      'asc'=>'sector.descripcion',

      'desc'=>'sector.descripcion desc',

    ),

);





		$criteria=new CDbCriteria;


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


		$criteria->compare('t.date',$this->date,true);


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


		$criteria->compare('sector.descripcion',$this->id_sector,true);




		return new CActiveDataProvider('Ticket', array(

			'criteria'=>$criteria,

                        'sort'=>$sort,

		));

	}






in the view





<div class="search-form" style="display:none">

<?php $this->renderPartial('_search',array(

	'model'=>$model,

)); ?>

</div><!-- search-form -->


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

	'id'=>'ticket-grid',

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

	'filter'=>$model,

	'columns'=>array(

		'date',

		'name',

                array('name'=>'id_sector','sortable'=>'true','value'=>'$data->sector->descripcion'),


		//'id_sector',

		

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>






THANKS! It helped me out, did like this:

In the search() method in the model


		$sort = new CSort;

		$sort -> attributes = array(

			'AktivStatus' => array(

				'asc'=>'AktivStatus',

				'desc'=>'AktivStatus desc',

			),

		

			'aux_kundnummer' => array(

				'asc'=>'kund.Kundnummer',

				'desc'=>'kund.Kundnummer desc',

			),

		);


		return new CActiveDataProvider('Abonnemang', array(

			'criteria'=>$criteria,

			'sort' => $sort,

		));

In the view


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

		'id'=>'abonnemang-grid',

		'dataProvider' => $dataProvider,

		'filter' => $model,

		'columns'=>array(

			array(

				'header' => 'Kundnummer',

				'name' => 'aux_kundnummer',

				'filter' => CHtml::activeTextField($model, 'aux_kundnummer'),

				'value' => '$data -> kund -> Kundnummer',

			),

			

			'AktivStatus',

			

			array(

				'class'=>'CButtonColumn',

			),

		),

	));