CGridview filter on page load with pre define value in search field

bjtilley picture bjtilley · May 10, 2012 · Viewed 11.2k times · Source

I am working with the Yii framework.

I have set a value in one of my cgridview filter fields using:

Here is my jQuery to assign a value to the searchfield:

$('#gridviewid').find('input[type=text],textarea,select').filter(':visible:first').val('".$_GET['value']."');

And here my PHP for calling the cgridview:

$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'bills-grid',
'dataProvider'=>$dataProvider,
'filter'=>$model,
'cssFile'=>Yii::app()->baseUrl . '/css/gridview.css',
'pager'=>array(
    'class'=>'AjaxList',
    'maxButtonCount'=>25,
    'header'=>''
),
'columns' => $dialog->columns(),
'template'=>"<div class=\"tools\">".$dialog->link()."&nbsp;&nbsp;&nbsp;".CHtml::link($xcel.'  Export to excel', array('ExcelAll'))."</div><br />{items}{summary}<div class=\"pager-fix\">{pager}</div>",));

The value appears in the search field and my cgridview works correctly without any issues, but I am unable to trigger the cgridview to refresh or filter. Does anyone know who to trigger the cgridview to filter after page load with a predefined value?

Any help would be greatly appreciated and please let me know if you need additional information.

Thank you.

Answer

dInGd0nG picture dInGd0nG · May 11, 2012

You can solve the problem without any clientside code modification. In your controller action just set the default value for the attribute as shown below

public function actionAdmin()
{
    $model = new Bills();
    $model->unsetAttributes();
    $model->attribute_name="default filter value";//where attribute_name is the attribute for which you want the default value in the filter search field
    if(isset($_GET['Bills'])){
        $model->attributes = $_GET['Bills'];
    }

    $this->render('admin',array('model'=>$model));
}