Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGridView filter dropdown from array

Tags:

yii

I have table of providers (id, title, onoff) where onoff column is a status: 1 = on, 0 = off I dont have table in DB for these statuses, so I don't have model for statuses.

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'provider-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        array(
            'name'=>'id',
            'htmlOptions'=>array('width'=>'40px'),
        ),
        'title',
        array(
            'name'=>'onoff',
            'filter'=>CHtml::dropDownList('Provider[onoff]', '',  
                array(
                    ''=>'All',
                    '1'=>'On',
                    '0'=>'Off',
                )
            ),
        ),
        array(
            'class'=>'CButtonColumn',
            'template'=>'{update}{delete}'
        ),
    ),

It filters data, but after ajax forget the state of dropdown What is the best way to build dropdown in this case?

And what is the best way to substitute 1 to On and 0 to Off in datagrid cells?

like image 749
dr0zd Avatar asked Apr 10 '12 11:04

dr0zd


1 Answers

$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'provider-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
    array(
        'name'=>'id',
        'htmlOptions'=>array('width'=>'40px'),
    ),
    'title',
    array(
        'name'=>'onoff',
        'value'=>'Crud::getOnoff($data->onoff)',
        'filter'=>CHtml::listData(Crud::getOnoffs(), 'id', 'title'),,
    ),
    array(
        'class'=>'CButtonColumn',
        'template'=>'{update}{delete}'
    ),
),

In model

public function getOnoffs()
{
return array(
    array('id'=>'1', 'title'=>'On'),
    array('id'=>'0', 'title'=>'Off'),
);
}
public function getOnoff($onoff)
{
if($onoff == 1) 
    return 'On';
else 
    return 'Off';
}
like image 137
dr0zd Avatar answered Nov 01 '22 16:11

dr0zd