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'
))
));
}
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.
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,
));
}