1

I have a problem with h:datatable and ui:repeat.

I have the next objects structure:

  • Product
    • SubproductType1
    • SubproductType2
    • SubproductType3

In the datatable I need to show info of the Product and info of one of the subproducts like:

Name         Price       StartDate       EndDate
P1Name        25$        01/01/13        01/07/13
P2Name        25$        01/01/13        01/07/13

The price and Dates comes from the SubproductType2 price attribute.

So if I have this

<h:dataTable value="#{bundleBean.products}" var="myBundle" >

    <ui:param name="currentSubProd" value="#sbaBean.getSubProdOfBundleFilterByCategory(myBundle.id, categoryType.internet)}" />
                    <h:column>
                    <f:facet name="header" >Name</f:facet>
                            #{myBundle.name}
                </h:column>
                    <h:column>
                    <f:facet name="header" >Price</f:facet>
                            #{currentSubProd.price}
                </h:column>
                    <h:column>
                    <f:facet name="header" >StartDate</f:facet>
                            #{currentSubProd.startDate}
                </h:column>
                    <h:column>
                    <f:facet name="header" >EndDate</f:facet>
                            #{currentSubProd.endDate}
                </h:column>
</h:dataTable>                      

It works perfectly but it invokes to the bean method every time #currentSubProd is called in the xhtml file, 3 times in this case. So if I have some query inside the method the DB is doing the query 3 times.

I realiced that with ui:repeat inside a column it works fine but you can't put and ui:repeat outside it using an ui:repeat for every column on the datatable.

Like this (NOT Working):

    <ui:repeat name="currentSubProd" value="#sbaBean.getSubProdOfBundleFilterByCategory(myBundle.id, categoryType.internet)}" >
                    <h:column>
                    <f:facet name="header" >Name</f:facet>
                            #{myBundle.name}
                </h:column>
                    <h:column>
                    <f:facet name="header" >Price</f:facet>
                            #{currentSubProd.price}
                </h:column>
                    <h:column>
                    <f:facet name="header" >StartDate</f:facet>
                            #{currentSubProd.startDate}
                </h:column>
                    <h:column>
                    <f:facet name="header" >EndDate</f:facet>
                            #{currentSubProd.endDate}
                </h:column>
    </ui:repeat>
</h:dataTable>

Any idea how to solve that?

Thanks in advance.

Regards.

dcalap
  • 1,048
  • 2
  • 13
  • 37
  • It calls it X times, because `ui:param` is just an alias. Everytime you do `#{currentSubProd.Y}` it evaluates `#{sbaBean.getSubProdOfBundleFilterByCategory(myBundle.id, categoryType.internet).Y}`. Can't you fill the objects before showing the table (in its constructor, in a beforePhase or in a `` and just iterate on its values? – Simon Arsenault Jul 04 '13 at 12:01

1 Answers1

2

Just stop doing business logic in getter methods. Getter methods should only return already-prepared data.

Do the business logic in (post)constructor or action(listener) methods instead.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555