Formularios con Ordenacion
A veces necesitamos ordenar en base a ciertos criterios. Generalmente lo implementamos en el list.xhtml.
Usamos un
<p:outputLabel value="#{msg['field.ordenadopor']}"/>
<b:flyOutMenu width="50px">
<b:navCommandLink value="#{msg['field.porproveedor']}" update=":form:dataTable" action="#{articuloController.orderBy('idproveedor')}" />
<b:navCommandLink value="#{msg['field.porcodigo']}" update=":form:dataTable" action="#{articuloController.orderBy('idarticulo')}"/>
<b:navCommandLink value="#{msg['field.porarticulo']}" update=":form:dataTable" action="#{articuloController.orderBy('descripcion')}" />
</b:flyOutMenu>
En el controller
Agregamos un atributo order
En el método move() obtener el atributo de ordenacion
Crear un Document para ordenar
Crear el mètodo orderBy()
private String order="idproveedor";
public void move() {
try {
Document doc;
switch (loginController.get("searcharticulo")) {
case "_init":
Document sort= new Document();
switch(order){
case "descripcion":
sort =new Document("descripcion",1);
break;
case "idarticulo":
sort =new Document("idarticulo",1);
break;
case "idproveedor":
sort =new Document("proveedor.idproveedor",1);
break;
default:
sort =new Document("idarticulo",1);
}
articuloList = articuloRepository.findPagination(page, rowPage, sort);
break;
case "_autocomplete":
//no se realiza ninguna accion
break;
case "idarticulo":
doc = new Document("idarticulo", articulo.getIdarticulo());
articuloList = articuloRepository.findFilterPagination(doc, page, rowPage, new Document("idarticulo", -1));
break;
default:
articuloList = articuloRepository.findPagination(page, rowPage);
break;
}
articuloFiltered = articuloList;
articuloDataModel = new ArticuloDataModel(articuloList);
} catch (Exception e) {
JsfUtil.errorMessage("move() " + e.getLocalizedMessage());
}
}// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="orderBy(String order)">
public String orderBy(String order) {
try {
this.order = order;
loginController.put("searcharticulo", "_init");
page = 1;
move();
} catch (Exception e) {
JsfUtil.errorMessage("orderBy() " + e.getLocalizedMessage());
}
return "";
}
Last updated
Was this helpful?