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