ejbjmoordb Método complete(String query)
Se utiliza para hacer filtros para componentes autocomplete.
Se utiliza para hacer filtros para componentes autocomplete.
Componentes
<p:autocomplete>
Necesitamos definir los atributos
Componente
<?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:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:composite="http://java.sun.com/jsf/composite"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<composite:interface >
<composite:attribute name="value" />
<composite:attribute name="multiple" default="false" />
<composite:attribute name="dropdown" default="false"/>
<composite:attribute name="minQueryLength" default="1"/>
<composite:attribute name="itemLabel" />
<composite:attribute name="update" />
<composite:attribute name="rendered"/>
<composite:attribute name="field"/>
<composite:attribute name="fromstart" default="true"/>
<composite:attribute name="required" default="false" />
<composite:attribute name="size" default="25"/>
<composite:attribute name="listener"
method-signature="void handleSelect(org.primefaces.event.SelectEvent)" />
</composite:interface>
<composite:implementation>
<p:autoComplete dropdown="#{cc.attrs.dropdown}"
scrollHeight="250"
size="#{cc.attrs.size}"
multiple="#{cc.attrs.multiple}"
emptyMessage="#{app['info.nohayregistros']}"
value="#{cc.attrs.value}"
completeMethod="#{tamanoController.tamanoServices.complete}"
var="p"
required="#{cc.attrs.required}"
itemLabel="#{cc.attrs.itemLabel}"
itemValue="#{p}"
forceSelection="true">
<f:converter binding="#{tamanoConverter}"/>
<f:attribute name="field" value="#{cc.attrs.field}"/>
<f:attribute name="fromstart" value="#{cc.attrs.fromstart}"/>
<f:attribute name="fielddropdown" value="#{cc.attrs.dropdown}"/>
<f:attribute name="fieldminquerylength" value="#{cc.attrs.minQueryLength}"/>
<p:ajax event="itemSelect" listener="#{cc.attrs.listener}"
update="#{cc.attrs.update}" />
<f:facet name="itemtip">
<h:panelGrid columns="1" cellpadding="5">
<h:outputText value="#{msg['field.idtamano']} #{p.idtamano}" />
<h:outputText value="#{msg['field.descripcion']} #{p.descripcion}" />
</h:panelGrid>
</f:facet>
</p:autoComplete>
</composite:implementation>
</html>
Services
Definir el método complete.
public List<Tamano> complete(String query) {
List<Tamano> suggestions = new ArrayList<>();
try {
suggestions = repository.complete(query);
} catch (Exception e) {
JsfUtil.errorMessage("complete() " + e.getLocalizedMessage());
}
return suggestions;
}
Código completo
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.avbravo.storeejb.services;
import com.avbravo.storeejb.repository.TamanoRepository;
import com.avbravo.avbravoutils.JsfUtil;
import com.avbravo.storeejb.entity.*;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import org.bson.Document;
/**
*
* @authoravbravo
*/
@Stateless
public class TamanoServices {
@Inject
TamanoRepository repository;
List<Tamano> tamanoList = new ArrayList<>();
public List<Tamano> complete(String query) {
List<Tamano> suggestions = new ArrayList<>();
try {
suggestions = repository.complete(query);
} catch (Exception e) {
JsfUtil.errorMessage("complete() " + e.getLocalizedMessage());
}
return suggestions;
}
// <editor-fold defaultstate="collapsed" desc="getTamanoList()">
public List<Tamano> getTamanoList() {
try {
tamanoList = repository.findAll(new Document("idtamano", 1));
} catch (Exception e) {
JsfUtil.errorMessage("getTamanoList() " + e.getLocalizedMessage());
}
return tamanoList;
}// </editor-fold>
public void setTamanoList(List<Tamano> tamanoList) {
this.tamanoList = tamanoList;
}
}
Controller
Injectar el Services, crear mètodos get/set
@Inject
TamanoServices tamanoServices;
public TamanoServices getTamanoServices() {
return tamanoServices;
}
public void setTamanoServices(TamanoServices tamanoServices) {
this.tamanoServices = tamanoServices;
}
Pagina .xhtml
<p:outputLabel value="#{msg['field.descripcion']}"/>
<e:autocompletetamano listener="#{tamanoController.handleSelect}"
value="#{tamanoController.tamanoSelected}"
itemLabel=" #{p.descripcion}"
field="descripcion"
update=" :form:dataTable"/>
Comments