Adding A Pre-Condition To Data Loaded In Cgridview

(Sukhman4) #1

I have a post model, it is having attributes like section,category. in Gii generated code of admin controller, by default all the posts are loaded via Cactivedataprovider . But what i want is, to provide links/drop-down menu for the users to select a particular section or category, and then show the Posts having this particular section or category. so the value for the section/category will be taken from $_GET[]. How can i add this to tell Cactivedataprovider to load the posts of a particular category or section only?


(Amjad Mghl) #2

Do as

1 - Create a dropdown with onChange reload the page with selected value(using javascript)

2 - In controller’s action where you are calling for data i.e. CActiveRecord, use CDBCriteria to set the category for whom you want to list the articles


(Sukhman4) #3

I am using Gii generated search() method, which in turn use CDbcriteria->compare() method for searching. since the paging and filtering is done using AJAX, so i am confuse how exactly to add a condition to existing $CDbcriteria->compare() statements?

(Softark) #4

Hi Sukhwinder,

Make use of the "advanced search form" in the gii generated admin page. The form should have the text field for category_id … you can replace it with a dropdown list … and the job is done. Your search() method should already have the code to handle searching by category_id.

Please read the following wiki for detailed explanation.