1

So here's the thing. I have a popup that has a button, the button itself has a fileDownloadActionListener, this one is responsible for downloading an excel file. So what I need basically is to hide the popup right after I generate the file.

Here's my .jspx file (Just the popup)

    <af:popup childCreation="deferred" autoCancel="enabled"
              id="myPopUp"
              contentDelivery="lazyUncached"
              binding="#{viewScope.mbMyBean.myPopUp}"
              partialTriggers="b17">
        <af:dialog id="d16" type="cancel"
                   title="Do you wish to download a file?"
                   inlineStyle="width:400px;">
            <af:panelGroupLayout id="pgl32"
                                 inlineStyle="max-width: 200px;">
            <af:outputText value="You're about to download a file. Ready?" id="ot45"
                               />
            </af:panelGroupLayout>
            <f:facet name="buttonBar">
                <af:button text="GO" id="b17"
                    <af:fileDownloadActionListener contentType="excelHTML"
                                                   filename="#{viewScope.mbMyBean.FileName}"
                                                   method="#{viewScope.mbMyBean.GenerateEmptyExcel}"
                                                   />
                </af:button>
            </f:facet>
        </af:dialog>
    </af:popup>

And here's the java method:

public void GenerateEmptyExcel(FacesContext facesContext, OutputStream outputStream) {

    try {


        HSSFWorkbook wb1 = generateEmptyExcelFile();
        wb1.write(outputStream);


        outputStream.flush();
        outputStream.close();

        this.myPopUp.hide();

        AdfFacesContext.getCurrentInstance().addPartialTarget(this.myPopUp);

        System.gc();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

PROBLEM

The popup won't hide.

NOTES

  1. The popup is properly binded within the bean
  2. I do not own this code and I'm doing a maintainance.
  3. I do not know why the programmer used System.gc() since I consider it as a bad practice. Here's a good reason
Community
  • 1
  • 1
Luis Deras
  • 1,239
  • 2
  • 19
  • 48

2 Answers2

1

Ideally this.myPopUp.hide(); should close the popup but if it is not working for some reason, you can try closing the popup using javascript like:

public static void hidePopup(String popupId){
   if (popupId != null)
   {
     ExtendedRenderKitService service =
       Service.getRenderKitService(FacesContext.getCurrentInstance(),
                                   ExtendedRenderKitService.class);    
     StringBuffer hidePopup = new StringBuffer();             
      hidePopup.append("var popupObj=AdfPage.PAGE.findComponent('" + popupId +
       "'); popupObj.hide();");
     service.addScript(FacesContext.getCurrentInstance(), hidePopup.toString());
   }
 }

You can get the popup clientId that you can pass into hidePopup using: this.myPopUp.getClientId(FacesContext.getCurrentInstance());

amishra
  • 951
  • 9
  • 12
  • Is it org.apache.myfaces.trinidad.util.Service? – Luis Deras Nov 23 '16 at 19:49
  • It doesn't seem to work. the popup.getCientID() is returning something diffrent than the actual rendered html. The actual rendered html has ::content after the popupID. Either way, I try with both and still no response – Luis Deras Nov 23 '16 at 20:20
  • Did you set clientComponent="true" for popup? It is required in order for popup to be accessed in javascript – amishra Nov 23 '16 at 20:31
  • One other thing to check would be the code for generating the excel . It may be failing and hence the code for hiding the popup never getting executed. Try to comment that portion of the code out and see if you are still not able to hide the popup – amishra Nov 23 '16 at 21:00
  • I've tried both things and just can't get it to work. Could this be related to the browser? – Luis Deras Nov 23 '16 at 21:20
  • Could be, what is the browser you are using? Also, make sure your page/page fragment which contains popup does not have any errors (any red or even orange color markers) – amishra Nov 23 '16 at 21:49
1

I had the same problem after downloading a file, you should try this:

Use a resource type javascript to trigger an event click in commandButton

<af:resource type="javascript">              

          function customHandler(evt) {
              console.log(evt);

              var exportCmd = AdfPage.PAGE.findComponentByAbsoluteId("pt1:b17");
              console.log(exportCmd);
              var actionEvent = new AdfActionEvent(exportCmd);
              console.log(actionEvent);
              actionEvent.forceFullSubmit();
              actionEvent.noResponseExpected();
              actionEvent.queue(false);

              setTimeout(function(){hidePopup();}, 1000);    


          }                                    

          function hidePopup() {

              var popup = AdfPage.PAGE.findComponent("pt1:popupAceptarDescargarPlantilla::content");

              popup.hide();

          }

        </af:resource>

You should have the following buttons:

<af:commandButton text="Aceptar" id="b17" visible="false" clientComponent="true" partialSubmit="true">
                                                                        <af:fileDownloadActionListener contentType="excelHTML" filename="#{viewScope.mbGestionArchivos.nombre_archivo}" method="#{viewScope.mbGestionArchivos.generateExcelVacio}"/>
                                                                    </af:commandButton>
                                                                    <af:button text="Aceptar" id="botonPrueba" actionListener="#{viewScope.mbInformeDetalle.prepareForDownloadAction}" clientComponent="true" partialSubmit="true"></af:button>

This is the java method called by the button :

    public void prepareForDownloadAction(ActionEvent act) {

    FacesContext context = FacesContext.getCurrentInstance();
    ExtendedRenderKitService erks =
    Service.getService(context.getRenderKit(),
           ExtendedRenderKitService.class);

    erks.addScript(context, "customHandler();");
    }

The hidden button is triggered using javascript a ADF methods, the magic happen in the setTimeout, When executing this function we avoid for a second to make submit but the request travels to the server, here we can observe how the file is started.

csuazo
  • 164
  • 3
  • 20