Yii Framework Forum: CGridView filter dropdown from array - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CGridView filter dropdown from array Rate Topic: ***** 1 Votes

#1 User is offline   gv0zd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 06-April 12

Posted 10 April 2012 - 06:21 AM

Resolved

Hi! 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?
0

#2 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,168
  • Joined: 05-January 12
  • Location:Morocco

Posted 10 April 2012 - 07:14 AM

Well I haven't tested it but I guess it's more or less:
        …
        array(
            'name'=>'onoff',
            'value'=>'$data->onoff ? "On" : "Off"', // Notice the double quotes inside the single quotes. I guess it can be the opposite (single into double)
            'filter'=>CHtml::dropDownList('Provider[onoff]', 'onoff',  // you have to declare the selected value
                array(
                    ''=>'All',
                    '1'=>'On',
                    '0'=>'Off',
                )
            ),
        ),
        …

0

#3 User is offline   gv0zd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 06-April 12

Posted 10 April 2012 - 07:30 AM

View Postbennouna, on 10 April 2012 - 07:14 AM, said:

Well I haven't tested it but I guess it's more or less:
        …
        array(
            'name'=>'onoff',
            'value'=>'$data->onoff ? "On" : "Off"', // Notice the double quotes inside the single quotes. I guess it can be the opposite (single into double)
            'filter'=>CHtml::dropDownList('Provider[onoff]', 'onoff',  // you have to declare the selected value
                array(
                    ''=>'All',
                    '1'=>'On',
                    '0'=>'Off',
                )
            ),
        ),
        …


No, it doesn't work too
0

#4 User is offline   gv0zd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 06-April 12

Posted 10 April 2012 - 07:52 AM

$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';
}

2

#5 User is offline   ReekrScream 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 06-May 12

  Posted 11 May 2012 - 10:26 PM

Hello,

I am having the same problem. I tried the solution posted by "gv0zd" but it didn't work. After every selection in the dropbox, it returns to the "" value (in my case "ALL").

This is what I tried also but it's not working either:
<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'contents-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'columns'=>array(
		array(
                    'header'=>'Active',
                    'name'=>'active',
                    'value'=>'Contents::model()->getActiveStateName($data->active)',
                    'filter'=>CHtml::dropDownList('Contents[active]','',CHtml::listData(Contents::model()->getActiveStatesArray(), 'value', 'name'),array('options'=>array('$data->active'=>'selected'))),
                    

...
...
...


Everything works fine except for this part:
array('options'=>array('$data->active'=>'selected'))


It seems like the $data->active is not applied in that case.

The end result is: "All" is selected after every dropdown selection. The selection doesn't stick.


Anyone have any ideas?

Thank you!
0

#6 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,085
  • Joined: 16-February 11
  • Location:Japan

Posted 11 May 2012 - 11:24 PM

Just as gv0zd has posted, you don't have to set a dropdown list for the filter.
You just have to give the option list array for it.
                    'filter'=>CHtml::listData(Contents::model()->getActiveStatesArray(), 'value', 'name'),

The selection of the dropdown list should be automatically set ... doesn't it?
0

#7 User is offline   ReekrScream 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 06-May 12

Posted 13 May 2012 - 09:56 PM

View Postsoftark, on 11 May 2012 - 11:24 PM, said:

Just as gv0zd has posted, you don't have to set a dropdown list for the filter.
You just have to give the option list array for it.
                    'filter'=>CHtml::listData(Contents::model()->getActiveStatesArray(), 'value', 'name'),

The selection of the dropdown list should be automatically set ... doesn't it?


Hi softark,

I tried gv0zd's original suggestion and it works fine (you are right) however there's one small problem I was trying to fix, which took me down the dropDownList road (sorry I did not specify that).

When I use gv0zd's code above, I get this output:

<select name="Contents[active]">
<option value=""></option>   <<//-- this is the line
<option value="" selected="selected">All</option>
<option value="1">Yes</option>
<option value="0">No</option>
</select>


There's always that first EMPTY option that I don't know how to get rid of. I tried removing the "All" option in getActiveStatesArray() but I would like to at least add a "name" for that option.

Any idea how I would do this?

Thank you very much for your reply.
0

#8 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,085
  • Joined: 16-February 11
  • Location:Japan

Posted 14 May 2012 - 03:01 AM

I see. :)

