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

Question:

I spent the whole day today trying to do something that is supposed to be simple but I couldn't. Here's the thing, I'm using primefaces and I'm still new to it and I need to somehow populate my <p:datatable> with a list of objects. Until then 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 SpendingMes object. The SpendMes list is then passed to my page (returned by expensesAnoController.gastosAnoList), but from there I can only display OR the Bills OR the Food. My xhtml looks 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 managed to fill the columns with the attributes from the Accounts list. The problem is that I need to fill the table with the attributes from the Food list too, how do I do it? Any idea? I tried to put two subtables, two datatables, nothing was nice. If anyone can help me.

NOTE: As I'm going to separate it by month, I thought it would be more practical to create the GastosMes class and within it have a list of accounts and meals for this month.

Answer:

When you created your subTable you could have used the dataTable value 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> 
Scroll to Top