0
votes

I use Primefaces 5 and I would like reset a selectonemenu to default value.

I have a paging structure so:

    <p:fragment>

    <p:tabView id="tabview1" cache="false" dynamic="true" >
        <p:tab title="One" id="tab1">
            <ui:include src="tab1.xhtml" />
        </p:tab>
        <p:tab title="Two" id="tab2" >
            <ui:include src="tab2.xhtml"/>
        </p:tab>
    </p:tabView>
</p:fragment> 

Then, tab1.xhtml for example is this:

<h:form id="frm1">
    <table id="tbl1">          
        <tr>
            <td>
                <p:selectOneMenu id="select1"
                                 value="#{bean.test}" 
                                 valueChangeListener="#{bean.setXXX}"
                                 var="var1" >
                    <f:selectItem itemLabel="Select..." 
                                  itemValue="#{null}" />
                    <f:selectItems  var="var2"
                                    value="#{bean.list}" 
                                    itemLabel="#{var2.label}"
                                    itemValue="#{var2}"/>
                    <p:ajax process="@this" event="change" update="@this"/>
                </p:selectOneMenu>
            </td>
        </tr>
    </table>
</h:form>

I believe that for reset my selectonemenu I can write in my setXXX method: RequestContext.getCurrentInstance().reset("tabView1:tab1:frm1:tbl1:select1");

Doesn't work and I have excpetion "Caused by: java.lang.IllegalArgumentException"

Any idea? What is correct path for reset?

Thanks

2

2 Answers

0
votes

As far as i understand what you mean by reset is to establish again the default value to the select menu, don't you?

In that case the thing is very simple. You just need to modify the current value of the selected item. So, in your Backing Bean "bean", in the setXXX method just reset the original value of the attribute="#{bean.test}".

When ajax updates the combo it will refresh the recent default value test.

0
votes

You are referring the component using tabView1:tab1:frm1:tbl1:select1.

There are 2 errors in it.

In JSF/Primefaces the components which implement javax.faces.component.NamingContainer are the only components that attach their id to the inner components.

So p:tab and table (not a JSF component at all) are not one of those components.

From your code only component that implement javax.faces.component.NamingContainer are p:tabView and h:form.

So your p:selectOneMenu id will be just be tabview1:frm1:select1

RequestContext.getCurrentInstance().reset("tabview1:frm1:select1");

This you can look it up from your Browser's Inspect Element.

enter image description here

and Remember to update the component once you reset, which you can do it from ManagedBean itself using Requestcontext

RequestContext.getCurrentInstance().update("tabview1:frm1:select1");