CGridView and a drop down list

Hello,

I’m struggling to achieve a similar admin view as in http://www.yiiframework.com/demos/blog/index.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

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

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

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.yiiframework.com/forum/index.php?/topic/9533-cdbcriteria-compare-weird-behavior/ , but yours is a bit more elegant.

Thanks again

Kind regards

Bill