简体   繁体   中英

How to set a f:selectItem in a specific option after a p:commandButton action?

I have a javascript code that clears all the p:inputText (after a p:commandButton action) of the form. The problem is that the p:selectOneMenu still has the f:selectItem selected in the option it was selected. I need to put the values in the first f:selectItem of each p:selectOneMenu .

How to I do that? How can I clear the selected values?

The java script code:

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

formularioAltas is the form id.

The code of the p:commandButton :

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

And that code does not reset(I dont want to clear the values, I just want to put the first option selected) the values of the p:selectOneMenu

Here it is:

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

The 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
}

Now I'm not entirely sure if I follow you, but let me know if this helps you out at all. It's entirely JSF and I threw in some ajax. You can play around with your javascript still.

Front-end:

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

Bean:

@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)
}

Josef's answer does lead you in the right direction, but since you shared some code, I will just use it in my answer.

First, make sure that your button calls your bean method and updates the selectOneMenu component after it's done. Although there's some ajax going on here, primefaces abstracts that for you.

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

The update attribute is important as it will look for the component whose id matches whatever is specified there. So you need to give your selectOneMenu an id. If you need to update more than one component, you can add their ids to the update attribute separated by either space or comma.

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

Now it's just a matter of cleaning up your values inside your action method:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM