在InputText上为CommandButton侦听器输入Event

时间:2012-08-23 13:19:03

标签: user-interface jsf-2 primefaces

我有<p:inputText value="#{beans.text}" /><p:commandButton actionlistener="#{beans.method}" />。 如何在inputText中通过'enter'从Button执行actinoListener-Method?

感谢您的回答

事情出了问题。这是code-snippet-&gt;

<h:panelGrid columns="2">
<p:commandButton actionListener="#{hoursView.saveOrUpdateHour}" />
<p:commandButton actionListener="#{hoursView.onFilterChange()}" oncomplete="dlgHoursFilter.show()" immediate="true" update=":hoursFilter" />
</h:panelGrid>
</h:panelGrid>
<h:panelGrid columns="4" class="buttonContent" id="buttonView">
<p:commandButton ajax="false" actionListener="#{hoursView.showDay()}" immediate="true" update="hoursList" />
<p:commandButton  ajax="false" actionListener="#{hoursView.showWeek()}" immediate="true" update="hoursList" />
<p:commandButton  ajax="false" actionListener="#{hoursView.showMonth()}" immediate="true" update="hoursList" />
<p:commandButton  ajax="false" actionListener="#{hoursView.showAll()}" immediate="true" update="hoursList" />
</h:panelGrid>
         <h:panelGrid columns="3" id="tableGrid">
            <p:dataTable id="hoursList" value="#{hoursView.listHours}"
                var="hours" widgetVar="hoursTable" resizableColumns="true"
                emptyMessage="#{msg.noData}" rowKey="#{hours.id}"
                selection="#{hoursView.selected}" selectionMode="single">

                <p:ajax event="rowDblselect" update=":hourEditDialog"
                    listener="#{hoursView.onHourSelect}"
                    oncomplete="dlgHourEdit.show()" />

                <f:facet name="header">
                    <h:panelGrid columns="3" class="buttonContent" id="regexContent">
                        <h:outputText value="#{msg.regexHour}" />
                        <p:inputText value="#{hoursView.regex}" id="regex">
                        </p:inputText>
                        <p:commandButton icon="addButton" id="regexSave"
                            process="regexContent" actionListener="#{hoursView.regexSave}"
                            update=":editContent:hoursList,:editContent:growl,regex">
                            <p:defaultCommand target="regexSave" scope="regexContent" />
                        </p:commandButton>
                        <p:tooltip for="regexSave" value="#{msg.regexSave}"
                            showEffect="fade" hideEffect="fade" />
                    </h:panelGrid>
                </f:facet>

如果我触发'enter'事件,将执行regexSave-Method。但是saveOrUpdateHour的方法也是....


事情出了问题。这是code-snippet-&gt;

<h:panelGrid columns="2">
<p:commandButton actionListener="#{hoursView.saveOrUpdateHour}" />
<p:commandButton actionListener="#{hoursView.onFilterChange()}" oncomplete="dlgHoursFilter.show()" immediate="true" update=":hoursFilter" />
</h:panelGrid>
</h:panelGrid>
<h:panelGrid columns="4" class="buttonContent" id="buttonView">
<p:commandButton ajax="false" actionListener="#{hoursView.showDay()}" immediate="true" update="hoursList" />
<p:commandButton  ajax="false" actionListener="#{hoursView.showWeek()}" immediate="true" update="hoursList" />
<p:commandButton  ajax="false" actionListener="#{hoursView.showMonth()}" immediate="true" update="hoursList" />
<p:commandButton  ajax="false" actionListener="#{hoursView.showAll()}" immediate="true" update="hoursList" />
</h:panelGrid>
<h:panelGrid columns="3">
<h:outputText value="#{msg.regexHour}" />
<p:inputText value="#{hoursView.regex}" id="regex">
<p:ajax event="change" update="hoursList, growl" listener="#{hoursView.regexSave}" />
</p:inputText>
<p:commandButton  actionListener="#{hoursView.regexSave}" update="hoursList, growl" />
</h:panelGrid>

如果我触发'enter'事件,将执行regexSave-Method。但是saveOrUpdateHour的方法也是....

同时我稍微配置了我的代码。现在,textfield和button位于我表格的标题中。

    <h:panelGrid columns="3" id="tableGrid">
            <p:dataTable id="hoursList" value="#{hoursView.listHours}"
                var="hours" widgetVar="hoursTable" resizableColumns="true"
                emptyMessage="#{msg.noData}" rowKey="#{hours.id}"
                selection="#{hoursView.selected}" selectionMode="single">

                <p:ajax event="rowDblselect" update=":hourEditDialog"
                    listener="#{hoursView.onHourSelect}"
                    oncomplete="dlgHourEdit.show()" />

                <f:facet name="header">
                    <h:panelGrid columns="3" class="buttonContent" id="regexContent">
                        <h:outputText value="#{msg.regexHour}" />
                        <p:inputText value="#{hoursView.regex}" id="regex">
                        </p:inputText>
                        <p:commandButton icon="addButton" id="regexSave"
                            process="regexContent" actionListener="#{hoursView.regexSave}"
                            update=":editContent:hoursList,:editContent:growl,regex">
                            <p:defaultCommand target="regexSave" scope="regexContent" />
                        </p:commandButton>
                        <p:tooltip for="regexSave" value="#{msg.regexSave}"
                            showEffect="fade" hideEffect="fade" />
                    </h:panelGrid>
                </f:facet>   ....

1 个答案:

答案 0 :(得分:3)

您可以使用onkeypress的{​​{1}},并在jquery的帮助下调用按钮上的p:inputText,请注意.click()将确保不执行任何操作(return false; 1}})将在按下回车后调用

像这样:

p:ajax for example

更新 而不是<p:commandButton id="myButtonID" actionlistener="#{beans.method}" /> <p:inputText value="#{beans.text}" onkeypress="if (event.keyCode == 13) { jQuery('#myButtonID').click(); return false;}"/> 尝试:editContent:hoursList(请参阅表格包装)或者如果您使用的是primefaces 3.4,请尝试update="tableGrid"