java - 如何设置 f :selectItem in a specific option after a p:commandButton action?

标签 java jsf primefaces selectonemenu

我有一个 javascript 代码,可以清除表单的所有 p:inputText (在 p:commandButton 操作之后)。问题是 p:selectOneMenu 仍然在它所选择的选项中选择了 f:selectItem我需要将值放在每个 p:selectOneMenu 的第一个 f:selectItem 中。

我该怎么做?如何清除选定的值?

java脚本代码:

            <script type="text/javascript">
                function limpiarForm()
                {
                    document.getElementById("formularioAltas").reset();
                }
            </script>

formularioAltas 是表单 ID。

p:commandButton的代码:

<p:commandButton value="Guardar" action="#{altasBean.agregarRefaccion()}" oncomplete="limpiarForm()" />

并且该代码不会重置(我不想清除这些值,我只想选择第一个选项)p:selectOneMenu

的值

这里是:

<h:outputText value="Estado de la refacción" />
    <p:selectOneMenu value="#{altasBean.refaccion.estado}">
        <f:selectItem itemLabel="..." itemValue="0" />
        <f:selectItem itemLabel="Ok" itemValue="Ok" />
        <f:selectItem itemLabel="Reparar" itemValue="Reparar" />
        <f:selectItem itemLabel="Sospechoso" itemValue="Sospechoso" />
    </p:selectOneMenu>

bean :

private RefaccionBean refaccion = null;
/**
 * Get the value of refaccion
 *
 * @return the value of refaccion
 */
public RefaccionBean getRefaccion() {
    return refaccion;
}

/**
 * Set the value of refaccion
 *
 * @param refaccion new value of refaccion
 */
public void setRefaccion(RefaccionBean refaccion) {
    this.refaccion = refaccion;
}

public void agregarRefaccion() {
   I did a lot of things here...
   And after those things i clear the p:inputText with the javascript code
   -> After that i want to set the values of the p:selectOneMenu in the fist f:selectItem
}

最佳答案

约瑟夫的答案确实引导您走向正确的方向,但由于您共享了一些代码,我将在我的答案中使用它。

首先,确保您的按钮调用您的 bean 方法并在完成后更新 selectOneMenu 组件。虽然这里发生了一些 ajax,但 primefaces 为您抽象了这些。

<p:commandButton value="Guardar" action="#{altasBean.agregarRefaccion}" update="selectOneMenu" />

update 属性很重要,因为它将查找 id 与此处指定的任何内容相匹配的组件。所以你需要给你的 selectOneMenu 一个 id。如果需要更新多个组件,可以将它们的 id 添加到 update 属性中,并用空格或逗号分隔。

<h:outputText value="Estado de la refacción" />
<p:selectOneMenu value="#{altasBean.refaccion.estado}" id="selectOneMenu">
    <f:selectItem itemLabel="..." itemValue="0" />
    <f:selectItem itemLabel="Ok" itemValue="Ok" />
    <f:selectItem itemLabel="Reparar" itemValue="Reparar" />
    <f:selectItem itemLabel="Sospechoso" itemValue="Sospechoso" />
</p:selectOneMenu>

现在只需清理操作方法中的值即可:

public void agregarRefaccion() {
   //If you have other values to clean, clean them here
   this.refaccion.estado="0"; //Matches the value for your first item
}

关于java - 如何设置 f :selectItem in a specific option after a p:commandButton action?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23920239/

相关文章:

java - Web 服务中日期类型的问题

JSF inputFile 拒绝在框架中显示 'mypage.xhtml',因为它将 'X-Frame-Options' 设置为 'deny'

jsf - 根据 PrimeFaces LazyDataModel<T> 返回的行数渲染 JSF/PrimeFaces 组件

javascript - Primefaces SelectManyMenu 带过滤器 : How to select all displayed Items with JS

java - 如何在 android 中更改 SeekBar 颜色? (以编程方式)

java - 命题逻辑程序中括号的解析

JSF 2 AJAX - 重新加载整个 div (例如 <ui :include src ="toReloadData.xhtml"/>)

java - JSF 数据表过滤器示例

java - 这段代码有什么问题吗? (java)(错误: cannot find symbol method getValue() )

jsf - 如何获取正在调用托管 bean 的 getter 方法的 UIComponent?