p:commandButton Call a javascript function if ajax=false
Posted: 06 Dec 2011, 18:46
Hi everybody,
I have a commandButton which is calling a java method to generate an excel file, my question is : is it possible to call a javascript function once the file has been generated ?
Here is the code I use in the jsp :
and the java method generating the excel file :
The excel file is generated, so everthing is fine. But since I put ajax="false" the flag oncomplete="myJavascriptFunction()" does not work, so how can I call a javascript function once the excel file has been downloaded ? Is it even possible ???
The funny thing is that when I put ajax="true" the javascript function is called, but the file is not downloaded !!!
I have searched in the PrimeFaces user's guide, there I found that CommandButton component submits it's enclosed form, so I tried specifying the javascript function in the onsubmit flag of the form, but this function is not called after but before the file is generated. So this option does not solve my problem.
Has anyone any idea ???
Thx
I have a commandButton which is calling a java method to generate an excel file, my question is : is it possible to call a javascript function once the file has been generated ?
Here is the code I use in the jsp :
Code: Select all
<p:commandButton id="extractionDataRegional"
actionListener="#{exportController.doExport}" ajax="false"
oncomplete="myJavascriptFunction()"
value="Do export" update="messages" />
Code: Select all
public static void doExport() throws IOException, InvalidFormatException {
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext()
.getResponse();
response.setContentType("application/vnd.ms-excel");
String headerResponse = "attachment;filename=";
headerResponse = headerResponse.concat(fileName);
response.addHeader("Content-disposition", headerResponse);
ServletOutputStream out = response.getOutputStream();
Workbook wb = WorkbookFactory.create(inputStream);
...........
wb.write(out);
out.flush();
out.close();
context.responseComplete();
The funny thing is that when I put ajax="true" the javascript function is called, but the file is not downloaded !!!
I have searched in the PrimeFaces user's guide, there I found that CommandButton component submits it's enclosed form, so I tried specifying the javascript function in the onsubmit flag of the form, but this function is not called after but before the file is generated. So this option does not solve my problem.
Has anyone any idea ???
Thx