You don't have to use jquery or views to do this.  I use this handy little 
function a lot.  I usually put it in a Component but there is no need 
unless you need it elsewhere

public function array_to_csv_download($array, $filename = "export.csv", 
$delimiter=",") {
    // open raw memory as file so no temp files needed, you might run out 
of memory though
    $f = fopen('php://memory', 'w'); 
    // loop over the input array
    foreach ($array as $line) { 
        // generate csv lines from the inner arrays
        fputcsv($f, $line, $delimiter); 
    }
    // rewrind the "file" with the csv lines
    fseek($f, 0);
    // tell the browser it's going to be a csv file
    header('Content-Type: application/csv');
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    // tell the browser we want to save it instead of displaying it
    header('Content-Disposition: attachement; filename="'.$filename.'"');
    // make php send the generated csv lines to the browser
    fpassthru($f);
  }
  
In your controller do

public function export(){
  $this->layout = false;
  $this->autoRender = false;
 
  $this->loadModel('CadastroMailing');
$conditions = array('OR'=>array('cadastro_mailing_tipo_id'=>$dados));
$result = $this->CadastroMailing->find('all',array( 
'conditions'=>$conditions , 'order'=>array('nome ASC')));
  $this->array_to_csv_download($result, 'your_filename', $delimiter=",");
}


and that's it, it will generate a csv automatically when you click a link 
to that page.





On Monday, April 6, 2015 at 2:08:10 PM UTC-4, michel martins wrote:
>
> Hi people!
>
> Well, recently I started working with cakephp and I have a question to 
> generate data for export in a CSV table to excel.
>
> I created the view and controller, and I'm using the class 
> https://github.com/FriendsOfCake/cakephp-csvview .
>
> But when I click, nothing happens. What can be wrong in the process?
>
> //função ajax para capturar o click, jogar no método PHP e retornar o link 
> da planilha para download.
> $(function(){
>  $("#botoes button").click(function(){
> var botao = $(this).attr('data-download'); 
>  $.ajax({
> url:webroot('leads/Leads/export'),
> type:'post',
> data:{
> tipo: botao
> },
> success:function(r){
> $("#botoes button [data-download="+botao+"]").removeClass('btn-danger');
> $("#botoes button [data-download="+botao+"]").addClass('btn-success');
> $("#botoes button [data-download="+botao+"]").val('Download');
>  },error:function(){
> console.log('erro');
> }
>  });
>
> });
>
> });
>
> The method:
>
> public function export(){
> $dados = $this->param('tipo');
>
> $this->loadModel('CadastroMailing');
> $conditions = array('OR'=>array('cadastro_mailing_tipo_id'=>$dados));
> $result = $this->CadastroMailing->find('all',array( 
> 'conditions'=>$conditions , 'order'=>array('nome ASC')));
>  $_serialize = 'result';
>
> $this->viewClass = 'CsvView.Csv';
>              $this->set(compact('data', '_serialize'));
>  }
>
> I would like the download automatically initiated after the click, but if 
> you have to click the button also solves my problem.
>
> Can help me?
>

-- 
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

--- 
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to cake-php+unsubscr...@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

Reply via email to