CGridView filter dropdown from array
15,069
Solution 1
array( 'name' => 'language',
'value' => '$data->language',
'filter' => CHtml::listData( Vediodesc::model()->findall(), 'language', 'language'),
),
Solution 2
$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';
}
Author by
dr0zd
Updated on June 12, 2022Comments
-
dr0zd almost 2 years
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?