Sorting In List View

In the CList view of Yii, i added sorting attributes but it shows as links. How it convert to drop down list (attributes shown in drop down) shown in view?

Controller


public function actionIndex()

	{

		$dataProvider=new CActiveDataProvider('Student');

		$this->render('index',array(

			'dataProvider'=>$dataProvider,'sort'=>array(

                        'attributes'=>array(

                             'address','name'

                        ))

		));

	}

index.php


<h1>Students</h1>


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

	'dataProvider'=>$dataProvider,

	'itemView'=>'_view','sortableAttributes'=>array(

'name',

'address',


),

)); ?>



_view.php


<div class="view">


		<b><?php echo CHtml::encode($data->getAttributeLabel('name')); ?>:</b>

	<?php echo CHtml::encode($data->name); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('address')); ?>:</b>

	<?php echo CHtml::encode($data->address); ?>

	<br />




</div>

Dear Friend

I am not able to decipher your exact requirement.

Let us make a dropDown of attributes at the top.

Based on selection from the dropDownList , we are going to display one link for sorting.

Let us have a model: Member..

CONTROLLER.




public function actionIndex()

	{   $model=new Member;

	    $attributes=array_keys($model->attributes);

	    isset($_GET['sortAttr'])?$sortAttr=array($_GET['sortAttr']):$sortAttr=array();//sortAttr is the name attribute of DropDownList.

	    $dataProvider=new CActiveDataProvider('Member');

	    $this->render('index',array(

		'dataProvider'=>$dataProvider,

		'attributes'=>$attributes,

		'sortAttr'=>$sortAttr,

		));

	}



VIEW




//MAKING A DROPDOWNLIST

echo 

CHtml::dropDownList('sortAttr','',array_combine($attributes,$attributes),array('id'=>'sort','prompt'=>'select a Field'));


//THE WIDGET WITH ID AND DYNAMICALLY MADE SORTABLEATTRIBUTES PROPERTY


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

        'id'=>'member-list',

	'dataProvider'=>$dataProvider,

	'itemView'=>'_view',

	'sortableAttributes'=>$sortAttr,

));


//THE SCRIPT TO UPDATE THE GRID AFTER MAKING CHANGES IN DROPDOWNLIST.


Yii::app()->clientScript->registerScript('sort','

$("#sort").change(function()

        {	

	    $.fn.yiiListView.update("member-list",{data:{sortAttr:$(this).val()}})	

	});

');



We have to make appropriate changes in CSS for correct placement of dropDown.

Regards.

Thanks… :)

But in this code, the drop list contain all fields of the corresponding table. How can it customise? Only selected fields in drop list for sorting?

You can construct the needed array in the following way.




//ONLY ID AND NAME ATTRIBUTES ONLY CHOSEN.

echo CHtml::dropDownList('sortAttr','',array('id'=>'ID','name'=>'Name'),array('id'=>'sort','prompt'=>'select a Field'));



CONTROLLER




public function actionIndex()

{   

     isset($_GET['sortAttr'])?$sortAttr=array($_GET['sortAttr']):$sortAttr=array();//sortAttr is the name attribute of DropDownList.

     $dataProvider=new CActiveDataProvider('Member');

     $this->render('index',array(

	'dataProvider'=>$dataProvider,

	'sortAttr'=>$sortAttr,

		));

}



Thanks…its works fine.

Have another doubt. Now the sorting may be Ascending or Descending order. Can we set the sorting order for all fields?

In the following example we are configuring sorting parameters for id and name.

CONTROLLER.




public function actionIndex()

	{  

	    isset($_GET['sortAttr'])?$sortAttr=array($_GET['sortAttr']):$sortAttr=array();


	    $dataProvider=new CActiveDataProvider('Member',array(

		'sort'=>array('attributes'=>array(

		    'id'=>array('asc'=>'id ASC','desc'=>'id DESC','default'=>'desc'),

		    'name'=>array('asc'=>'name ASC','desc'=>'name DESC','default'=>'asc'),

		),'defaultOrder'=>'id DESC'),

		));


		$this->render('index',array(

			'dataProvider'=>$dataProvider,

			'sortAttr'=>$sortAttr,

		));

	}



The following link may be useful

CSort::attributes