Yii 1.1: csvout

Generates csv file output for selected tables on a page
5 followers

This extension generates csv file output for selected tables on a page, multiple selection is allowed

Documentation

Requirements

  • Yii 1.0.5

Installation

  • Copy all the 'csv' catalogue under /protected/extensions

  • Register controller 'CsvController' in /protected/config/main.php:

[php]

 'controllerMap'=>array(
          'csv'=>array(
              'class'=>'application.extensions.csv.CsvController',
              'property1'=>'value1',
              'property2'=>'value2',
              ),
      ...........other controllers (if exist)
 ),

[php]

Usage

  • Add button(s) to your view page (several times, if needed - as many as you wish!):

[php]

    <?php $this->widget('application.extensions.csv.csvWidget', array(
                    'property1'=>'value1',
                    'property2'=>'value2')); 
     ?>

[php]

where possible properties with their default values are:

  • 'table' => '.dataGrid' string selector (understanding by jQuery $()-function) of the table(s) to be output, by default it's a 'table' with class='dataGrid' (selection result may be not single!)

  • 'csvFile' => '' string csv file name (if '', it will be 'data.csv' by default)

  • 'ignoredRow' => 'filterRow' string class name for the rows 'tr' which should be omitted in the csv output

  • 'ignoredCol' => 'actionCol' string class name for the cells 'td', 'th' to be omitted in the csv output

  • 'htmlOptions' => array() array htmlOptions array - standard for Yii

Change Log

July 5, 2009

  • Initial release.

Total 8 comments

#13556 report it
laqrhead at 2013/06/05 07:02pm
Strip HTML from header rows (and all other rows).

If HTML is screwing up your CSV, change line 106 from:

input.setAttribute('value', cells[k].innerHTML);

to:

input.setAttribute('value', jQuery(cells[k].innerHTML).text());

Great extension!

#5806 report it
gukan at 2011/11/15 05:02am
If pagination

Hi,

Its not working fine when i have pagination. I like to export full table

#3118 report it
Rohit B at 2011/03/17 03:32am
Using csvout with Yii 1.1.6

Hello,

I added the changes suggested by seb and luoshiben into the package.

Also, I noticed that once the results were filtered through an Ajax call, the changes were not reflected in the CSV export. For this, the existing form (id=csvExportForm) has to removed.

\$(\"#csvExportForm\").remove();

This line goes before the formCsv initialisation. With this, there is also no need for the firstTime variable and its check. Thus, we get correct CSV export after each search operation.

Tested in IE8, Firefox 3.6 and Chrome 10.

#681 report it
luoshiben at 2010/03/19 10:57pm
two issues

Thanks for the extension. I had two issues and will give my solutions for both:

  1. In IE8 (didn't test other versions of IE) when I clicked on the export button I got the error "formCSV is null or is not an object" and the file was not downloaded. I solved this problem by replacing line 92 of csvWidget.php with:
    
    formCsv = \$('<form>').attr({id: 'csvExportForm', method: 'POST', action: '".Yii::app()->urlManager->createUrl('csv/index')."'});
    $('body').append(formCsv);
    
  2. I noticed that no escaping or enclosing of characters was being done when lines were added to the export. I borrowed an example from comments on the php man page for the fputcsv function and added the following to CsvController.php: A. At the very top of the actionIndex method I added:
    
    $delimiter = ',';
    $enclosure = '"';
    $delimiter_esc = preg_quote($delimiter, '/');
    $enclosure_esc = preg_quote($enclosure, '/');
    
    B. Then, inside of the "foreach($table as $row)" section I replaced the code to look like this:
    
    foreach($row as $indice=>$field)
    {
        if(preg_match("/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $field))
            $row[$indice] = $enclosure . str_replace($enclosure, $enclosure . $enclosure, $field) . $enclosure;
    }
    $outputTable[] = join($delimiter, $row);
    
    Hope that helps someone.
#1121 report it
gallego123 at 2009/12/03 06:31am
forgiveness for confusing

see

http://www.yiiframework.com/forum/index.php?/topic/4213-csvout-install-problem/

greetings

#1127 report it
gallego123 at 2009/12/02 08:17am
correction

var dato=cells|k|.innerHTML;

var index_a2= dato.lastIndexOf("< / a >");

if (index_a2!=-1) {

var index_a1= dato.substring(0,index_a2).lastIndexOf(">")+1;

var dato_link= dato.substring(index_a1,index_a2);

input.setAttribute('value', dato_link); } else input.setAttribute('value', cells|k|.innerHTML);

#1129 report it
gallego123 at 2009/12/02 07:38am
title link for sort

hi seb your solution not work for me

mi solution :

var dato=cells[k].innerHTML;

var index_a2= dato.lastIndexOf(\"\");

if (index_a2!=-1) {

var index_a1= dato.substring(0,index_a2).lastIndexOf(\">\")+1;

var dato_link= dato.substring(index_a1,index_a2);

input.setAttribute('value', dato_link); } else input.setAttribute('value', cells[k].innerHTML);

i'm using PHPExcel if you are interested, ask me

greetings

#1513 report it
seb at 2009/07/08 08:57am
Fix: links are exported as <a href ...>

Column headers often contains links for sorting. Now this links exported as < a href ... >Text< /a>.

To fix this we need to change exportToCsv() JS function in csvWidget.php:

function exportToCsv()
{
...
if (cells[k].className.indexOf('{$this->ignoredCol}') === -1) {
    var input = document.createElement('input');
    input.setAttribute('type', 'hidden');
    input.setAttribute('name', 'tCells['+i+']['+j+']['+k+']');
    //check if cell contains a link
    if(cells[k].firstChild.nodeName.toLowerCase()!=='a'){
        input.setAttribute('value', cells[k].innerHTML);
    } else {
input.setAttribute('value', cells[k].firstChild.innerHTML); } formCsv.appendChild(input); } ... }

Leave a comment

Please to leave your comment.

Create extension
Downloads
  • Yii Version: 1.1
  • License: New BSD License
  • Developed by: kvl
  • Category: Others
  • Votes: +6 / -1
  • Downloaded: 1,014 times
  • Created on: Jul 5, 2009
  • Last updated: Jul 5, 2009