PrimeFaces selectOneMenu dynamic="true" does not work

307 views Asked by At

I trie to not load the p:selectOneMenu item list when the page is loaded, because the list is too big. I set the attribute dynamic="true" to have lazy loading, but it always loads the list on page load. Instead I want to load it only when user starts opens the p:selectOneMenu.

My xhtml

<p:selectOneMenu id="companyEntity"
                 value="#{docBean.docIncomingEntity.companyEntity}"
                 effect="fade"
                 style="width: 100%"
                 dynamic="true"
                 filter="true"
                 converter="omnifaces.SelectItemsConverter">
    <f:selectItems value="#{companyBean.loadAllCompaniesList()}"
                   itemLabel="#{item.name}"
                   itemValue="#{item}"
                   var="item"/>
</p:selectOneMenu>

My Bean

...
public List<CompanyEntity> loadAllCompaniesList() {
    return companyDAO.selectAllCompanies();
}

My DAO

public List<CompanyEntity> selectAllCompanies() {
    return em.createQuery("select a from CompanyEntity a order by a.name", CompanyEntity.class)
            .getResultList();
}
1

There are 1 answers

2
Jasper de Vries On

First of all, as you did not state your version, the dynamic attribute was added in PrimeFaces 6.2.

Note that the dynamic="true" attribute of the p:selectOneMenu will call the getter method of the items from the bean when the page is loaded. What it does not do is render the options HTML in the initial response. The options will be added to the HTML DOM tree using an Ajax request when the menu is first opened.

If you want your data to be loaded lazily you should use the p:autoComplete component. AutoComplete displays suggestions while the input is being type. It features various options, customizable content, multiple selection, effects and events.

See:

Off topic: you are currently using loadAllCompaniesList() (which basically is your getter method). This method can be called multiple times per open of you menu. Meaning your query will be executed multiple time per open. This is a performance issue.

See also: