Adding a parameter to Gridview search

Hello there,

for some exotic reason I have a table with a discriminant column (named [font="Lucida Console"]type[/font]). This parameter is used in order to show/hide some records from the table.

So, when managing the table I issue: [font=“Lucida Console”].../index.php?r=table/admin&type=1[/font], the parameter ‘[font=“Lucida Console”]type[/font]’ always go with it.

Now, the problem is, when the user issue a search from the Gridview; because [font=“Lucida Console”]type[/font] column didn’t appears in the grid, the controller never sees this parameter, so it can’t filter the valid records.

So, how can I add this parameter to any query on the gridview?

Thank you,

cbi

Why don’t you just set this parameter inside a controller? Or create a scope in the model: $models = ClassName::model()->visible()->findAll().

Hello andy_s, and thank you for your answer.

I didn’t quite follow you. This parameter is initialized when it first shows the gridview, then I need to recover its value in order to filter in [font=“Lucida Console”]search()[/font] method of the model, with the user’ search parameters of the gridview.

Perhaps I missing something?

cbi

Now, trying to find a solution I stumble upon a strange case… at [font="Lucida Console"]admin.php[/font]:


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

  'id'=>'agent-grid',

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

  'ajaxUrl' => Yii::app()->createUrl($this->route, array( 'Agent[type]' => $model->type ) ),

  'filter' => $model,

  'columns'=> array( ... ) )

So, ajaxUrl goes with the [font="Lucida Console"]type[/font] parameter fixed; so far so good. But when the user sends a new query, the [font="Lucida Console"]Agent[type]=<type>[/font] is lost at [font="Lucida Console"]jquery.yiigridview.js[/font]:


if(options.data!==undefined && options.type=='GET') {

  options.url = $.param.querystring(options.url, options.data);

  options.data = {};

}

It seems that [font=“Lucida Console”]jquery.ba-bbq.js[/font], [font=“Lucida Console”]querystring( ajaxUrl, userData, merge = 0)[/font] function doesn’t understand array parameters, so it erases [font=“Lucida Console”]Agent[type]=<type>[/font] ? Could it be related to html encoding? Agent[…] => Agent%5B…%5D ??

If I use it without array parameters, runs fine:


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

  'id'=>'agent-grid',

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

  'ajaxUrl' => Yii::app()->createUrl($this->route, array( 'type' => $model->type ) ),

  'filter' => $model,

  'columns'=> array( ... ) )

In the controller actionAdmin() I need to add:


if(isset($_GET['Agent']))

  $model->attributes = $_GET['Agent'];


if( isset( $_GET['type'] ) )

  $model->type = $_GET[ 'type'];  



cbi

Can you just build a search form that will collect your ‘type’ response?

Then you can create the grid based on the result.

Take a look at the ‘advanced search’ on the admin view of gii-generated CRUD. Make sure that your form submits via GET not POST. Once you have that in place you can use your controller code to conditionally display the grid if you like, or if it is null you could display the grid without ‘type’ filtering.

Hello dniznick,

[font=“Lucida Console”]_search.php[/font] form runs OK. I’ve just added a hidden field to store [font=“Lucida Console”]type[/font] value.

In fact, I could drop CGridView search functionality and only use _search.php form. But my customer like to use it.

The workaround runs OK, now. But I like to point this issue in jQuery array based parameters merge in order to shed light for others.

Thank you,

cbi

Have been struggling with exactly the same as above and it did shed light and stopped me trying to debug.

A hack solution into top of actionAdmin method.





// TODO: find a better solution to this hack

		$modelName = ucfirst($this->id);


		if(isset($_GET['ajax']))

		{

			// restore $_GET

			$_GET[$modelName] += Yii::app()->session['actionAdminGetHack'];

		}

                else

		{

			// store $_GET

			Yii::app()->session['actionAdminGetHack'] = $_GET[$modelName];

		}




Im dealing with this problem too.

I have a grid that needs to apply a condition set previously. The first load is ok, but when you do a filter/paging the value is lost bc the ajax doesnt send it.

I dont wanna rely on sessions, so I saw your solution using ajaxUrl and it almost worked, paging still ignores this url.

Im gonna try to add my filter column to the grid, passing a default value inside the textbox filter and then try put it visibility: hidden or something like this.

Ok, Ive solved this problem writing some code on CLinkPager.

I added a public field


public $params = array();

And


protected function createPageButton($label, $page, $class, $hidden, $selected) {

        if ($hidden || $selected)

            $class.=' ' . ($hidden ? self::CSS_HIDDEN_PAGE : self::CSS_SELECTED_PAGE);

        $pagerParams = '';

        $urlDefault = $this->createPageUrl($page);

        foreach ($this->params as $key => $value) {

            $pagerParams .= "&" . $key . "=" . $value;

        }

        $finalUrl = $urlDefault . $pagerParams;

        return '<li class="' . $class . '">' . CHtml::link($label, $finalUrl) . '</li>';

    }

added this code inside createPageButton function.

It’s working, but now I’m trying to verify if the


$this->createPageUrl($page);

is not adding the same parameters, to avoid duplicates. Im going to do this inside the foreach, using something like


 strpos

You can then add parameters ilke this:




.

.

.

'columns' => array(

array(

            'class' => 'CButtonColumn',

            'template' => '{delete}{update}',

        ),

    ),

 'pager' => array(

        'params' => array('param1' => 'value1'),

    ),