2

ExportableGridBehavior - tinyurl.com/expgrid

This works fantastically with usual dataproviders. But when using with CArrayDataProvider it is causing issues, and giving blank data in the CSV. Is it possible to use this extension with a CArrayDataProvider. What should my syntax in the Controller look like?

Model Code: searchProfitCurrency method

$rawData=Yii::app()->db->createCommand($selectStatment)->queryAll();

    return new CArrayDataProvider($rawData, array(
        'id'=>'id',
        'sort'=>array(
            'attributes'=>array(
                'Currency',
                'profitMarginCurrency'
            ),
        ),
        'pagination'=>array(
            'pageSize'=>50,
        ),
    ));

Controller Code:

$search_dataProvider = $model->searchProfitCurrency();
$this->exportCSV($search_dataProvider, 
  array(
        $search_dataProvider['Currency'],
        $search_dataProvider['profitMarginCurrency'],

  ));
marooned
  • 51
  • 7

1 Answers1

0

I got the same problem and actually I don't know which way could the example of code the author posted and you paste here, work.

I modified the class in a simple way and it works for me now. So

in class ExportableGridBehavior

  if ($data instanceof CActiveDataProvider) {
    $this->csvRowHeaders($fileHandle, $attributes, $data->model);
    $this->csvRowModels($fileHandle, new CDataProviderIterator($data, 150), $attributes);

  } else if ($data instanceof CArrayDataProvider) {   // @luca
    fputcsv($fileHandle, $attributes, $this->csvDelimiter, $this->csvEnclosure);
    foreach ($data->rawData as $i=>$r) {
      $row = array();
      foreach ($attributes as $attr) {
        $row[] = $data->rawData[$i][$attr];
      }
      fputcsv($fileHandle, $row, $this->csvDelimiter, $this->csvEnclosure);
    }


  } else if (is_array($data) && current($data) instanceof CModel) {

and

in Controller

  $dataProvider = new CArrayDataProvider($filteredData, array(
    'keyField' => 'idvr',
    'id'=>'idvr',
    'sort'=>array(          'attributes'=>array('numfor','codfis','driscos','nominativo','codtrib','ravved','impdeb'),
      'defaultOrder'=>array('driscos'=>'ASC')
    ),
    'pagination' => array( 'pageSize'=>50, ),
    'keyField' => 'codfis',
  ));


if ($this->isExportRequest())
  $this->exportCSV($dataProvider, array_keys($dataProvider->rawData[0]));
brasofilo
  • 25,496
  • 15
  • 91
  • 179
Luca Marletta
  • 457
  • 5
  • 13