Cgridview Data Sort, By Clicking On The Column Names

So,I have my CGridviews, a couple of them, all working and displaying nicely.

The problem is, now i want to enable a user to sort by a certain property, by clicking on the column name.

I have tried to read around, but nothing seems to come close to what i want to achieve.

Here is the code for generating the data provider i use for one of the gridviews


<?php	

	$command = Yii::app()->db->createCommand();	

	$staff1 = $command->select('project.id,project.name, project.start_date, project.end_date, project.project_ref AS project_ref , technical.surname AS Technical, sales.surname AS ARM, sm.surname AS  Manager,project.duration,project.status')

	->from('project')

	->leftjoin('staff technical','project.staff1=technical.id')

	->leftjoin('staff sales','project.staff2=sales.id')

	->leftjoin('staff sm','project.project_manager=sm.id')

	->order('name')

	->queryAll();

	$provide= new CArrayDataProvider($staff1, array(

		

	)); ?>

You can see i order them by default, according to the "name" property.

Then I display the data from the query with a gridview




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

	'dataProvider'=>$provide,

	'columns'=>array(

					'project_ref'=>array('name'=>'Project Ref', 'value'=>'$data["project_ref"]'),

					'name'=>array(

								'name'=>'Project Name', 

								'type'=>'raw',

							    'value'=>'CHtml::link($data["name"], array("project/view", "id"=>$data["id"]))',),

					'Technical'=>array('name'=>'ARM', 'value'=>'$data["Technical"]'),

					'ARM'=>array('name'=>'Technical', 'value'=>'$data["ARM"]'),

					'Manager'=>array('name'=>'Manager', 'value'=>'$data["Manager"]'),			

					'start_date'=>array('name'=>'Started', 'value'=>'$data["start_date"]','htmlOptions'=>array('style' => 'text-align: right;')),

					'duration'=>array('name'=>'Duration(Weeks)', 'value'=>'$data["duration"]'),

					'status'=>array('name'=>'%age ', 'value'=>'$data["status"]'),					

				 ),

		

	

)); ?>

Now i want, when a user clicks on the column name, the items in the grid view, are sorted by that column.

Thank

For what I can say quickly is that in your search action (in your Model),

you added


$command->order('name')

. This overwrites the

request of the user to order on another attribute.

Therefore, it might be more useful to use the defaultOrder:




$dataProvider=new CActiveDataProvider($staff1, array(

  'sort'=>array(

    'defaultOrder'=>array(

      'title'=>false

    )

  )

));

Here, false is ASC and true is DESC [See this post.]

By using an array in defaultOrder instead of ‘title ASC’, the sort icon should also appear.

Hope this helps.

Thanks for the suggestion,I have tried this, and it doesn’t seem to provide the results that i need. In the code snippet you have provided, you have used CAtiveDataProvider, and in my code, I used CArrayDataProvider.Could this be the reason?

Secondly, should the "title" attribute be on of the fields i want to sort? I have changed this too, but it still yields no results.

Thanks

Dear Skapeyi,

in the pre-generated code of Yii the Gridviews always use the CActiveDataProvider, so probably this might be the case as an ArrayDataProvider might only return an array with results and the ActiveDataProvider also provides the possibility to actively go through the data. This is because the ArrayDataProvider (as does the SQLDataProvider) returns an array and the CActiveDataProvider provides an object [See Forum Topic].

Thus probably it does work if you change the DataProvider. Next, the ‘title’ attribute should indeed be an attribute of the model used.

Hope this provides you with all information required.

The icons for sorting don’t show up. I thought it was a theme problem, so i disabled the theme, but still, it doesn’t provide the required results. >:(

But they do when you sort on another attribute, right? Thus via a user event?

In my opinion it is not necessary for the order icon to show on page load,

but only when the user interact with the system.

If you think this is a requirement, I would suggest to report it as bug?

I am doing research on this. Will let you know the outcomes.