简体   繁体   中英

How to update backing bean when deselecting the last slected item from a p:selectCheckboxMenu with a primefaces ajax event “change”?

I have a primefaces "selectCheckboxMenu" in my xhtml file, and I need to update it when the ajax event "change" gets triggered, it works fine when selecting and deselecting items from my checked list, the only problem is the event seems to get lost whenever I deselect the last and only selected item on aforesaid list. There seems to be some code being executed in the front end (HTML, JavaScript) but it doesn't reach my Managed Bean in my debugging console. Can someone help get this event to work so I can see the change in my Managed Bean?

<h:panelGroup>
  <h:outputLabel for="myDocs" value="#{msg['documentation']}" />
  <p:selectCheckboxMenu id="myDocs" value="#{documentMB.selectedDocs}" label="#{msg['select']}" showHeader="false" required="true" requiredMessage="#{msg['message']}" updateLabel="true" >
    <f:selectItems value="#{documentMB.availableDocs}" var="doc" itemLabel="#{doc.name}" itemValue="#{doc.id}" />
    <p:ajax event="change" update="contentMessage" listener="#{documentMB.changeContent()}" />
  </p:selectCheckboxMenu>
  <p:message for="myDocs" ></p:message>
</h:panelGroup>

My Managed Bean:

@ManagedBean
@ViewScoped
public class DocumentMB {
  List<Documents> selectedDocs;
  List<Docuemnts> availableDocs;
  @PostConstruct
  public void init() {
    //all initializations
  }
  //getters and setters
}

If you put a <p:growl autoUpdate="true"/> in there you'll see that requiredMessage is shown when you remove the last check. So the logic is that a failed validation will stop the listener from running (same happens with commandButtons and -Links).

I'm not sure if there is a better way, but I would just remove required="true" and do this validation manually when the whole form is submitted. If it's not part of a larger form, I would just show the current message to the user (update the p:message ).

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