Question

I have three tables in MySQL database.

  • category (excluded from this question)
  • sub_category
  • product

The relationship between these tables is intuitive - one-to-many in the order in which they appear.


I'm iterating through a list of SubCategory, List<SubCategory> using <p:dataGrid> as follows.

<p:dataGrid var="row" value="#{subCategoryManagedBean}" rows="2" first="0" columns="1" rowIndexVar="rowIndex" paginator="true" paginatorAlwaysVisible="false" pageLinks="10" lazy="true" rowsPerPageTemplate="5,10,15">
    <h:panelGrid columns="1" style="width:100%;">

        <!--Here set #{row.subCatId} to the associated managed bean, does not seem possible-->

        <!--<p:carousel var="prodRow" value="#{row.productSet.toArray()}" numVisible="4" pageLinks="5" headerText="#{row.subCatName}" style="text-align: left;">
            <p:graphicImage library="fileUpload" name="product_image/medium/#{prodRow.productImageSet.toArray()[0].prodImage}"/>
        </p:carousel>-->

    </h:panelGrid>
    <p:ajax event="page" onstart="PF('blockDataPanelUIWidget').block()" oncomplete="PF('blockDataPanelUIWidget').unblock()"/>
</p:dataGrid>

While iterating over List<SubCategory>, I want to set the id of each sub category, subCatId (of type Long) to the managed bean in question so that a list of products associated with each sub category can be fetched before showing products in <p:carousel> Is it possible?


Additional information:

Each object of SubCategory in List<SubCategory> has a set of products, Set<Product> already fetched and they are displayed in <p:carousel> too as show in the commented code in XHTML but I need to fetch only top five products of each sub category after sorting a list of products of each sub category in descending order.

This seems possible only if a list of products is queried for every sub category while iterating over List<SubCategory>.

Please suggest, if there are some other ways round. Anyway, I need to display a sub category-wise list of products in a tabular format - sub category1 -> show its top 5 products in the first row of <p:dataGrid>, sub category2 -> show its top 5 products in the second row of <p:dataGrid> and so on.

Était-ce utile?

La solution

Sure, you can do it. This is how your <p:carousel> should be:

<p:carousel var="prodRow" value="#{mrBean.getProducts(row.subCatId)}" numVisible="4"
            pageLinks="5" headerText="#{row.subCatName}" style="text-align: left;">
    <p:graphicImage library="fileUpload" name="product_image/medium/#{prodRow.productImageSet.toArray()[0].prodImage}"/>
</p:carousel>

This is the @ManagedBean:

@ManagedBean
@RequestScoped
public class MrBean {
    ...
    public List<Product> getProducts(Long subCatId) {
        // Fetch your top 5 products here
    }
    ...
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top