So your Contents::model()->getActiveStatesArray() returns:
array(
    '' => 'All',
    '1' => 'Yes',
    '2' => 'No',
)

And the auto filter will add one more item:
    '' => '',

at the beginning of the array as the 'empty' entry meaning "don't care".

Now if your Contents::model()->getActiveStatesArray() had only returned 'Yes' and 'No', and if you could accept empty string for "don't care", then there had been nothing problematic.

Well, if you want 'All' for "don't care", then you have to set a dropdown list for the filter.
Would you please try this:
    'filter'=>CHtml::dropDownList(
        'Contents[active]',
        $model->active,
        CHtml::listData(Contents::model()->getActiveStatesArray(), 'value', 'name')
    ),


Or, with your getActiveStatesArray() returning only 'Yes' and 'No' ...
    'filter'=>CHtml::dropDownList(
        'Contents[active]',
        $model->active,
        CHtml::listData(Contents::model()->getActiveStatesArray(), 'value', 'name'),
        array(
            'empty' => 'All',
        )
    ),


http://www.yiiframew...DownList-detail

P.S.
I think you can declare your getActiveStatesArray() static and refer it simply by Contens::getActiveStatesArray().
2

#9 User is offline   ReekrScream 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 06-May 12

Posted 14 May 2012 - 07:32 PM

Hi Softark,

I just tried both of your code variants but I get the same bug as before: when choosing "Yes" from the dropdown, the filtering works fine, however the selected item on the dropdown becomes "All" (or the first option) in all cases. This means that I can pick "Yes" or "No" in that field but I cannot select "All" to view all records without refreshing the page.

Here is a very short recording I made of the problem:
http://www.mediafire...qeobgl9cpnckamu

Please take a look when you have time and let me know what else I could try.

Side note: when I made the change the first time, it worked perfectly fine until I refreshed the page and tried on another browser. I immediately thought it might have something to do with caching, so I emptied the "assets" dir. But no luck after that first time.

Thank you very much for your great help and a great community!
0

#10 User is offline   ReekrScream 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 06-May 12

Posted 14 May 2012 - 08:32 PM

Allright, everyone, I figured it out with some more tweaking. This was the line that worked without problems:

/protected/views/content/admin.php
                	'filter'=>CHtml::dropDownList(
                        	'Contents[active]',
                        	$model->active,
                        	CHtml::listData(
                                	Contents::model()->getActiveStatesArray(),
                                	'value',
                                	'name'),
                        	array('options'=>array('$data->active'=>'selected'))),


The only thing that was missing from my previous attempts was the $model->active, which softark informed me about.

Thank you very much for your help. I am very happy with the Yii platform and community, you guys rock!
0

#11 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,085
  • Joined: 16-February 11
  • Location:Japan

Posted 15 May 2012 - 12:58 AM

Glad to hear that. :)

View PostReekrScream, on 14 May 2012 - 08:32 PM, said:

                        	array('options'=>array('$data->active'=>'selected'))),


I think the 'options' you set for the dropdown is not written correctly.
Fortunately it seems to be causing no problem, either being totally ignored or just producing a garbage html code without meaning.
You can omit it safely. :)
1

#12 User is offline   ReekrScream 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 06-May 12

Posted 16 May 2012 - 12:34 AM

View Postsoftark, on 15 May 2012 - 12:58 AM, said:

Glad to hear that. :)


I think the 'options' you set for the dropdown is not written correctly.
Fortunately it seems to be causing no problem, either being totally ignored or just producing a garbage html code without meaning.
You can omit it safely. :)


