0

Im trying to perform a click in a button using javascript, I want to execute a method which is supposed to run in a backing bean.

This is my resource

       <af:resource type="javascript">
          function closePopup(event) {
              //var dialog = event.getSource();
              //var popup = dialog.findComponent("pt1:b17");
              //console.log(popup);
              //popup.click();
              //$("#pt1:b17").trigger("click");
              //popup.hide();
              //event.cancel(); document.getElementById('pt1:b17')
              console.log("trigger the event");

              eventFire(document.getElementById(AdfPage.PAGE.findComponentByAbsoluteId('pt1:b17').getClientId()), 'click');
          }

          function eventFire(el, etype) {

              if (el.fireEvent) {
                  console.log("true");
                  el.fireEvent('on' + etype);
              }
              else {
                  console.log("false");
                  var evObj = document.createEvent('Events');
                  evObj.initEvent(etype, true, false);
                  el.dispatchEvent(evObj);
              }
          }
        </af:resource>

Now this is my jsp code:

                                                                 <af:group id="g4">
                                                                    <af:commandButton text="Aceptar" id="b17">
                                                                        <af:fileDownloadActionListener contentType="excelHTML" filename="#{viewScope.mbFiles.file_name}" method="#{viewScope.mbFiles.generateFile}"/>
                                                                    </af:commandButton>
                                                                    <af:button text="test" id="buttonTest">
                                                                        <af:clientListener method="closePoPup" type="action"/>
                                                                    </af:button>
                                                                </af:group>

I cant trigger the method that download my file.

Thanks in advance.

divibisan
  • 11,659
  • 11
  • 40
  • 58
csuazo
  • 164
  • 3
  • 20

1 Answers1

0

I post the answer in another question:

https://stackoverflow.com/a/41708683/5120410

The code is like this:

The method in Java:

  public void prepareForDownloadAction(ActionEvent act) {

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

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

Now this is my method in Javascript:

<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>

Greatings.

Community
  • 1
  • 1
csuazo
  • 164
  • 3
  • 20