I am trying to export cgridview data (current search results) to a CSV by using the excelview extension, by clicking a button.
However, I am not getting the filtered data in my CSV; instead, I’m getting all the records in the model.
This is my controller action I am calling:
public function actionExcel()
{
$model = new PackagingMetric('search');
$this->widget('application.extensions.EExcelView', array(
'dataProvider'=> $model->search(),
'grid_mode'=>'export',
'exportType'=>'Excel5',
'filename'=>'report',
));
}
Would anyone know how to resolve this issue, or where my error is ?
You don’t have any search criteria there. Usually you do it like this:
I don’t see anything in your code that takes values from user input, so you should use your search form somehow. Of course, you could have default values in your model, but I don’t think that’s the case.
Edit:
I looked a bit more into the source code of the extension you are using. It seems that the export buttons are actually standard
<a href=''></a>links that send you to a page that’s supposed to output the Excel sheet. However, because the actual filter data is not transmitted to that page, there’s no way to apply those filters server-side.Since this is the intended behavior of the extension, there’s no elegant way to solve it, but there are messy ways.
A first option would be to tweak the extension yourself, but this will break compatibility with future versions. Another way is to use Javascript to send your data where you need it. In the view file where you are displaying the gridview, you should wrap the widget in a
formelement or in anActiveFormwith all validation disabled. Then you need to place the following piece of Javascript code somewhere on the page:This will submit the form to the address specified by the export link. Because the form wraps the widget, it will contain the input elements that filter your results, so you will have access to the user-entered filters on the server. You can use the code I originally posted to pick up the data from there.
Note: I am using the default class name for the
divthat contains the export buttons, which is.summary. If you are using a different class, you should change the respective JS code.