Yii Framework Forum: Exporting Only Filtered Results From Cgridview - Yii Framework Forum

Jump to content

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

Exporting Only Filtered Results From Cgridview Rate Topic: -----

#1 User is offline   baggeconsulting 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 26-January 13

Posted 26 January 2013 - 10:40 PM

Hi ALL,

Trying to export the customers to CSV --- the results filtered on grid. But I am getting ALL of them instead of the filtered.

You will be able to see this action on my controller:
/* Action Export To File
****************************************************************************/
public function actionExportToFile() {
//echo 'test';
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="report-customers-' . date('YmdHi') .'.csv"');

$model=new Customer('search');
$model->unsetAttributes(); // clear any default values

if(isset($_GET['Customer'])) {
$model->attributes=$_GET['Customer'];
}

$dataProvider = $model->search(false);

// csv header
echo Customer::model()->getAttributeLabel("Id").",".
Customer::model()->getAttributeLabel("Country_Id").",".
Customer::model()->getAttributeLabel("Gender_Id").",".
Customer::model()->getAttributeLabel("First_Name").",".
Customer::model()->getAttributeLabel("Last_Name").
" \r\n";
// csv data
foreach ($dataProvider->getData() as $data) {
echo "$data->Id, $data->Country_Id, $data->Gender_Id, $data->First_Name, $data->Last_Name \r\n";
}

exit;
}
/*******************************************************************************/


And I would like keep using AJAX on my filter --- So How can I do that ? I am missing some POST/GET ? --- I have followed a lot of stuff on the web but unfortunately still I couldn't figured it out. ----------------- Thanks in advance for this help.

See attached files.

Cheers!

Attached File(s)


0

#2 User is offline   Reza m 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 148
  • Joined: 07-March 12

Posted 27 January 2013 - 03:54 AM

you can achive this as follows:


public function actionAdmin() {
		$model=new Customer('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Customer']))
			$model->attributes=$_GET['Customer'];
                
                Yii::app()->user->setState('exportModel',$model); //this can be any other storage

		$this->render('admin',array(
			'model'=>$model,
		));
	}

public function actionExportToFile() {
		//echo 'test';
		header('Content-type: text/csv');
		header('Content-Disposition: attachment; filename="report-customers-' . date('YmdHi') .'.csv"');

		$model=new Customer('search');
		$model->unsetAttributes();  // clear any default values
		
		if(Yii::app()->user->getState('exportModel'))
                      $model=Yii::app()->user->getState('exportModel');

		$dataProvider = $model->search(false);
		
		// csv header
		echo 	Customer::model()->getAttributeLabel("Id").",". 
				Customer::model()->getAttributeLabel("Country_Id").",". 
				Customer::model()->getAttributeLabel("Gender_Id").",".
				Customer::model()->getAttributeLabel("First_Name").",".
				Customer::model()->getAttributeLabel("Last_Name").
				" \r\n";
		// csv data
		foreach ($dataProvider->getData() as $data) {
			echo "$data->Id, $data->Country_Id, $data->Gender_Id, $data->First_Name, $data->Last_Name \r\n";
		}
		
		exit; 
	}

0

#3 User is offline   baggeconsulting 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 26-January 13

Posted 27 January 2013 - 08:53 AM

It is working now. I fixed it using $_SESSION on the search() method which is into the customer model class.
And then I call it from the controller (actionExportToFile).

Anyway Thanks a lot.
0

#4 User is offline   baggeconsulting 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 26-January 13

Posted 27 January 2013 - 07:02 PM

Reza m: Just I was thinking your solution is better. I'll be doing it on that way.

Many thanks for your time.
0

#5 User is offline   karauzum 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 28-May 13

Posted 30 May 2013 - 04:51 AM

Mr. i want to export to excel filtered result and dont know how,

i tried your codes but couldt do it.

How did you do it? Can you give me an example?


Ok i got it with mr Raza m example. Thank you...
0

#6 User is offline   quinlena 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 01-July 13

Posted 01 July 2013 - 05:29 AM

I have a page navigation, not all records are unloaded. How do export regardless of the pagination?
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