Problem loading collection using datatable

UI Components for JSF
Post Reply
jameson.villanueva
Posts: 16
Joined: 15 Dec 2011, 06:57
Contact:

17 Apr 2012, 08:09

Hi all, I'm trying to implement a list of grouping items organized by groups and subgroups using the datatable component. Below the beans:
Grupos.java

Code: Select all

public class Grupos implements Serializable{
    
    private static final long serialVersionUID = 1L;
    private Integer id;
    private String descripcion;
    private Integer plantillaId;
    private String estado;
    private List<SubGrupos> subGrupos;
   //constructor, geters and setters

}
SubGrupos.java

Code: Select all

public class SubGrupos implements Serializable{
    
    private static final long serialVersionUID = 1L;
    private Integer id;
    private String descripcion;
    private Integer grupoId;
    private String estado;
   private List<Items> items;
//constructor, geters and setters

}
Items.java

Code: Select all

public class Items implements Serializable{
    
    private static final long serialVersionUID = 1L;
    private long numDocPe;
    private int lineaPed;
    private String codItem;
    private Float desRecLin;
    private Float porDesGlo;
    private Float porRecGlo;
    private BigDecimal precio;
    private BigDecimal cantPed;
    private BigDecimal cantDes;
    private String codana2;
    private BigDecimal factor;
    private String uniMed;
    private BigDecimal cantidadMedAlter;
    private BigDecimal precioMedAlter;
    private String motivoNoDesp;
    private String observacion;
    private String descripItem;
    private BigDecimal vmDescGlo;
    private BigDecimal vmRecGlo;
    private BigDecimal vmDescRecLin;


    private String cbmCodigo;
    private BigDecimal cantidadSolicitada;
    private String codmotivo;
    private String tipoCotiz;
    private Long numCotiz;
    private Integer lineaCotiz;
    private BigDecimal precioFinal;
    private BigDecimal totalLineal;
    private BigDecimal saldo;

    //constructor, getters and setters
}
Java class that populates the groups collection

Code: Select all

