exportablegridbehavior Export filtered GridView as CSV

  1. Changelog
  2. Requirements
  3. Available behavior params
  4. Available behavior methods
  5. Usage
  6. Resources

This behaviour allows you to add an 'Export' button near your gridview, that will download the grid filtered data as CSV.

Changelog

  • v2.0
  • Totally rewritten code
  • Allows export of arrays, single model and CActiveDataProvider ("search" result)
  • Allows export of multiple models on same csv
  • New method (better and cleaner) for generation of export url with grid search filters
  • v1.1
  • Cleaner method of file generation allows to handle huge datasets.
  • Export data is now generated on php output. No more session temporal storage.
  • CSV downloaded is now done without redirections.
  • GridView id automatically fetch from widget

Requirements

  • Yii Framework v1.1.13 or greater (Dependecy with system.web.CDataProviderIterator)
  • An explicit rule matching export action on urlManger like:
'<controller:\w+>/<action:\w+>' => '<controller>/<action>'

Available behavior params

  • 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'
  • buttonId Id for the export button. Default 'export-button'
  • exportParam GET param added to the grid url, representing the 'export' command. Default 'exportCSV'

Available behavior methods

  • public exportCSV(mixed $data, $attributes = array(), $endApplication = true, $endLineCount = 0)
    mixed $data Data to be exported. CModel, CDataProvider or Array.
    Array $attributes Attributes of $data to be exported.
    boolean $endApplication True to end CApplication excecution.
    integer $endLineCount Number of empty rows to add after this data.
  • public isExportRequest()
    returns boolean True if the request have exportParam, False otherwise.
  • public renderExportGridButton(CGridView $gridWidget, $label = 'Export', $htmlOptions = array())
    CGridView $gridWidget CGridView widget.
    String $label Export button label.
    Array $htmlOptions Export button additional HTML options.

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' => ';', //i.e. Excel friendly csv delimiter
            ));
}
Step 3: Some magic

Call the exportCSV() 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'];
        if ($this->isExportRequest()) { //<==== [[ADD THIS BLOCK BEFORE RENDER]]
            //set_time_limit(0); //Uncomment to export lage datasets
            //Add to the csv a single line of text
            $this->exportCSV(array('POSTS WITH FILTER:'), null, false);
            //Add to the csv a single model data with 3 empty rows after the data
            $this->exportCSV($model, array_keys($model->attributeLabels()), false, 3);
            //Add to the csv a lot of models from a CDataProvider
            $this->exportCSV($model->search(), array('id', 'title', 'user.id', 'user.name'));
        }
        $this->render('admin', array(
            'model' => $model,
        ));
    }
    . . .
}
Step 4: Render export button

Add the export button to the View that renders the CGridView.

//Capture your CGridView widget on a variable
//$gridWidget=$this->widget('bootstrap.widgets.TbGridView', array( . . .
$this->renderExportGridButton($gridWidget,'Export Grid Results',array('class'=>'btn btn-info pull-right'));

Resources

6 0
27 followers
2 770 downloads
Yii Version: 1.1
License: Apache-2.0
Category: Others
Developed by: Gero
Created on: Feb 21, 2013
Last updated: 4 years ago

Downloads

show all

Related Extensions