Wednesday 5 February 2014

How to set the active and inactive status for 1,0 values in YII CGridView

In Yii normal CGridView will have the below structure


'columns'=>array(
'id',
'title',
'sstatus',

So to set the 0 as Inactive and 1 as Active 

we need to edit the 'status' as

array(
'header'=>'status', //for to set the title for the table column, we can remove this if we need to show the default value
'name'=>'sstatus' // where sstatus is my database column name
'value'=>'($data->sstatus==1)? "Active" : "Inactive"'
),


Once set we can see 0 as inactive and 1 as active. 


But how about filtering? 
The filtering in Yii will work when we input 1 and not as active, so just replace this filter area with dropdown list in the same area where we updated our above code, just add filter array like below



array(
'header'=>'status', //for to set the title for the table column, we can remove this if we need to show the default value
'name'=>'sstatus' // where sstatus is my database column name
'value'=>'($data->sstatus==1)? "Active" : "Inactive"',

'filter'=>CHtml::dropDownList('Slider[sstatus]', 'sstatus',  
                array(
                    ''=>'All',
                    '1'=>'Active',
                    '0'=>'Inactive',
                )
            ),
        ),

),



in the above Slider is my model name and sstatus is my database table column... thats it.......


But i faced some problems like auto select and select all is not working, so we can set the filter directly like the below code


  array(
           'name' => 'sstatus',
           'filter' => array(0 => 'In Active', 1 => 'Active'),
'value' => '($data->sstatus == 1) ? "Active" : "Inactive"',
       ),

No comments:

Post a Comment