0

I am trying to call a dialog to send a form inside it, but before I need to collect some data and pass from the button btn1.

The problem is that this code does not work at the first click (comandButton type="button"), but it properly works at the second click.

test.xhtml

<!DOCTYPE html>
<html lang="en"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <f:facet name="first">
            <meta http-equiv="X-UA-Compatible" content="IE=edge" />
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
            <meta name="apple-mobile-web-app-capable" content="yes" />
        </f:facet>
        <h:outputScript name="js/ripple-effect.js" library="rio-layout" />
        <h:outputScript name="js/perfect-scrollbar.js" library="rio-layout" />
        <h:outputScript name="js/layout.js" library="rio-layout" />
        <link rel="icon" sizes="16x16 32x32" type="image/x-icon" href="img/favicon.ico" />
        <link rel="stylesheet" type="text/css" href="css/style.css" />
    </h:head>
    <h:body>
        <h:form id="form">
            <p:dataTable id="table" var="s" value="#{testBean.strings}" 
                         widgetVar="tables">
                <p:column headerText="My string">
                    <h:outputText value="#{s}"/>
                </p:column>
                <p:column headerText="Actions">
                    <!--Button works at the second click-->
                    <p:commandButton id="btn1" type="button"
                                     title="First"
                                     onclick="PF('dlg1').show();">
                        <p:ajax event="click" listener="#{testBean.storeRecord(s)}" 
                                async="true" process="@this"/>
                    </p:commandButton>
                </p:column>
            </p:dataTable>
            <p:dialog id="dlg1" header="Dialog1" widgetVar="dlg1" appendTo="@(body)">
                <h:form id="frmdlg1">
                    <p:panelGrid columns="2" layout="grid">
                        A<p:inputText value="#{testBean.input}"/>
                        B<h:outputText value="#{testBean.result}"/>
                        <f:facet name="footer">
                            <p:commandButton value="save"
                                             title="btndlg1"
                                             oncomplete="PF('dlg1').hide();"
                                             update="form"
                                             action="#{testBean.saveString()}"
                                             process="@form"/>
                        </f:facet>
                    </p:panelGrid>
                </h:form>
            </p:dialog>
        </h:form>
    </h:body>
</html>

TestBean.java

@ViewScoped
@ManagedBean
public class TestBean implements Serializable {

    private static final long serialVersionUID = 657318945087311944L;

    private List<String> strings = new ArrayList<>();
    private String result;
    private String input;

    @PostConstruct
    public void init() {
        for(int i=0; i<=10;i++){
            strings.add(String.valueOf(i));
        }
    }

    //-------------------------------------------
    //  Methods
    //-------------------------------------------
    public void saveString() {
        System.out.println("Inside storeRecord");
        System.out.println("Input es: " + input + "- Result es: " + result);
    }

    public void storeRecord(String s){
        System.out.println("Inside storeRecord");
        result = s;
        System.out.println("Input es: " + input + "- Result es: " + result);
    }

    //  Getters and setters
...
Joe
  • 7,749
  • 19
  • 60
  • 110
  • Any errors in your browsers JavaScript console? – Jasper de Vries May 16 '17 at 09:05
  • This one -> [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/. send @ jquery.js.xhtml?ln=primefaces&v=6.0:25 ajax @ jquery.js.xhtml?ln=primefaces&v=6.0:25 getScript @ core.js.xhtml?ln=primefaces&v=6.0:1 a @ core.js.xhtml?ln=primefaces&v=6.0:3 loadScripts @ core.js.xhtml?ln=primefaces&v=6.0:3 (anonymous) @ VM3843:1 (anonymous) @ jquery.js.xhtml?ln=primefaces&v=6.0:14 ... – Joe May 16 '17 at 09:09
  • It is just a warning, and I upgarded primefaces to version 6.1 and the warning dissapear but not the error. The behaviour remains the same – Joe May 16 '17 at 09:46
  • Point #2 in the duplicate – Kukeltje May 16 '17 at 12:19
  • I had thoght that this is the function of appendTo="@(body)" in the dialog – Joe May 16 '17 at 12:29
  • Taking the dialog out the form it works fine – Joe May 16 '17 at 12:53

0 Answers0