How to popular a p:datatable with a List<> that contains two List<> attributes inside?

Asked

Viewed 893 times

3

I spent all day today trying to do something that’s supposed to be simple but I couldn’t. The thing is, I’m wearing the primefaces and I’m new to it yet and I need some popular way of my <p:datatable> with a list of objects. So far ok. But inside the list object I have two more lists. Example:

List<GastosMes> gastosAno

public class GastosMes {
   public int mesReferente
   List<Conta> contas
   List<Comida> comidas
} 

public class Conta{
   public Double valorGas;
   public Double valorAgua;
   public Double totalContas;
}

public class Comida{
   public Double valorFeijao;
   public Double valorArroz;
   public Double totalComida;
}

Assuming this silly example, I have two lists inside the Gastosmes object. The Gastosmes list is then passed to my page (returned by foodAntroller.gastosAnoList), but then I can only display OR the Accounts OR Foods. My xhtml is like this :

<p:dataTable value="#{gastosAnoController.gastosAnoList}"
                var="gastosMes">

                <p:columnGroup type="header">
                    <p:row>
                        <p:column rowspan="2" headerText="Mês" style="width:100px" />
                        <p:column colspan="3" headerText="Gastos do Ano Corrente" />
                    </p:row>
                    <p:row>
                        <p:column headerText="Gastos Contas" />
                        <p:column headerText="Gastos Comida" />
                        <p:column headerText="Total Líquido" />
                    </p:row>
                </p:columnGroup>                                        
                <p:subTable var="conta" value="#{gastosMes.contas}">
                    <f:facet name="header">
                        <h:outputText value="#{gastosMes.mesReferente}" />
                    </f:facet>  
                    <p:column>                      
                    </p:column> 
                    <p:column >
                        <h:outputText value="#{contas.valorFeijao}" />
                    </p:column>
                    <p:column>
                        <h:outputText value="#{contas.valorArroz}" />
                    </p:column>     
                    <p:column>
                        <h:outputText value="#{conta.totalContas}" />
                    </p:column>                     
                </p:subTable>                   
</p:dataTable>

As you can see, I created a subtable (I searched a lot and only found it as a "solution") and I was able to fill the columns with the attributes of the list of Accounts. The problem is that I need to fill the table with the attributes of the Food list as well, how do I do ? any idea? I tried to put two subtables, two datatables, nothing was cool. If anyone can help me.

NOTE: As I will separate by month, I thought it would be more practical to create the Gastosmes class and within it have a list of bills and foods related to this month.

  • I think there are several components that you can use to compose the datatable, but it depends on what you want to do (you can use ui:repeat instead of subtable). For example, see this question where the user needed to group a checkbox list within another checkbox list. http://answall.com/a/169839/60946

1 answer

1

When you created your subTable you could have used the value of dataTable as a reference, example:

<p:subTable var="bean" value="#{gastosMes}">
                    <f:facet name="header">
                        <h:outputText value="#{gastosMes.mesReferente}" />
                    </f:facet>  
                    <p:column>
                    <h:outputText value="#{bean.valorGas}" />                     
                    </p:column> 
                    <p:column >
                        <h:outputText value="#{bean.valorFeijao}" />
                    </p:column>                    
                </p:subTable> 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.