Reputation: 378
I need a way to sort a grid by clicking the column header...
public function search() {
$criteria = new CDbCriteria;
$criteria->select = 'cac.id, cac.client_name, cac.phone, cd.code, t.outgoing_call_date, (SELECT count(id) FROM css_ataps_calls WHERE caller_id = cac.id AND start_date = current_date) as attempts;
Sorting Section
$sort = new CSort();
$sort->defaultOrder = 'cac.id';
$sort->attributes = array('client_id', 'attempts'=>array('asc'=>'attempts', 'desc'=>'attempts DESC'));
SGridView Code
array('name'=>'attempts',
'type'=>'raw',
'value'=>'$data->attempts',
'htmlOptions'=>array('style'=>'width:15%;text-align:center;'),
'headerHtmlOptions'=>array('style'=>'width:15%;')
),
Upvotes: 1
Views: 480
Reputation: 576
In model class:
public $attempts;
and
$dataProvider=new CActiveDataProvider('...', array(
'sort'=>$sort),
));
and in view grid:
'columns'=>array(
...,
array(
'name'=>'attempts',
'htmlOptions'=>array('style'=>'width:15%;text-align:center;'),
'headerHtmlOptions'=>array('style'=>'width:15%;')
),
)
You may also need to update attributeLabels in your model to reflect the new custom field.
Upvotes: 1