Yii Framework Forum: CGridView and a drop down list - Yii Framework Forum

Jump to content

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

CGridView and a drop down list Problem with filtering... Rate Topic: -----

#1 User is offline   vjbill 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 18-May 10
  • Location:Poland

Posted 19 June 2010 - 05:43 PM

Hello,
I'm struggling to achieve a similar admin view as in http://www.yiiframew....php/post/admin (the blog demo). In the demo, after the post/admin view is rendered, all the posts in the system are shown and the drop down list for the status doesn't have any item as 'selected' (in the <option> tag).
I've managed to put a similar view together, but the only problem I'm having is that I can't see all the users in my user admin view, as in a drop down list of filter options, one of them is selected and is filtering the CGridView from the beginning. Which I don't want.

My 'profile/admin' view looks like this:
<?php
	$this->widget('zii.widgets.grid.CGridView', array(
	'id' => 'profile-grid',
	'dataProvider' => $model->search(),
	'filter' => $model,
	'columns'=>array(
		'id',
		array(
			'name' => 'username',
			'type' => 'raw',
			'value' => 'CHtml::link(CHtml::encode($data->username), array("profile/view", "id" => $data->id))',
		),
		array(
			'name' => 'email',
			'type' => 'raw',
			'value' => 'CHtml::link(CHtml::encode($data->email), "mailto:" . $data->email)',
		),
		array(
			'name' => 'createtime',
			'type' => 'datetime',
			'filter' => false
			),
		array(
			'name' => 'lastvisit',
			'type' => 'datetime',
			'filter' => false
		),
		array(
			'name' => 'status',
			'value' => '$data->itemAlias("UserStatus", $data->status)',
			'filter' => $model->itemAlias('UserStatus')
			//'filter'=> false
		),
		array(
			'name' => 'superuser',
			'value' => '$data->itemAlias("AdminStatus", $data->superuser)',
			'filter' => $model->itemAlias('AdminStatus')
			//'filter'=> false
		),
		array(
			'class'=>'CButtonColumn'
		)
	)
	));
?>


The itemAlias method works similarly to the one in the blog demo (Lookup::items). It returns an array in the form [Type][CodeInt] = Label, which applies human-readable labels to user status identified by an integer ('1' is Yes, '0' is No etc.). The main problem is that the option '0' is selected, as in the code:
<option value=""></option>
<option value="0" selected="selected">No</option>
<option value="1">Yes</option>


How is that possible? I really want the drop down list to not have any selected options, so that the Grid View could show all the users in the system when the admin view is accessed for the first time. To finish - I can say that filtering works, so if I select anything from the drop down list, the grid is updated.

Any help appreciated,
With kind regards,
Bill
0

#2 User is offline   vjbill 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 18-May 10
  • Location:Poland

Posted 20 June 2010 - 12:43 PM

Answering myself:
I got to the moment, where I found out that a value '0' supplied to the drop down list is by default selected, so I changed that to '-1'.
Now I'm trying to figure out, why Yii does this query:
Querying SQL: SELECT COUNT(*) FROM 'tbl_user' 't' WHERE (superuser=:ycp0)
AND (status=:ycp1). Bind with parameter :ycp0='0', :ycp1='0'

while rendering the admin view... I can't remember setting the default values for the query anywhere... Does it have something to do with my CDbCriteria in the search() method of the User model?

Thanks in advance for your help,
With kind regards,
Bill
0

#3 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,358
  • Joined: 12-October 09
  • Location:Croatia

Posted 21 June 2010 - 02:13 AM

That is "known" problem for fields that have default values defined in the database...

in current implementation when you create a model those fields gets the default values... that's why CGridView filters by that values...
check this thread with a possible solution http://www.yiiframew...__fromsearch__1

here is a poll for deciding future approach to this problem: http://www.yiiframew...__fromsearch__1
Find more about me.... btw. Do you know your WAN IP?
0

#4 User is offline   vjbill 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 18-May 10
  • Location:Poland

Posted 21 June 2010 - 02:00 PM

Hi mdomba,
Ah, I see. Hehe, in some way it's a nice feeling I'm not alone with that problem :-D.
I found a solution here http://www.yiiframew...weird-behavior/ , but yours is a bit more elegant.

Thanks again
Kind regards
Bill
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