Search code examples
primefacesjsf-2selectcheckboxmenu

Primefaces selectCheckboxMenu erratic behavior when deselecting individual checkboxes


In an application based on jsf 2.1 and Primefaces 6.1.5, I have difficulties implementing a <p:selectCheckboxMenu

I simplified the code according to the instructions here How to create a Minimal, Complete, and Verifiable example My code now looks quite similar to the code in the Primefaces Showcase. Primefaces Showcase

After much analyzing, I can describe 'erratic' a bit better. When deselecting an item, the item following it is affected. The last item is not affected at all. And the first item can never be deselected. It seems like a bug in the use of indices.

Can anyone confirm this and perhaps suggest a Workaround?

Here is the xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:p="http://primefaces.org/ui">

    <h:form id="form">
        <p:panel>
            <p:selectCheckboxMenu id="testSCM"
                                  value="#{myForm.testList}"
                                  multiple="true"
                                  label="Choose item..."
                                  updateLabel="true">
                <f:selectItems var="s"  value="#{myForm.testItems}" itemLabel="#{s}" />
            </p:selectCheckboxMenu>
        </p:panel>

        <p:commandButton value="Submit" update="displayItems" oncomplete="PF('itemDialog').show()" style="margin-top:10px;" />

        <p:dialog header="Selected Items" modal="true" showEffect="fade" hideEffect="fade" widgetVar="itemDialog" width="250">
            <p:outputPanel id="displayItems">
                <p:dataList value="#{myForm.statusList}" var="item" emptyMessage="No items selected">
                    <f:facet name="header">
                        Status
                    </f:facet>
                    #{item}
                </p:dataList>
            </p:outputPanel>
        </p:dialog>
    </h:form>
</ui:composition>

And this is the form:

@Named("myForm")
@SessionScoped
public class MyForm implements Serializable {

    private String[] testList;
    private List<String> testItems;

    public String[] getTestList() {
        return testList;
    }

    public void setTestList(String[] testList) {
        this.testList = testList;
    }

    public List<String> getTestItems() {
        return testItems;
    }

    public void setTestItems(List<String> testItems) {
        this.testItems = testItems;
    }

    public void reset() {
        testItems = new ArrayList<>();
        testItems.add("Item1");
        testItems.add("Item2");
        testItems.add("Item3");
        testItems.add("Item4");
        testItems.add("Item5");
        testItems.add("Item6");
    }
}

Solution

  • The problem was caused by a bug in Primefaces version 6.1.5. The code works fine when downgrading to Version 6.0 or upgrading to Version 6.1.8, which is what I chose to do.

    The problem is described in Primefaces issue tracker on github