Search code examples
phpsortingyiicgridview

sort value from function in cgridview


i have a table that one of column has value from function

<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'camera-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
    array(
        'name'=>'location', 
        'header'=>'Location', 
        'value'=>'$data->location->name'
    ),
    array(
        'header'=>'Last Date',
        'value'=>'$data->myfunction($data->location->name)'
    ),
),)); ?>

my questions is:

  1. how to sort column Last Date?
  2. there's chance the Last Date is null. So, if i want to display rows with Last Date is not null, what i'm suppose to do?

Thanks before :)


Solution

  • in the end, i found this solution. overall, i must make my custom variable as SQL query. So, i can sort or search custom variable like other variable from database.

    in mymodel,

    1. declare new variable public $LAST_DATE;

    2. add $criteria

    $criteria->select = array('*','COALESCE(location.name, \'\') AS LAST_DATE'); $criteria->order = 'LAST_DATE DESC'; return new CActiveDataProvider($this, array( 'criteria'=>$criteria, 'sort'=>array( 'defaultOrder'=>'LAST_DATE DESC', 'attributes' => array( 'LAST_DATE'=>array( 'asc'=>'COALESCE(location.name, \'\')', 'desc'=>'COALESCE(location.name, \'\') DESC', ), '*', ), ),