Friday, April 10, 2015

Re: Export CSV with CakePHP

I managed with your help, guys. Thanks! =D

2015-04-08 12:50 GMT-03:00 John Andersen <j.andersen.lv@gmail.com>:
This looks suspicious to me

url:webroot('leads/Leads/export'),

Should it not be only /leads/export in order to reach your controller method?

Enjoy, John



On Monday, 6 April 2015 21:08:10 UTC+3, 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 a topic in the Google Groups "CakePHP" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cake-php/c1ez9zgZ1hE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cake-php+unsubscribe@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.



--
-- Att,


Michel Martins
michelmfreitas@gmail.com

--
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+unsubscribe@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.

No comments: