Filter In Cgridview - Retrieving The Results And Transferring To Another View


(Biancajsen) #1

Hi everyone,

I have a cgridview where I filter the contents as I wish using the header filters in each column. It works fine but I was wondering if after filtering, I can transfer the results to another view. I would then add a button or link somewhere on the page and on clicking this button or link, I am redirected to another view with the contents coming from the gridview contents after filtering. Can someone guide me on how to do that?

Thanks beforehand


(Biancajsen) #2

Hi,

I have tried to retrieve the filter values by $_GET[‘Mymodel’] in another function in my controller but I get undefined index as error message. These filter values are retrieved in the actionAdmin of the controller and it works fine. Can’t they be retrieved in the same way in another function of the controller?


(Faridplusplus) #3

CGridView has a property called ‘ajaxUrl’ which determines where GET parameters should go. something like this:


$this->widgets('zii.widgets.gird.CGridView', array(

	.....

	'ajaxUrl' => array('controller/action'),

	.....

));


(Biancajsen) #4

Hi Faridplus,

Thanks for your reply. The problem is that I want to retrieve the $_GET values in the actionAdmin of my controller and also in another action of the same controller. I have a button in the gridview page which will call the 2nd function and I need to pass the filter values to this 2nd function.


(Chellamnivas) #5

Dear Friend

We can do it in the following way.

Add the following at the bottom of admin.php




<?php echo CHtml::button('Collect',array('id'=>'butt'));?>


<?php Yii::app()->clientScript->registerSCript('test','

$("body").on("click","#butt",function(){

	var str="&";

	$.each($("#test-grid input").serializeArray(),function(i,j){

		str=str+j.name+"="+j.value+"&";

		

		});

		window.location="'.CHtml::normalizeUrl(array('test/collect')).'"+str;

	});


')?>



Here test-grid is the id of Cgrid.

actionCollect method of TestController




public function actionCollect()

{

	$model=new Test('search');

	$model->unsetAttributes();

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

	$dataProvider=$model->search();

	$this->render('index',array(

		'dataProvider'=>$dataProvider

		));

}



My solution looks not that much elegant. I think we are missing some simple solution.

Kindly share with us if you get one.

Regards.


(Biancajsen) #6

Dear Seenivasan,

You are a champ!!! I do not know if it is elegant or not; what I know is that it works like a charm. Thank you so much :D I will look into the code more deeply later on, in order to understand it. Thanks again


(Biancajsen) #7

Dear Seenivasan,

I am customizing the code you gave me in order to add a dynamic view in the url. It works fine with the dynamic view but I also wanted to add a parameter to it. I have tried in vain but with the added parameter, it does not work. Could you please help me. I guess it is a syntax problem. Here is the code:




	<?php echo 'Choose a report: ';$rapport = array('arrival'=>'Arrival','Detailed'=>'Detailed','Guest'=>'Guest','Summary'=>'Summary'); ?>

	<?php echo CHtml::dropDownList('rapport','Index',$rapport,array('id'=>'vw')); ?>





	<?php echo 'Report Title:'; echo CHtml::textField($titre,'',array('id'=>'titre','size'=>85,'maxlength'=>95)); ?>


	<?php echo CHtml::button('Process',array('id'=>'butt'));?>


<?php $this->endWidget(); ?>


<?php Yii::app()->clientScript->registerSCript('test','$("body").on("click","#butt",function()

	{

		var str="&";

		$.each($("#reservation-grid input").serializeArray(),function(i,j)

		{

			str=str+j.name+"="+j.value+"&";

		});

		alert(str);vw=$("#vw").val(); titre=$("#titre").val(); alert(titre);

		window.location="'.CHtml::normalizeUrl(array('"+vw+"', 'id'=>$titre)).'"+str;        });')?>



I also tried the following:-





window.location="'.CHtml::normalizeUrl(array('"+vw+"', 'id'=>"+titre+")).'"+str; 



but it does not work. Thanks


(Biancajsen) #8

I managed to solve it in another way. I suppose it was a problem with url encoding.

I changed the code to this:




		titre=$("#titre").val();

		var str="&id="+$("#titre").val()+"&";

		$.each($("#reservation-grid input").serializeArray(),function(i,j)

		{

			str=str+j.name+"="+j.value+"&";

		});

		window.location="'.CHtml::normalizeUrl(array('"+vw+"')).'"+str;        });')?>




It works. It’s not too elegant, may be


(Trevor) #9

Hi,

I have a similar problem that I have solved for all but one of the cgridview filters, which is a related field.

I am using the solution Seenivasan has supplied but the related status does not get added to the string.

here is the cgridview :-

$this->widget(‘zii.widgets.grid.CGridView’, array(

'id'=&gt;'tbl-orders-grid',


'dataProvider'=&gt;&#036;dataProvider,


'afterAjaxUpdate'=&gt;&quot;function() {


                                            jQuery('#Orders_date_first').datepicker(jQuery.extend({showMonthAfterYear:false}, jQuery.datepicker.regional['id'], {'showAnim':'fold','dateFormat':'yy-mm-dd','changeMonth':'true','changeYear':'true','constrainInput':'false'}));

jQuery(’#Orders_date_last’).datepicker(jQuery.extend({showMonthAfterYear:false}, jQuery.datepicker.regional[‘id’], {‘showAnim’:‘fold’,‘dateFormat’:‘yy-mm-dd’,‘changeMonth’:‘true’,‘changeYear’:‘true’,‘constrainInput’:‘false’}));

                                            }&quot;,





'filter'=&gt;&#036;model,


'columns'=&gt;array(


	array(


		'name'=&gt;'id',


		'type'=&gt;'raw',


		'value'=&gt;'str_pad(&#036;data-&gt;id,8-strlen(&#036;data-&gt;id),&quot;0&quot;,STR_PAD_LEFT)',


		'htmlOptions'=&gt;array('style' =&gt; 'text-align: right;width: 60px;')


	),


	array(


            'name'=&gt;'date_order_placed',


            'filter'=&gt;&#036;dateisOn,


            'value'=&gt;'date(&quot;d-m-Y&quot;,strtotime(&#036;data-&gt;date_order_placed))',


            ), 


	array(


	  'name'=&gt;'status_holder',


	  'type'=&gt;'raw',


	  //'htmlOptions'=&gt;array('id' =&gt; 'order_status_search'),


	  'value'=&gt;'CHtml::value(&#036;data,&quot;status.orders_status_name&quot;)',


	  'filter'=&gt;CHtml::listData(OrderStatus::model()-&gt;findAll(


				  array(


				   'select'=&gt;array('orders_status_name'),


				   'distinct'=&gt;true


					


				  )),&quot;orders_status_name&quot;,&quot;orders_status_name&quot;)//this is the focus of your code


	   


   ),


   


	array(


		'class'=&gt;'EButtonColumnWithClearFilters',


		 'clearVisible'=&gt;true,


		'template'=&gt;'{view}{email}',


		'buttons'=&gt;array


		(


			'email' =&gt; array


			(


				'label'=&gt;'Reprint invoice and email to yourself',


				'imageUrl'=&gt;Yii::app()-&gt;request-&gt;baseUrl.'/images/email.png',


				'url'=&gt;'Yii::app()-&gt;createUrl(&quot;orders/ureprint&quot;, array(&quot;id&quot;=&gt;&#036;data-&gt;id))',


				


				


			),


			


		),


	),


),

)); ?>

The other filters, dates and id, work perfectly.

any help would be really appreciated :)