【问题标题】:Enter Event on InputText for CommandButton listener在 CommandButton 侦听器的 InputText 上输入事件
【发布时间】:2012-08-23 13:19:03
【问题描述】:

我有一个<p:inputText value="#{beans.text}" /> 和一个<p:commandButton actionlistener="#{beans.method}" />。 如何通过 inputText 中的“输入”从 Button 执行 actinoListener-Method?

感谢解答

出了点问题。这是代码-sn-p->

<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'-event,regexSave-Method 将被执行。但是 saveOrUpdateHour 的方法也是....


出了点问题。这是代码-sn-p->

<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'-event,regexSave-Method 将被执行。但是 saveOrUpdateHour 的方法也是....

同时我已经配置了我的代码。现在文本字段和按钮在我的表格的标题中。

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

【问题讨论】:

  • 问题中缺少一些单词。我猜你希望你说你有一个 inputText 和一个 commandButton
  • 输入文本和命令按钮的格式一样吗?只有一个命令按钮?你为什么不包括你的 xhtml 的 sn-p 来澄清这一点?
  • 同时我已经配置了我的代码。现在文本字段和按钮在我的表格的标题中。

标签: user-interface jsf-2 primefaces


【解决方案1】:

您可以使用p:inputText 中的onkeypress 并在jquery 的帮助下调用按钮上的.click(),注意return false; 将确保在输入后不会调用任何内容(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 尝试update="tableGrid"(请参阅表格包装器)或者如果您使用的是primefaces 3.4,请尝试update="hoursList"

【讨论】:

  • 我已经测试过了。它只有在我删除命令按钮更新中的“:editContent:hoursList”时才有效......如果我删除我的表将不会用新数据刷新......
  • 什么表?然后您应该更新问题中的代码...在您更新表格的地方看不到任何:editContent:hoursList
  • 用更新 tableGrid 和更新 hoursList 测试你的想法,但两者都不起作用。呼叫计数没有区别......你是什么意思增加你的acaptenace率?我是新来的对不起
猜你喜欢
  • 2019-11-01
  • 2017-05-03
  • 1970-01-01
  • 2018-09-04
  • 1970-01-01
  • 2020-09-06
  • 1970-01-01
  • 2012-09-20
  • 1970-01-01
相关资源
最近更新 更多