Filter In Cgridview - Retrieving The Results And Transferring To Another View
#1
Posted 10 December 2012 - 02:22 PM
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
#2
Posted 11 December 2012 - 05:24 AM
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?
#3
Posted 11 December 2012 - 12:33 PM
Jimlam, on 11 December 2012 - 05:24 AM, said:
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'), ..... ));
If you liked my post, just give it a +1. that's all!
#4
Posted 11 December 2012 - 02:08 PM
faridplus, on 11 December 2012 - 12:33 PM, said:
$this->widgets('zii.widgets.gird.CGridView', array( ..... 'ajaxUrl' => array('controller/action'), ..... ));
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.
#5
Posted 11 December 2012 - 02:11 PM
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.
#6
Posted 11 December 2012 - 02:44 PM
seenivasan, on 11 December 2012 - 02:11 PM, said:
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.
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

#7
Posted 13 December 2012 - 08:32 AM
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
#8
Posted 13 December 2012 - 09:03 AM
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
#9
Posted 26 January 2013 - 11:53 PM
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'=>'tbl-orders-grid',
'dataProvider'=>$dataProvider,
'afterAjaxUpdate'=>"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'}));
}",
'filter'=>$model,
'columns'=>array(
array(
'name'=>'id',
'type'=>'raw',
'value'=>'str_pad($data->id,8-strlen($data->id),"0",STR_PAD_LEFT)',
'htmlOptions'=>array('style' => 'text-align: right;width: 60px;')
),
array(
'name'=>'date_order_placed',
'filter'=>$dateisOn,
'value'=>'date("d-m-Y",strtotime($data->date_order_placed))',
),
array(
'name'=>'status_holder',
'type'=>'raw',
//'htmlOptions'=>array('id' => 'order_status_search'),
'value'=>'CHtml::value($data,"status.orders_status_name")',
'filter'=>CHtml::listData(OrderStatus::model()->findAll(
array(
'select'=>array('orders_status_name'),
'distinct'=>true
)),"orders_status_name","orders_status_name")//this is the focus of your code
),
array(
'class'=>'EButtonColumnWithClearFilters',
'clearVisible'=>true,
'template'=>'{view}{email}',
'buttons'=>array
(
'email' => array
(
'label'=>'Reprint invoice and email to yourself',
'imageUrl'=>Yii::app()->request->baseUrl.'/images/email.png',
'url'=>'Yii::app()->createUrl("orders/ureprint", array("id"=>$data->id))',
),
),
),
),
)); ?>
The other filters, dates and id, work perfectly.
any help would be really appreciated
