Exporting Only Filtered Results From Cgridview


(Mauricio Gomez) #1

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!


(Reza Mms) #2

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; 

	}




(Mauricio Gomez) #3

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.


(Mauricio Gomez) #4

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

Many thanks for your time.


(Ismailsekerci) #5

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…


(Lena Frolova) #6

I have a page navigation, not all records are unloaded. How do export regardless of the pagination?