You are absolutely correct about it working without the "options". However, I don't see any bad HTML generated with the "options" array in place. This is the output:
<select name="[url=""]Contents[active][/url]" id="[url=""]Contents_active[/url]">
<option value="" selected="[url=""]selected[/url]">All</option>
<option value="[url=""]1[/url]">Yes</option>
<option value="[url=""]0[/url]">No</option>
</select>

Well, in any case, I will remove the "options" array and leave it like that.

Thank you very much for your guidance and help!
0

#13 User is offline   thura747 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 10-July 12

Posted 05 September 2012 - 02:26 AM

Q : How to filter CGridview filter dropdown from controller array

I create an array at my controller

$arr_method = array('Cash'=>'Cash', 'Cheque'=>'Cheque', 'Credit Card'=>'Credit Card', 'Bank Transf'=>'Bank Transf');

I call above array at Cgridview

array('name' => 'method',
'value'=>'(isset($data->method)) ? CHtml::encode($data->method) :""',
'filter'=>CHtml::dropDownList('method', '', $arr_method),
),

dropdown list is appear at filter. but when I select something, filter is going back to 'Cash'. and result is showing blank.
0

#14 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,085
  • Joined: 16-February 11
  • Location:Japan

Posted 05 September 2012 - 05:28 AM

    array('name' => 'method',
    	'value'=>'(isset($data->method)) ? CHtml::encode($data->method) :""',
    	'filter'=>CHtml::activeDropDownList($model, 'method', $arr_method),
    ),

Or,
    array('name' => 'method',
    	'value'=>'(isset($data->method)) ? CHtml::encode($data->method) :""',
    	'filter'=>$arr_method,
    ),

0

#15 User is offline   jaswalkiranavtar 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 31-October 12

Posted 31 October 2012 - 04:47 AM

Code for CGridView:

<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'puzzle-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'group.name',
array(
'name'=>'group_id',
'filter'=>CHtml::listData(PuzzleGroup::model()->findAll(),'id','name'),
),
'category.name',
'phrase',
array(
'name'=>'enabled',
'filter'=>array('Yes'=>'Yes','No'=>'No'),
),
array(
'class'=>'CButtonColumn',
),
),
));
//'group.name',
?>

Relations between the two tables:

public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'tbHints' => array(self::HAS_MANY, 'TbHints', 'puzzle_id'),
'group' => array(self::BELONGS_TO, 'PuzzleGroup', 'group_id'),
'category' => array(self::BELONGS_TO, 'PuzzleCategory', 'category_id'),
);
}

Code for search function:

public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.

$criteria=new CDbCriteria;

$criteria->compare('id',$this->id);
$criteria->compare('group_id',$this->group_id);
$criteria->compare('category_id',$this->category_id);
$criteria->compare('phrase',$this->phrase,true);
$criteria->compare('enabled',$this->enabled,true);

return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}


For the above code can anyone please let me know how to create CGridView for Puzzle with dropdownlist filters containind puzzlegroup name from PuzzleGroup which is related to Puzzle with group_id.
Also i want to display PuzzleGroup name instead of id in the CGridView.
0

#16 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,085
  • Joined: 16-February 11
  • Location:Japan

Posted 31 October 2012 - 07:29 AM

Hi,

I think your code should work ... doesn't it?
About displaying the name instead of the id, you could do it like this:
array(
    'name'=>'group_id',
    'filter'=>CHtml::listData(PuzzleGroup::model()->findAll(),'id','name'),
    'value'=>'$data->group->name',
),

0

#17 User is offline   riyazMuhammed 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 13-September 12
  • Location:Bangalore

Posted 03 July 2014 - 06:31 AM

just
'filter'=>array('1'=>'On','0'=>'Off'),

is enough.
1

#18 User is offline   Yatin Mistry 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 11-March 14
  • Location:Mumbai

Posted 08 August 2014 - 06:07 AM

View PostriyazMuhammed, on 03 July 2014 - 06:31 AM, said:

just
'filter'=>array('1'=>'On','0'=>'Off'),

is enough.


Thanks,

Great Solutoin.
Yii Posts

Yes it is 2.0.0.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users