if you use a CListView and have too many sortable attributes, a Dropdown-List is much more user-friendly.
This can be "easy" achieved by extending CListView like in my little dirty hack here:
Yii::import('zii.widgets.CListView');
class CustomListView extends CListView
{
public function renderSorter()
{
if($this->dataProvider->getItemCount()<=0)
return;
$link = Yii::app()->controller->createUrl('');
$modelname = $this->dataProvider->modelClass;
$attributes = array();
foreach($this->sortableAttributes as $name=>$label)
$attributes[$label] = $this->dataProvider->model->getAttributeLabel($label);
echo '<div style="float: right;">';
echo 'Sort by: ';
if(isset($_GET[$modelname.'_sort'])) {
$selected = explode('.', $_GET[$modelname.'_sort']);
$selected = $selected[0];
}
echo CHtml::dropDownList('sorter',
isset($_GET[$modelname.'_sort'])
? $selected : 0, $attributes);
echo CHtml::dropDownList('direction', isset($_GET['direction']) ? $_GET['direction'] : 'asc',
array('asc' => 'Ascending', 'desc' => 'Descending'));
echo '</div><div style="clear: both;"></div>';
Yii::app()->clientScript->registerScript('sorter', "
$('#sorter').change(function() {
window.location.href = '".$link."?direction='+$('#direction').val()+'&".$modelname."_sort='+$(this).val() + '.' + $('#direction').val();
$('#direction').change(function() {
window.location.href = '".$link."?direction='+$('#direction').val()+'&".$modelname."_sort='+$('#sorter').val()+'.'+$(this).val();
});
");
}
} });
It is not perfectly elegant, but it works (for me). Let me know if there is an extension or something similar that solves this in a nicer way, or if you have any ideas on how to make this better.
Thanks - and enjoy!

Help















