Doubt with datatable and/or JSF/JPA modeling

Asked

Viewed 352 times

5

Good evening, I have the following problem... at first it seems kind of silly, but honestly I can’t get out of it

I want to create a table where the columns are Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday respectively... and in these columns are shown the data, these data have as attribute the day of the week in question, what would be the best way then to make them appear in the table in the correct column...

<p:dataTable id="tbl"
    value="#{bean.registros}"
    var="registro" emptyMessage="Nenhum registro">

<p:column headerText="DOMINGO">

         </p:column>

        <p:column headerText="TERÇA-FEIRA">

         </p:column>

     <p:column headerText="QUARTA-FEIRA">

         </p:column>


e assim vai...

see, each record contained in the "records" list has an attribute called a day, which is an Enum with the day of the respective week, persist, edit etc., everything is working normally, the problem is that I do not know how to make the records containing the day = MONDAY appear only in the correct column...

in other words I have a list of objects to be shown in a data table where in each column should be listed objects that have an attribute x (an Enum that represents the day of the week)

I tried to make a logic with rendered, but besides thinking it was too "gambiarra", it was not good, causing several other problems...

Someone with some good idea ?

  • It seems to me that you are using the wrong approach. Creating seven columns, each for a day of the week sounds very strange to me. I would create a table DIA_DA_SEMANA with the seven days and would make an N-N relationship of your table of records with the table of days of the week.

  • Good evening @Victor, thanks for the reply but I could not understand, I would like to explain a little this reasoning ?

  • Ah, sorry. I’m the one who got it wrong. When you refer to "table" I was thinking of table in database, not table in JSF or HTML.

2 answers

1

The problem with your approach is that you don’t have a proper data structure for the JSF component.

I explain. In your system there can be 2 items on Monday, 1 on Tuesday and 3 on Wednesday, right? This works in a common table, but one component DataTable represents what we usually call a "grid", that is, a list of records with a defined number of lines, each representing a system record. You can’t have three items in one column and two in the other.

You can do some kind of gambit using DataTable, but other options that should be considered according to your problem are:

  • Datalist: put 7 lists next to each other as you can display 7 independent lists. You will need to split the original list by day.
  • Schedule: if your idea is some kind of calendar, use this component to make it easy to view events. Use the view Weekly, that is, weekly.

If you still prefer to use the DataTable, will need to transform your data structure, more or less like this:

  1. Create a POJO with an attribute for each column of the record type. Example:

    class DataTableBean {
        Registro domingo, segunda, terca, quarta, quinta, sexta, sabado;
        ...
        //getters e setters
    }
    
  2. Divide your original record list into 7 different lists, one for each day of the week.

  3. Create a new class list DataTableBean

  4. Creates a new instance of DataTableBean, remove an item from each individual list and fill in the records for each day of the week.

  5. Repeat step 4 until there are no records left in the individual lists.

0

If you want an item-by-line marking, as in a Zebrinha table (http://www.donosdecasa.com/blog/wp-content/uploads/2015/01/zebrinha.jpg) the simplest and easiest way is with the rendered, for a dataTable performs an iteration of a for in the list and the rendered would check if the object of Enum can be displayed in that column.

If you want all records to appear "filling" all columns of a row as per Enum DiaDaSemana of each record, the best way would be to create a class that represents each line and perform that class populate in its ManagedBean, before sending to Xhtml page.

Finally, you could observe the subtable of primefaces (http://www.primefaces.org/showcase/ui/data/datatable/subTable.xhtml) to see if there’s anything that can help.

Browser other questions tagged

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