Ajax事件在第二次单击时起作用,但在第一次单击时不起作用

时间:2017-05-16 08:38:09

标签: ajax jsf events primefaces jsf-2.2

我正在尝试调用一个对话框来发送一个表单,但在我需要收集一些数据并从 btn1 按钮传递之前。

问题是这个代码在第一次点击时不起作用(comandButton type =" button"),但它在第二次点击时正常工作。

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

0 个答案:

没有答案