I don't know if it is possible, but suppose it must be. I need to present some data in frontend view - the list of companies in tabular form: Company name 1 - Country 1 - Website 1 - etc... Company name 2 - Country 2 - Website 2 - etc... etc... I'm trying to use CGridView for this purpose.
My controller:
public function actionList()
{
$sort = new CSort();
$sort->attributes = array(
'defaultOrder'=>'company_name DESC',
'company_name'=>array(
'asc'=>'company_name ASC',
'desc'=>'company_name DESC',
),
'country'=>array(
'asc'=>'country ASC',
'desc'=>'country DESC',
),
);
$criteria = new CDbCriteria();
$criteria->order = "company_name DESC";
$criteria->condition = 'approve = :approve';
$criteria->params = array(':approve'=>1);
$dataProvider = new CActiveDataProvider('EuCompanies', array('criteria'=>$criteria,'sort'=>$sort));
$this->render('list',
array('dataProvider'=>$dataProvider,
));
}
My view:
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dataProvider,
'columns'=>array(
'company_name',
'country',
'company_website',
'economic_sector',
'contact_person',
'email',
'phone_number'
),
));
?>
The output is a nice grid, which takes into account the selection criteria, and clickable "Company name" and "Country", but clicking on them doesn't make the column sort. I guess it is because sort can't be done without using $model->search() in dataProvider, or I've just done something wrong?
Ok, solved. Two points were wrong:
First:
$criteria->order = "company_name DESC";
was over-riding any other sort. After deleting this string, my columns become sortable.
Second (default sort didn't work):
Had to move
$sort->attributes = array(
'defaultOrder'=>'company_name DESC',
from attributes to
$sort = new CSort();
$sort->defaultOrder = 'company_name ASC';
$sort->attributes = array(...
Work done, thank you :)