YII: Extended cgridview Add export CSV function

Source: Internet
Author: User
Tags vars

The Cgridview component provided by Yii does not have a built-in data export capability, but we can add that functionality by extending the component.

Here's how:

1, first derive a subclass, add an action member, in the view of the INIT function to determine whether the browse action or data export action, if the browser is the action to maintain the default behavior, otherwise output CSV file.

[PHP]View Plaincopy
    1. public function  init ()   
    2. {  
    3.     if ( $this->action ==  ' export ')   
    4.     {  
    5.          parent::init ();   
    6.           $this->gencsv ();   
    7.     }   
    8.     ELSE  
    9.     {  
    10.          Parent::init ();   
    11.     }  
    12. }  

2, processing the output of the CSV file:

[PHP]View Plaincopy
  1. protected function gencsv ()
  2. {
  3. Header ("CONTENT-TYPE:TEXT/CSV;  charset=gb2312 ");
  4. Header (' content-disposition:attachment; filename= '. $this->filename.  "');
  5. //add Your content dump codes here
  6. Flush ();
  7. }

3. Then add a CSV export button to the Table control interface

Override its Renderitems () method as follows:

[PHP]View Plaincopy
  1. Public function Renderitems ()
  2. {
  3. if (Yii::app ()->user->checkaccess (' Administrator '))
  4. {
  5. echo ' <div class= ' ToolBar > ';
  6. echo ' <form action= '. Chtml::normalizeurl (Array ($this->action)).' &id= '. $this->id.  ' "method=" post ">";
  7. foreach ($this->getcontroller ()->getactionparams () as $name + = $value)
  8. {
  9. echo ' <input type= ' hidden ' name= '. Addcslashes ($name, '"').'" value= " .  Addcslashes ($value, '"').'/> ';
  10. }
  11. echo ' <input type= "image" title= ". Yii::t (' ifcms ',' Export to CSV ').' src= '. Yii::app ()->theme->baseurl.  '/images/ico-csv.png ' alt= ' Submit ' > ';
  12. echo ' </form> ';
  13. echo ' </div> ';
  14. }
  15. Parent::renderitems ();
  16. }

4, then in the click on the CSV action processing such as ACTIONCSV () render a single table view, the template is as follows

[PHP]View Plaincopy
  1. <?php
  2. $this->widget (' Application.extensions.grid.MyGridView ', array (
  3. ' id ' = 'grid ',
  4. ' action ' = 'export ',
  5. ' dataprovider ' =$dp,
  6. ' columns ' =Array (
  7. Array (
  8. ' header ' =>yii::t (' Statistics ',' Phone '),
  9. ' name ' = 'phone ',
  10. ),
  11. Array (
  12. ' Header ' =>yii::t (' Statistics ',' Count '),
  13. ' name ' = 'count ',
  14. ),
  15. )
  16. ));? >

Note that there is no output before the header setting statement in the 2nd step of the CSV output function above, including the following function:

[Plain]View Plaincopy
    1. Print, Echo, printf, Trigger_error, vprintf, Ob_flush, Var_dump, ReadFile, PassThru

Otherwise the content will only be exported in the browser, but the file download will not occur.

by Iefreer

Reference:

http://stackoverflow.com/questions/8028957/headers-already-sent-by-php

YII: Extended cgridview Add export CSV function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.