【问题标题】:How to set a f:selectItem in a specific option after a p:commandButton action?如何在 p:commandButton 操作后在特定选项中设置 f:selectItem?
【发布时间】:2014-05-28 19:26:17
【问题描述】:

我有一个 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>

豆子:

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
}

【问题讨论】:

  • 您想用 javascript 清除值吗?到目前为止,您介意发布您的代码吗? :)
  • 我用 javascript 清除了 p:inputText。但我想知道如何在第一个 f:selectItem 中设置 p:selectOneMenu。我只想知道如何去做,不管是 javascript 还是其他东西。纯 JSF 应该更好。
  • 好吧,让我看看我能做什么
  • 我只需清除操作方法中我想要的任何内容,并使用命令按钮中的更新属性更新受影响的组件。如果您发布您的 xhtml 和 bean 代码,我可以向您展示如何做到这一点。
  • 当然让我放一部分代码:)

标签: java jsf primefaces selectonemenu


【解决方案1】:

Josef 的回答确实将您引向了正确的方向,但是由于您共享了一些代码,因此我将在回答中使用它。

首先,确保您的按钮调用您的 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
}

【讨论】:

  • 感谢工作。因此,如果我想在没有 javascript 的情况下清除所有表单,我必须对所有其他组件执行相同操作吗?更新将如下所示: update="component1 component2 ... componentN" 我的意思是我的表单中有很多组件,这是最好的方法吗?
  • 当我需要更新很多组件时,我只是在它们周围包裹一个面板(并给它一个id)并更新面板;所有的孩子都以这种方式得到更新。不过,在 bean 方面,您必须手动清除每个值。但是你可以列出你提到的组件ID。
【解决方案2】:

现在我不完全确定我是否关注你,但如果这对你有帮助,请告诉我。它完全是 JSF,我加入了一些 ajax。您仍然可以使用您的 javascript。

前端:

<p:inputText id="input" value="#{bean.value}" />

<p:commandButton value="button" action="#{bean.action}">
   <f:ajax execute="input" render="output" />
</p:commandButton>

<p:selectOneMenu id="output" value="#{bean.placeValue}">
   <f:selectItems value="#{bean.values}" />
</p:selectOneMenu>

豆子:

@ManagedBean
@RequestScoped
public class Bean {

    private String value;
    private List<String> values; 

    @EJB
    private ValueService valueService;

    @PostConstruct
    public void init() {
        values = valueService.list();
    }

    public void action() {
        // ... Action taken
    }

    public String placeValue() {
        // ... Validation and clear desired values
        return value;
    }


    // ... (getters, setters, etc)
}

【讨论】:

  • 如果我想清除所有表格怎么办?用ajax怎么做?
  • 您可以在placeValue方法中将变量的值设置为null""
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-01
  • 2013-01-17
  • 2016-01-19
相关资源
最近更新 更多