0

I have a h:commandButton with an atribute onclick="this.form.target='_blank'" for open a PDF in new tab and it works perfectly, but after this action, all other actions (h:commandLink or h:commnadButton) without target attribute open in new tab too.

fragmat from xhtml file

<h:form>
 <rich:panel header="User Logs Data">
   <h:panelGrid id="ad" columns="2" columnClasses="fieldlabel,fieldvalue">

      <h:outputText value=" Branch:" />
      <h:selectOneMenu value="#{userLogsInformationController.branch}"
                            converter="#{bankBranchConverter}"
                            disabled="#{not sec:areAllGranted('SHOW_ALL_OUTWARD')}"
                            onchange="submit()"
                            valueChangeListener="#{userLogsInformationController.handleBranchChange}"
                            style="width : 250px;">
     <f:selectItem itemLabel="[Select Branch]"/>
     <f:selectItems value="#{userLogsInformationController.branches}" 
                    var="br" itemLabel="#{br.name}"/>
     </h:selectOneMenu>
  </h:panelGrid>
  <h:panelGrid columns="2">
     <h:commandButton value="Report">
         <f:ajax event="click" execute="@form" listener="#{userLogsInformationController.showReport}" render="@form" />
     </h:commandButton>
     <h:commandButton value="Download" 
                      actionListener="#{userLogsInformationController.downloadReport}"
                      onclick="this.form.target='_blank'" ajax = "false"
                      disabled="#{userLogsInformationController.notAbleToPrint}"/>

     <rich:messages id="messages" style="padding-left:40px; margin-top: 5px"/>
   </h:panelGrid>
  </rich:panel>
 </h:form>

I have researched some issues and found that for the first time, target equals _blank it's added to the form as attribute. Second time, target="_blank" is still present in form, while it shouldn't (target parameter is undefined at this point).

I am not getting the solution and cant able to add the method in the page. Need some guide on how to fix this issue.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Shahriar Miraj
  • 307
  • 3
  • 14
  • Solution is very simple. Put the download button in its own non-nested form. – Kukeltje May 29 '18 at 07:08
  • Thanks Kukeltje, its fine now – Shahriar Miraj May 29 '18 at 07:54
  • Great it works. Please create an answer and accept it. Add the corrected code and some textual explanation – Kukeltje May 29 '18 at 08:41
  • But another issue arises. though I have to move the download to another form, whenever I am firing the function, other properties seems not populated that time. Do you understand what I am saying ? – Shahriar Miraj May 29 '18 at 08:57
  • Have you tried with `commandLink` instead? https://stackoverflow.com/questions/14066973/target-blank-in-hcommandlink-not-opening-page-in-new-tab-of-browser – Aritz May 29 '18 at 09:12
  • Which version of PF are you using? I have fixed this problem in 6.1, see https://github.com/primefaces/primefaces/issues/2164 – Rapster May 29 '18 at 09:18
  • Thanks Rapster. I have checked your answer before posting this question and I got the proper workflow from your answer to that question. I have solved this problem and add in below. – Shahriar Miraj May 29 '18 at 09:26
  • Submitting those others via ajax in their own form... – Kukeltje May 29 '18 at 09:29

1 Answers1

4

This answer is collected from another source. When we restrict the target only to a single action, we need to get the form in its initial state. With the oncklick action we set the target of the form to a _blank page. After the click, the page is opened in a new tab/page (triggers the action event). At last, the onblur action is triggered and set the form again to its initial state (the rest of the buttons will open in the _self page) With this code, we can restrict to only a h:commandbutton to open in a new page

<h:commandButton value="Download"
                 actionListener="#{userLogsInformationController.downloadReport}"
                 onclick="this.form.target='_blank'"
                 onblur="this.form.target='_self'"
                 ajax = "false" disabled="#{userLogsInformationController.notAbleToPrint}"/>
Shahriar Miraj
  • 307
  • 3
  • 14