Doubt with dataTable and / or JSF / JPA modeling

5

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

I want to create a table where the columns are SUNDAY, MONDAY, THIRD, FOURTH, FIFTH, SIXTH, SATURDAY respectively ... and in those columns the data is shown, 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 a call called day, which is an enum with the respective day of the week, persists, edit etc everything is working normally, the problem is that I do not know how to make the records that contain day = MONDAY only appear in the correct column ...

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

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

Someone with some good ideas?

    
asked by anonymous 18.02.2015 / 23:00

2 answers

1

The problem with your approach is that you do not have a data structure appropriate to the JSF component.

I explain. On your system there may be 2 items in the second, 1 on Tuesday and 3 on Wednesday, correct? This works in a common table, but a DataTable component represents what we usually call a grid, that is, a list of records with a defined number of rows, each representing a system record. You can not have 3 items in one column and two in the other.

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

  • DataList : place 7 lists next to each other because you can display 7 independent lists. You will need to split original list by day.
  • Schedule : If your idea is some kind of calendar, use this component to make it much easier to display the events. Use Weekly view, that is, weekly.

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

  • Create a POJO with one attribute for each column of record type. Example:

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

  • Create a new list of class DataTableBean

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

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

  • 22.05.2015 / 19:16
    0

    If you want an item-by-line markup, as in a Zebrinha table ( link ) the simplest and easiest way is with rendered , because dataTable performs an iteration of a for in the list and rendered would check if the Enum object can be displayed in that column .

    If you want all the records to appear by "populating" all the columns of a row according to the% enum% of each record, the best way would be to create a class that represents each row and perform that populate of classes in its DiaDaSemana , before sending it to the Xhtml page.

    Finally, you could see ManagedBean of subtable ( link ) to see if you have anything that might help.

        
    20.02.2015 / 02:58