Gridview filtering based on user input

Hi,

I have a gridview which I want to update based on a dropdown list selection of an attribute that is not a column in the grid (but is a relational attribute of the model in question). The value of the option selected in the dropdown needs to be passed as an argument to the search() method on the model called by the ‘dataProvider’ of CGridView widget. Can someone suggest how I could accomplish this? I think I should be using ajax but am I not too sure if and how.

I guess I am trying to pre-filter the data fetched by dataProvider instead of filtering on column data after it is fetched.

Appreciate any help. Thanks.

Try this post here:

Yii Forum

Thanks. The solution in the post involves having a relational attribute as a data column in the grid and generating a dropdown filter for it - I am aware of how to achieve that. I am looking for a solution where the dropdown is not a data column but is, say, placed above the grid and when the user picks an option, the value is passed as a parameter to the search() method called by the grid thereby refreshing the grid with relevant data.

I’ve been struggling with a similar issue.

I have a form which allows the user to input variables (lat,lng, distance,typeid) that are required for a database query. The query result is then displayed in a CGridView.

When the form is submitted the data in the controller is :

POST=Array

(

[RouteSearch] => Array


    (


        [typeid] => 0


        [distance] => 200


        [lat] => 52.5


        [lng] => -4


    )





[yt0] => Search

)

GET=Array

(

)

The database query can be run and the information is displayed correctly in the CGridView.

If I try to page or sort the table the data in the controller is typically:

POST=Array

(

)

GET=Array

(

[ajax] => im-route-search


[page] => 2

)

There are no lat,lng, distance or typeid submitted so it is not possible to run the database query. The gii examples always use the entire table contents so don’t have this problem

The work around (so far) is to put code in the controller to store the missing information in the session and recover it if it isn’t present.

e.g.

	if(isset($_POST['RouteSearch'])) {


		$model->attributes=$_POST['RouteSearch'];


	} else if (isset($session['RouteSearch'])) {


		$model->attributes=$session['RouteSearch'];


	}

The sort and paging seems to work correctly but it isn’t very elegant. It would be much better if they were provided in the request.

Is there any way of configuring the CGridView to include a set of data attributes in all the sort and page requests?