I suggest to store the products of the currently loaded subCategory
in a Map
, just after they are loaded by the lazy model of <p:dataGrid>
. The Map will never grow too much because you'll empty it at each invocation of the load
method:
@ManagedBean
@ViewScoped
public final class FeaturedProductManagedBean extends LazyDataModel<SubCategory> implements Serializable
{
@EJB
private final FeaturedProductBeanLocal service=null;
private Product selectedProduct; //Getter and setter.
private Long count; //Getter and setter.
private Map<Long, Product> productsBySubCategory = new HashMap<>(); //Getter only.
private static final long serialVersionUID = 1L;
@Override
public List<SubCategory> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters)
{
int rowCount = service.rowCount().intValue();
setRowCount(rowCount);
List<SubCategory> subCategories = service.getList(first, pageSize);
productsBySubCategory.clear();
for (SubCategory sc : subCategories)
{
productsBySubCategory.put(sc.getSubCatId(), service.getProductList(sc.getSubCatId());
}
return subCategories;
}
}
And access directly the Map
from EL:
<p:carousel value="#{featuredProductManagedBean.productsBySubCategory[row.subCatId]}" />
Note that you don't need all that boilerplate that checks page boundaries and sets pagesize.