public class GruposDao implements Serializable{

public List<Grupos> searchGrupos(Integer plantilla, String codLis, String codBod) throws SQLException{
        List<Grupos> ret = new ArrayList<Grupos>();
        String sql;
        
        sql = "SELECT * FROM WWW_PLANTILLAS_PEDIDOS_GRUPOS WHERE plantilla_id = ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1, plantilla);
        Grupos grupo;
        SubGrupos subGrupo;
        Items item;
        List<SubGrupos> subGrupos;
        List<Items> items;
        ResultSet rs1 = ps.executeQuery();
        while(rs1.next()){
            grupo = new Grupos();
            grupo.setId(rs1.getInt("id"));
            grupo.setDescripcion(rs1.getString("descripcion"));
            grupo.setPlantillaId(rs1.getInt("plantilla_id"));
            grupo.setEstado(rs1.getString("estado"));
            
            sql = "SELECT * FROM WWW_PLANTILLAS_PEDIDOS_SUBGRUPOS WHERE grupo_id = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, grupo.getId());
            ResultSet rs2 = ps.executeQuery();
            subGrupos = new ArrayList<SubGrupos>();
            while(rs2.next()){
                subGrupo = new SubGrupos();
                subGrupo.setId(rs2.getInt("id"));
                subGrupo.setDescripcion(rs2.getString("descripcion"));
                subGrupo.setGrupoId(rs2.getInt("grupo_id"));
                subGrupo.setEstado(rs2.getString("estado"));
                
                sql =  "SELECT items.cod_item, items.descrip_res, item_precio.precio, stock.saldo, items.uni_med ";
                sql += "FROM items inner join WWW_PLANTILLAS_PEDIDOS_ITEMS on items.cod_item = WWW_PLANTILLAS_PEDIDOS_ITEMS.cod_item ";
                sql += "INNER JOIN item_precio ON item_precio.cod_item = items.cod_item INNER JOIN stock ON items.cod_item = stock.cod_item ";
                sql += "INNER JOIN WWW_PLANTILLAS_PEDIDOS_SUBGRUPOS ON WWW_PLANTILLAS_PEDIDOS_SUBGRUPOS.id = WWW_PLANTILLAS_PEDIDOS_ITEMS.subgrupo_id ";
                sql += "INNER JOIN WWW_PLANTILLAS_PEDIDOS_GRUPOS ON WWW_PLANTILLAS_PEDIDOS_GRUPOS.id = WWW_PLANTILLAS_PEDIDOS_SUBGRUPOS.grupo_id ";
                sql += "INNER JOIN WWW_PLANTILLAS_PEDIDOS ON WWW_PLANTILLAS_PEDIDOS.id = WWW_PLANTILLAS_PEDIDOS_GRUPOS.plantilla_id ";
                sql += "WHERE WWW_PLANTILLAS_PEDIDOS_ITEMS.subgrupo_id = ? AND item_precio.codlis = ? AND stock.cod_bod = ?";
                ps = conn.prepareStatement(sql);
                ps.setInt(1, subGrupo.getId());
                ps.setString(2, codLis);
                ps.setString(3, codBod);
                ResultSet rs3 = ps.executeQuery();
                items = new ArrayList<Items>();
                while(rs3.next()){
                    item = new Items();
                    item.setCodItem(rs3.getString("cod_item"));
                    item.setDescripItem(rs3.getString("descrip_res"));
                    item.setPrecio(rs3.getBigDecimal("precio"));
                    item.setSaldo(rs3.getBigDecimal("saldo"));
                    item.setUniMed(rs3.getString("uni_med"));
                    items.add(item);
                }//fin de while de items
                subGrupo.setItems(items);
                rs3.close();
                subGrupos.add(subGrupo);
            }//fin de while de subgrupos
            grupo.setSubGrupos(subGrupos);
            rs2.close();
            ret.add(grupo);
        }//fin de while de grupos
        rs1.close();
        ps.close();
        return ret;
    }

}
ManageBean

Code: Select all

@ManagedBean
@ViewScoped
public class PlantillasController implements Serializable{

    private static final long serialVersionUID = 1L;
    private List<Grupos> grupos;
    
    GruposBo service = new GruposBo();
    
    public PlantillasController() {
        grupos = service.searchGrupos(1, "02", "GYE");
    }

    public List<Grupos> getGrupos() {
        return grupos;
    }

    public void setGrupos(List<Grupos> grupos) {
        this.grupos = grupos;
    }
}
view xhtml

Code: Select all

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Página de Prueba</title>
    </h:head>
    <h:body>
        <h:form id="form">
            <p:growl id="growl"/>
            <p:dataTable value="#{plantillasController.grupos}" var="grupo">
                <f:facet name="header">#{grupos.descripcion}</f:facet>
                <p:subTable value="#{grupo}" var="subgrupo">
                    <f:facet name="header">#{subgrupo.descripcion}</f:facet>
                    <p:subTable value="#{subgrupo}" var="items">
                        <p:column>#{items.codItem}</p:column>
                        <p:column>#{items.descripItem}</p:column>
                    </p:subTable>
                </p:subTable>
            </p:dataTable>
        </h:form>
    </h:body>
</html>
Last edited by jameson.villanueva on 17 Apr 2012, 08:18, edited 3 times in total.

User avatar
T.dot
Expert Member
Posts: 620
Joined: 01 Feb 2012, 15:39
Location: Vienna/Austria

17 Apr 2012, 08:11

Nice Beans but where's your problem? ;)

jameson.villanueva
Posts: 16
Joined: 15 Dec 2011, 06:57
Contact:

17 Apr 2012, 08:16

Yes, Yes, the result in the view does not show values

User avatar
T.dot
Expert Member
Posts: 620
Joined: 01 Feb 2012, 15:39
Location: Vienna/Austria

17 Apr 2012, 08:24

