2

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.

Kai
  • 38,985
  • 14
  • 88
  • 103
bjtilley
  • 1,953
  • 3
  • 17
  • 22

3 Answers3

9

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));
}
dInGd0nG
  • 4,162
  • 1
  • 24
  • 37
1

Have a look at 'default' index action that gii generates:

public function actionIndex()
{
    $model = new Bills();
    $model->unsetAttributes();
    if(isset($_GET['Bills'])){
        $model->attributes = $_GET['Bills'];
    }

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

So if you add one line like: $model->attribute = 'test';, you're done. 'attribute' is of course the attribute that has to have the default filter value (in this case value is 'test') :). So your code looks like:

public function actionIndex()
{
    $model = new Bills();
    $model->unsetAttributes();
    if(isset($_GET['Bills'])){
        $model->attributes = $_GET['Bills'];
    }

    if(!isset($_GET['Bills']['attribute']) {
        $model->attribute = 'test';
    }

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

Of course youre attribute will have a test value (in filter) set up as long as you wont type anything in its filter field. I hope that that's what you're looking for. Your filter should work as always.

Sorry for my bad english :)

Regards

pere_
  • 235
  • 1
  • 4
0

You can use Yii's update:

$.fn.yiiGridView.update('bills-grid', {
 type: 'GET',
 url: <?php echo Yii::app()->createUrl('controller/action') ?>"?Class[attribute]=<?php echo $_GET['value'] ?>
 success: function() {
  $.fn.yiiGridView.update('bills-grid');
 }
});

This is how i do it, just change the URL, it should be the same controller action of the gridview and change URL parameters to the structure represented in there, should be like Bills[attribute]=value.

Skatox
  • 4,237
  • 12
  • 42
  • 47
  • Skatox, thanks so much for your help. I feel like I am so close. I am getting the following error in Firebug when using the code you provided: settings is undefined http://localhost/telecom/trunk/assets/4c8e5468/gridview/jquery.yiigridview.js Line 203 – bjtilley May 10 '12 at 22:19
  • Use firebug and check what information (url params) are being sent when using filters (not from the method i gave you) and send the same GET information plus the variables i gave you in the answer. – Skatox May 11 '12 at 01:11