Adding A Filter

Bonjour mes amis,

I was trying to creating a filter on a Gridview using the SQL Data Provider.

Prehaps this sample code will say more





$sql= "SELECT b.agency_name as agency,c.jurisdiction_name as jurisdiction,start_year,end_year,

			  e.approval_status_name as approval_status, a.work_plan_id as id

		FROM (select * from tbl_work_plan) a left join

		(select agency_id,agency_name from tbl_agency) b on

		a.agency_id = b.agency_id left join

		(select * from tbl_jurisdiction) c on

		a.jurisdiction_id = c.jurisdiction_id left join

		(select * from tbl_financial_year) d on 

		fy_id = financial_year_id left join

		(select * from tbl_approval_status) e on 

		e.approval_status_id = a.approval_status_id 

		where a.approval_status_id != 12";

  


//echo $sql;Yii::app()->end();


$count = Yii::app()->db->createCommand($sql)->query();

$count = count($count);


$dataProvider = new CSqlDataProvider($sql, 

                                      array(

									          'totalItemCount'=>$count,

											  'sort'=>array(

											                 'attributes'=>array(

															                       'agency','approval_status')

											              ),

											   'pagination'=>array(

											                        'pageSize'=>10,

																	)           

														  ));


?>


<h1>Work Plans</h1>


<?php


$this->widget('zii.widgets.grid.CGridView', 

                 array(

				       'dataProvider'=>$dataProvider,

					   'htmlOptions'=>array(

					                          'style'=>'position:relative;top:0px;cursor:arrow;'),

					   'columns'=>array(

					                    array('name'=>'number', 'header'=>'No.', 'value'=>'$row+1'),

					                    array('name'=>'agency', 'header'=>'Agency',  'type'=>'raw',

					                    	  'value'=>'CHtml::link(CHtml::encode($data["agency"]), "../workPlan/entrySummary?id=".$data["id"]." ")'),	

					                    array('name'=>'jurisdiction', 'header'=>'Jurisdiction', 'value'=>'$data["jurisdiction"]'),						

					                    array('name'=>'start_year', 'header'=>'Start year', 'value'=>'$data["start_year"]'),						

					                    array('name'=>'end_year', 'header'=>'End year', 'value'=>'$data["end_year"]'),						

					                    array('name'=>'approval_status', 'header'=>'Approval Status', 'value'=>'$data["approval_status"]'),											                   					                    													 

									/* array(   'header'=>'Edit',

										      'class'=>'CButtonColumn',

										      'template'=>'{editWorkPlan}',

										      'buttons'=> array(

										                 'editWorkPlan'=>array(

											                  'label'=>'Edit',

											                  'imageUrl'=>Yii::app()->request->baseUrl."/images/edit.png",											                     

															  'url'=>'Yii::app()->createUrl("workPlan/entrySummary",array("id"=>$data["id"]))',	                 

																				 )											                    

																),

											  ),	*/						

										

										),		

																		

											 ));


?>




I would like to add a filter to enable users search the records via a text box at the top, just like the admin pages for when scafolding is done.

I would really appreciate any help.

Merci. :)

There is no easy way to do so if you insist on using DAO. Does the size of your datasets prevent the usage of AR?

It is just that I am crawling information from different tables based on several conditions and scenarios.

This is only one relatively simple example of several other similar GridViews for which I have to display.

BTY, thanks for the quick response. :)

I suggest you to use active record instead of dao, it will keep the thinks much simplier.

You can create almost any query with AR, but the filter, sorting and paging procedure will be much easier.

I suggest you to switch to AR and ask in the forum if you have problem in implementing a specific filter, we’ll be glad to help you and you’ll see how active records rocks!

Using AR will greatly reduce your dev time and make your life much easier. You’re just using JOINs, right?