Default value in Filter

Good day.

Anyone have an idea on how to add default value in filter field?

eg.

I want to add 3833- a default value for voucher no

here is the code:


<?php $grid = $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'vouchers-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(

		'voucher_no',

		'first_name',

		'last_name',

		'purchase_date',

		'date_used',

		'barcode',


        ...  



Please change actionSearch in your model like this…


if(!empty($this->voucher_no))

	$criteria->compare('voucher_no', $this->voucher_no, true);

else

	$this->voucher_no='3833';

If you want default filter please use following code [but its not right]


if(!empty($this->voucher_no))

	$this->voucher_no = '3833';

$criteria->compare('voucher_no', $this->voucher_no, true);

The above solution would search the default value in case the user did not input anything…

If you want a default value to be displayed to the user so that he can change it or add some criteria… in your model… before rendering the view add the desired value to the model field… like




$model = new yourmodel();

$model->voucher_no='3833-';

$this->render(...);



This works great!

Thanks mdomba!

Thanks!

This works if the default filter is set directly on table records, but what about if I want to set a default filter on a related record value ?? I tried to do it like :

$model->relatedModel->TheAttributeIWantCheck = ‘’;

but I have an "Indirect modification of overloaded property" exception