This behaviour allows you to add an 'Export' button near your gridview, that will download the grid filtered data as CSV when clicked.
Requirements ¶
Yii Framework v1.1.13 or greater (Dependecy system.web.CDataProviderIterator)
Available behavior params ¶
- attributes Attributes to be exported. Default 'array()'
- gridId Id of the cgridview (if overriden). Default 'yw0'
- buttonId Id of the export button. Default 'export-button'
- stateVariable Nave of the state variable used to persist data to be downloaded. Default '__export'
- exportParam GET param added to the grid update url, representing the 'export' command. Change this value if your grid use the default one. Default 'exportGrid'
- downloadParam GET param added to the grid download url, representing the 'download the exported data' command. Change this value if your grid use the default one. Default 'exportGridD'
- csvDelimiter Delimiter used on the csv generated rows. Default ','
- csvEnclosure Enclosure used on the csv generated rows. Default '"'
- filename Filename poposed to the browser when downloading the file. Default 'export.csv'
Available behavior methods ¶
- public exportGrid($model, $attributes = null)
CModel $model Model with attribute filters already loaded
Array $attributes Attributes of $model to be exported. null to use "attributes" property provided on the behaviour params.
- public renderExportGridButton($label = 'Export', $htmlOptions = array())
String $label the button label
Array $htmlOptions additional HTML attributes of the button.
Usage ¶
Step 1: Place file ¶
Place ExportableGridBehavior.php file on application components directory.
Step 2: Add the behavior to the controller ¶
Add the behavior ExportableGridBehavior to your Controller
public function behaviors() {
return array(
'exportableGrid' => array(
'class' => 'application.components.ExportableGridBehavior',
'filename' => 'PostsWithUsers.csv',
'csvDelimiter' => ';',
'attributes' => array('user.name','title','date',),
));
}
Step 3: Do some magic ¶
Call the exportGrid() method on your 'admin' Action, just before the 'render' method call.
Like This:
class PostController extends CController {
. . .
public function actionAdmin() {
$model = new Post('search');
$model->unsetAttributes();
if (isset($_GET['Post']))
$model->attributes = $_GET['Post'];
$this->exportGrid($model);
$this->render('admin', array(
'model' => $model,
));
}
. . .
}
Step 4: Render export button ¶
Add the export button to the View that renders the CGridView.
$this->renderExportGridButton('Export Grid Results',array('class'=>'btn btn-info pull-right'));
Resources ¶
Total 4 comments
It worked great! Very simple to use
CONGRATULATIONS! :D
I do like your work as it is easy and just what I was looking for.
Beware however, the extension assumes the grid is populated using $model->search(). As one must provide input such as the grid-id, I incorrectly assumed it would magically figure use the same dataprovider as the one actually used by the grid.
It would be awesome if this could be overwritten through parameter.
Thanks
marcoaurelio:
This extension needs Yii 1.1.13 or higher... because it uses CDataProviderIterator.php included in Yii 1.1.13.
If you still want to use this behavior with Yii 1.1.12 or less, please download CDataProviderIterator.php and save it in components folder of your application.
Remember that you also need to import the component like:
Yii::import('application.components.CDataProviderIterator');passei o id da minha grid e o seguinte erro:
include(CDataProviderIterator.php) [function.include]: failed to open stream: No such file or directory (D:\wamp\www\framework\YiiBase.php:418)
Leave a comment
Please login to leave your comment.