1

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();
}
Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
Pegus
  • 31
  • 2

1 Answers1

1

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:

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
  • Thank you for your answer, but when I trie dynamic="true" it is always load the items on page loading but not on first selectOneMenu opening, I tried it many times. Please trie my code and you will see that method value="#{companyBean.loadAllCompaniesList()}" always is loading when page loading – Pegus Dec 25 '22 at 18:27
  • Yes your method always will as exactly stated above in in this answer. Dynamic ONLY prevents the HTML from being set to the DOM it does NOT stop your items from being loaded. – Melloware Dec 26 '22 at 20:43