Yii Framework Forum: [Solved] How To Pass The Order From Cgridview In Admin View To Another View - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[Solved] How To Pass The Order From Cgridview In Admin View To Another View Rate Topic: -----

#1 User is offline   cesarc 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 18-October 12

Posted 09 February 2013 - 10:43 AM

Hi
I want to pass the order from cgridview in admin view, that was seted clicking in title column, to another view

array('label'=>Yii::t('app','Report'), 'url'=>array('report','orderto'=>$orderto)),

How to get the value to pass in var $orderto ?


Thanks.
0

#2 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 09 February 2013 - 02:08 PM

Dear Friend

We can make the all the GET parameters stateful by storing them in session.

Then we can make use of it at any place.

I have a model Post.

PostController.php
public function actionAdmin()
	{
		$model=new Post('search');
		$model->unsetAttributes(); 
		if(isset($_GET['Post'])){
			$model->attributes=$_GET['Post'];
			
		}
		Yii::app()->user->setState("grid",$_GET);//storing all the GET parameters in session.
		
		$this->render('admin',array(
			'model'=>$model,
		));
	}

//For an example ,Now we can make use of it in actionIndex


public function actionIndex()
	{   $sort="";
		if(Yii::app()->user->hasState("grid"))
		{
				$grid=Yii::app()->user->getState('grid');
				if(isset($grid["Post_sort"]))
				{
					$sort=$grid['Post_sort'];
					$dot=strpos($sort,'.');
					if($dot>0)
					{
						$prop=substr($sort,0,$dot);
						$order=substr($sort,$dot+1);
						$order=strtoupper($order);
						$sort=$prop." ".$order;
					}
				}
				Yii::app()->user->setState('grid',null);
	    }
	    
		$dataProvider=new CActiveDataProvider('Post',array(
			'sort'=>array('defaultOrder'=>$sort),
		));
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
			
		));
	}


Rather than strong in session, if you want a link with get parameter the following can be done.

You have to declare beforeAjaxUpdate property in CGridView.

<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'post-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'beforeAjaxUpdate'=>'js:function(id,options)
          {
		var url=options.url;
		var arr=url.split("&");
		arr.shift();
		var params={};
	        for each(var e in arr)
	        {
	           var el=e.split("=");
	           params[el[0]]=el[1];
	        }
	        $("#link").attr("href","index.php?r=post/index"+"&"+params.Post_sort);
	   }',
	'columns'=>array(
		'id',
		'title',
		'created',
...............................................................................
..............................................................................


Then we have to add a link in following way.

echo CHtml::link("index","#",array('id'=>'link'));


I hope I helped a bit.
0

#3 User is offline   cesarc 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 18-October 12

Posted 10 February 2013 - 01:05 PM

Thanks very much seenivasan!

I copy and paste your reply and it work! I still don't understand how :)
I implemented the first solution.

My controller
public function actionAdmin()
	{
		$model=new Peliculas('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Peliculas']))
			$model->attributes=$_GET['Peliculas'];
		Yii::app()->user->setState("grid",$_GET);//storing all the GET parameters in session.
		$this->render('admin',array(
			'model'=>$model,
		));
	}


My view
if(Yii::app()->user->hasState("grid"))
{
	$grid=Yii::app()->user->getState('grid');
	if(isset($grid["Peliculas_sort"]))
	{
		$sort=$grid['Peliculas_sort'];
		$dot=strpos($sort,'.');
		if($dot>0)
		{
				$prop=substr($sort,0,$dot);
				$order=substr($sort,$dot+1);
				$order=strtoupper($order);
				$sort=$prop." ".$order;
		}
	}
	Yii::app()->user->setState('grid',null);
}
            

$criteria= $_SESSION['filtro'];
$criteria->order = $sort;
$dataProvider=new CActiveDataProvider('Peliculas', array(
    'criteria'=>$criteria,
    'pagination'=>false,
));
$data=$dataProvider->getData();
...


My model, search() function:
public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',trim($this->id));
		$criteria->compare('lower(catalogo)',strtolower($this->catalogo),true);
		$criteria->compare('id_pelicula',trim($this->id_pelicula));
		$criteria->compare('lower(titulo)',strtolower($this->titulo),true);
		$criteria->compare('lower(actores)',strtolower($this->actores),true);
		$criteria->compare('lower(director)',strtolower($this->director),true);
		
		$_SESSION['filtro']=$criteria;
		
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users