Please add version information, etc. as described here: viewtopic.php?f=3&t=1194

Regarding your problem:
I don't know if you can nest subTables. (Didn't try)

But first of all I think there's something wrong with the properties you're using, if I'm comparing your xhtml with the SubTable example from the showcase.

Code: Select all

<p:dataTable value="#{plantillasController.grupos}" var="grupo">
                <f:facet name="header">#{grupos.descripcion}</f:facet>
                <p:subTable value="#{grupo}" var="subgrupo">
                    <f:facet name="header">#{subgrupo.descripcion}</f:facet>
                    <p:subTable value="#{subgrupo}" var="items">
                        <p:column>#{items.codItem}</p:column>
                        <p:column>#{items.descripItem}</p:column>
                    </p:subTable>
                </p:subTable>
</p:dataTable>
The value of the first subTable should be #{grupo.subGrupos}. The value of the second subTable (as I said - don't know if that even works) should be #{subgrupo.items}.

Code: Select all

<p:dataTable value="#{plantillasController.grupos}" var="grupo">
                <f:facet name="header">#{grupos.descripcion}</f:facet>
                <p:subTable value="#{grupo.subGrupos}" var="subgrupo">
                    <f:facet name="header">#{subgrupo.descripcion}</f:facet>
                    <p:subTable value="#{subgrupo.items}" var="items">
                        <p:column>#{items.codItem}</p:column>
                        <p:column>#{items.descripItem}</p:column>
                    </p:subTable>
                </p:subTable>
</p:dataTable>
Showcase example is here:
http://www.primefaces.org/showcase-labs ... bTable.jsf

jameson.villanueva
Posts: 16
Joined: 15 Dec 2011, 06:57
Contact:

17 Apr 2012, 08:38

I'm using version 3.2 of primefaces.
I tried the code in java console and debug making collections of individual objects, items, subgroups and groups are filled correctly. I am following the example of the showcase

User avatar
T.dot
Expert Member
Posts: 620
Joined: 01 Feb 2012, 15:39
Location: Vienna/Austria

17 Apr 2012, 09:00

Did you try changing

Code: Select all

<p:subTable value="#{grupo}" var="subgrupo">
to

Code: Select all

<p:subTable value="#{grupo.subGrupos}" var="subgrupo">
?

Then i would try with only one subTable:

Code: Select all

<p:dataTable value="#{plantillasController.grupos}" var="grupo">
                <f:facet name="header">#{grupos.descripcion}</f:facet>
                <p:subTable value="#{grupo.subGrupos}" var="subgrupo">
                    <p:column>#{subgrupo.descripcion}</p:column>
                </p:subTable>
</p:dataTable>
If that works you can dig deeper...

jameson.villanueva
Posts: 16
Joined: 15 Dec 2011, 06:57
Contact:

17 Apr 2012, 09:26

Code: Select all

<p:dataTable value="#{plantillasController.grupos}" var="grupo">
                <p:subTable value="#{grupo.subGrupos}" var="subgrupo">
                    <f:facet name="header">#{subgrupo.descripcion}</f:facet>
                    <p:column>#{subgrupo.descripcion}</p:column>
                </p:subTable>
            </p:dataTable>
With this code only shows the description with component p: column, using f: facet shows nothing

User avatar
T.dot
Expert Member
Posts: 620
Joined: 01 Feb 2012, 15:39
Location: Vienna/Austria

17 Apr 2012, 12:18

I took a look at the Renderer:
- Nesting subTables isn't supported.
- You can only use properties from the outer-table in the header-facet of a subtable. So you can use grupo.property but not subgrupo.property.

jameson.villanueva
Posts: 16
Joined: 15 Dec 2011, 06:57
Contact:

17 Apr 2012, 17:05

From what I could tell the depth of the compontente p: subtable is 1, but beyond that you can not work. I currently use Groups, Subgroups and Items, someone please correct me if I'm wrong


Greetings to all

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 50 guests