Primefaces按Enter键执行commandButton

10

我正在使用PrimeFaces开发,遇到了以下问题:我有一个工具栏,其中包含多个按钮和表单中的inputtext等组件。但是当我在某个组件中按下Enter键时,工具栏的第一个按钮会被调用。在这种情况下,会调用commandbutton“idButtomNuevo”,因为它在第一位。

我的代码:

almacen.xhtml

<ui:composition  
 xmlns="http://www.w3.org/1999/xhtml"
 xmlns:h="http://java.sun.com/jsf/html"
 xmlns:f="http://java.sun.com/jsf/core"
 xmlns:ui="http://java.sun.com/jsf/facelets"
 xmlns:p="http://primefaces.org/ui" >

 <h:form id="idFormAlmacen" >
    <p:focus context="idFormAlmacen"/>
    <p:growl id="growl" showDetail="true" life="2500" for="keyAlmacen" globalOnly="true"/>
    <p:panel header="Almacen" styleClass="texto-panel"/>
    <ui:include src="/pages/logistica/almacen/toolbar_almacen.xhtml"/>
    <ui:include src="#{almacenBean.pathbodyAlmacen}" />    
 </h:form>
 <ui:include src="/pages/logistica/almacen/dialogos_almacen.xhtml" />
</ui:composition> 

toolbar_almacen.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:p="http://primefaces.org/ui">
    <p:toolbar>  
        <p:toolbarGroup align="left" styleClass="panelgrid-css">
            <p:commandButton id="idButtomNuevo" title="Nuevo"  icon="ui-icon-document" process="@this" value="Nuevo"
                    actionListener="#{almacenBean.limpiarAlmacen}" update="idFormAlmacen" immediate="true">
                 <p:resetInput target="idFormAlmacen"/>
            </p:commandButton>
            <p:commandButton id="idButtomGuardar" title="Guardar" icon="ui-icon-disk" action="#{almacenBean.guardarAlmacen}" 
                 disabled="#{almacenBean.au.btGuardarDisabled}" update=":idFormAlmacen" validateClient="true" value="Guardar"/>
            <p:commandButton id="idButtomEliminar" title="Eliminar" icon="ui-icon-trash" actionListener="#{almacenBean.eliminarAlmacen}"
                 disabled="#{almacenBean.au.btGuardarDisabled}" update="idFormAlmacen" process="@this" immediate="true"
                 value="Eliminar"/>
            <p:commandButton id="idButtomListar" title="Listar" icon="ui-icon-grip-solid-horizontal" process="@this" value="Listar"
                             actionListener="#{almacenBean.listarAlmacen}" update="idFormAlmacen" immediate="true"/>
            <p:commandButton id="idButtomBuscar" title="Buscar" icon="ui-icon-search" value="Buscar"
                             actionListener="#{almacenBean.buscarAlmacen}" immediate="true">
                <p:ajax event="dialogReturn" update="idFormAlmacen"/>
            </p:commandButton>
        </p:toolbarGroup>  
    </p:toolbar>
 </ui:composition>

路径bodyAlmacen = "/pages/logistica/almacen/crear_almacen.xhtml"

<ui:composition  
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:p="http://primefaces.org/ui" >

  <p:panel header="Datos Generales de Almacen" />  
  <h:panelGrid id="idPanelAlmacen1" columns="3" width="100%" border="0" columnClasses="colC1-T,colC2-T">
    <p:outputLabel  for="idEmpresa" value="Empresa:" style="margin-right:2px;"/>  
    <p:inputText id="idEmpresa"  style="width:350px;" value="#{almacenBean.alVista.strEmpresa}" readonly="true"/>  
    <h:outputText value="&#160;&#160;&#160;"/>

    <p:outputLabel for="idCodProveedor" value="Codigo:" style="margin-right:2px;"/>  
    <p:inputText id="idCodProveedor" value="#{almacenBean.alVista.codigo}" style="width:350px;" required="true"
                 requiredMessage="Ingrese Codigo ">
        <p:clientValidator />
    </p:inputText>  
    <p:message for="idCodProveedor" display="text"/>

    <p:outputLabel for="idNombre" value="Nombre:" style="margin-right:2px;"/>  
    <p:inputText id="idNombre" value="#{almacenBean.alVista.nombre}" style="width:350px;"
                 required="true" requiredMessage="Ingrese Nombre de Almacen">
        <p:clientValidator event="keyup" />
    </p:inputText>
    <p:message for="idNombre" display="text"/>
  </h:panelGrid>

  <h:panelGrid id="idPgDirAlmacen" columns="4" width="100%" border="0" columnClasses="colC1-T,colC2-T,colC3">
    <p:outputLabel for="idInDirAlmacen" value="Direccion de Almacen:" style="margin-right:2px;" />
    <p:inputText id="idInDirAlmacen" style="width:350px;" readonly="true" required="true"  value="#{almacenBean.alVista.direccionAlmacen}"
                 requiredMessage="Ingrese direccion de Almacen"/>   
    <p:commandButton title="Crear Direccion Almacen" actionListener="#{almacenBean.abrirDireccionAlmacen}"  process="@this"
                     icon="ui-icon-search" >
        <p:ajax event="dialogReturn" update=":idFormAlmacen:idPgDirAlmacen"/>
    </p:commandButton>
    <p:message for="idInDirAlmacen" display="text"/>
  </h:panelGrid>
</ui:composition> 

感谢一切。

2个回答

24

这不是 JSF 特定的行为,当按下 Enter 键时,将使用第一个具有 type="submit" 属性的 <input>。这在 SO 上已经讨论过(请参见这个这个),我将在此列出其中一些建议。

Javascript 解决方案:

在表单上使用 <h:form id="thisform" onkeypress="if( event.keyCode == 13){event.keyCode=0;}">。但是,这将防止您在表单上拥有任何默认命令,因为它会捕获按下 Enter 键的操作。

Primefaces 解决方案:

在表单上使用一个什么也不做的虚拟按钮,并将其设置为表单的默认命令。

<p:defaultCommand target="dummy"/>
<p:commandButton id="dummy" process="@none" global="false" style="display:none;"/>

然而,如果焦点在一个按钮上,并且我在这两种情况下都按下回车键,那么具有焦点的按钮的操作不会被调用。对于我的英语,请原谅。 - Ronald
尝试这个作为第一个例子:onkeypress="if( event.keyCode == 13){if(document.activeElement.type == 'text') event.keyCode=0;}" - Predrag Maric
2
谢谢,但我尝试使用:onkeypress="if( event.keyCode == 13){if(document.activeElement.type == 'text') return false;}",它可以工作,但是使用event.keyCode=0不起作用。 - Ronald

4

我在我的项目中也遇到了同样的问题。为了避免在按下回车键时提交,我只需像下面这样做。 在每个inputText字段中添加以下代码:

onkeypress="if (event.keyCode == 13) { return false; }"

1
为什么要在每个inputText上进行操作,而不是在表单上进行操作(就像其他答案中所述)?能否详细说明为什么要以更复杂的方式进行操作? - Kukeltje

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接