Sort datatable by attribute

Asked

Viewed 383 times

6

On the JSF page, it takes the Managed bean to list any object.

public class Ob implements Serializable {
    private Integer id;
    private Date data;
    private String descrição;
}

I wanted to set the table more or less like this:

Exemplo de tabela

For each DATE just below all objects that have that date.

I made a solution, but not elegant. I got the List of Ob, I’ve put together a list of unique dates within the bean Managed.

I ended up mounted table in xhtml with the two lists, I ended up putting forEach.

And the code got a little fuzzy, nor will I post here so big that it got.

  • You use primefaces or other framework?

1 answer

1

I managed to solve the problem.

When I request from the DAO return a MAP < Date , List < Myobject > > already ordered by Date. Methods Of The DAO

@PersistenceContext
private EntityManager em;

public Map<Date, List<MyObject>> ultimos30MyObject() throws ParseException{
    Locale BRAZIL = new Locale("pt","BR");
    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy",BRAZIL);
    Map<Date, List<MyObject>> map = new TreeMap<Date, List<MyObject>>(new OrdenarMyObjectDate());

    List<MyObject> lista = getUltimos30MyObject();
    List<String> dataunicas = getDatasUnicas(lista);

    for(String s: dataunicas){
        List<MyObject> lista2= new ArrayList<MyObject>();
        for(MyObject l: lista){
            if(s.equals(df.format(l.getData())))
                lista2.add(l);
        }
        if(!lista2.isEmpty())
            map.put(df.parse(s), lista2);
    }
    return map;
}
@SuppressWarnings("unchecked")
public List<MyObject> getUltimos30MyObject(){

    final String jpql = "select c from MyObject c order by c.id desc"; 
    final Query query = em.createQuery(jpql);  
    query.setFirstResult(0).setMaxResults(30);  
    return (List<MyObject>)query.getResultList(); 
}
private List<String> getDatasUnicas(List<MyObject> lista){
    DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    List<String> datau = new ArrayList<String>();

    for(MyObject l: lista){
        String s = df.format(l.getData());

        if(datau.isEmpty()){
            datau.add(s);
        }
        else{
            boolean okay = true;

            for(String aux : datau){
                if(aux.equals(s))
                    okay=false;
            }

            if(okay)
                datau.add(s);
        }
    }
    return datau;
}

Mybean

@Inject
MyObjectDao dao;
public Map<Date, List<MyObject>> ultimos30MyObject() throws ParseException{
    return dao.ultimos30MyObject();
}

My Commander, the map key(Date) are sorted.

public class OrdenarMyObjectDate implements Comparator<Date> {

    @Override
    public int compare(Date o1, Date o2) {
        return o2.compareTo(o1);
    }
}

On the XHTML page

<c:if test="#{applicationScope.listMyObject == null}">
   <c:set var="listMyObject" value="#{MyBean.ultimos30MyObject()}" scope="application"/>
</c:if>

<ui:repeat value="#{applicationScope.listMyObject.keySet().toArray()}" var="k">
   <h:form>
      <p:dataTable var="obj" value="#{applicationScope.listMyObject.get(k)}" styleClass="borderless tree-table-no-header" rowStyleClass="ui-datatable-odd"> 

         <f:facet name="header">
            <h:outputText value="#{k}">
               <f:convertDateTime pattern="dd/MM/yyyy"/> 
            </h:outputText>
         </f:facet>

         <p:column>
            <h:outputText value="#{obj.id}">
         </p:column>

         <p:column>
            <h:outputText value="#{obj.nome}">
         </p:column>
      </p:dataTable>
   </h:form>
</ui:repeat>

How Map Is in the Application Scope every time I Register new Myobject I update the list within the edit() and register methods()

ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
servletContext.setAttribute("listMyObject", dao.ultimos30MyObject());

Browser other questions tagged

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