<p:autocomplete> Multiple.
En algunas ocasiones necesitamos un autocomplete para almacenar múltiples selecciones.
En este ejemplo asignamos múltiples roles a un usuario.
Contamos con esta estructura
Entity
Usuario.java
import com.avbravo.ejbjmoordb.anotations.Embedded;
import com.avbravo.ejbjmoordb.anotations.Id;
import com.avbravo.ejbjmoordb.anotations.Referenced;
import com.avbravo.ejbjmoordb.pojos.UserInfo;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Usuario {
@Id
private String username;
private String password;
private String nombre;
private String cedula;
private String celular;
private String cargo;
private String email;
@Referenced(documment = "Rol",
field = "idrol", javatype = "String", lazy = false,
repository = "com.avbravo.transporteejb.repository.RolRepository")
private List<Rol> rol;
private String activo;
@Embedded
List<UserInfo> userInfo;
public Usuario() {
}
}
Rol.java
import com.avbravo.ejbjmoordb.anotations.Embedded;
import com.avbravo.ejbjmoordb.anotations.Id;
import com.avbravo.ejbjmoordb.pojos.UserInfo;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Rol {
@Id
private String idrol;
private String rol;
private String activo;
@Embedded
List<UserInfo> userInfo;
public Rol() {
}
@Override
public String toString() {
return "Rol{" + "idrol=" + idrol + ", rol=" + rol + '}';
}
}
Componente
<p:autoComplete dropdown="false"
multiple="true"
scrollHeight="250"
size="25"
emptyMessage="#{app['info.nohayregistros']}"
value="#{usuarioController.rolList}"
completeMethod="#{usuarioController.completeFiltrado}"
var="p"
required="true"
itemLabel="#{p.idrol}"
itemValue="#{p}" forceSelection="true">
<f:converter binding="#{rolConverter}"/>
<f:attribute name="field" value="idrol"/>
<p:ajax event="itemSelect" listener="#{rolController.handleSelect}"
/>
<f:facet name="itemtip">
<h:panelGrid columns="1" cellpadding="5">
<h:outputText value="#{msg['field.idrol']} #{p.idrol}" />
<h:outputText value="#{msg['field.rol']} #{p.rol}" />
</h:panelGrid>
</f:facet>
</p:autoComplete>
UsuarioController.java
public List<Rol> completeFiltrado(String query) {List<Rol> suggestions = new ArrayList<>();
List<Rol> temp = new ArrayList<>();
try {
Boolean found = false;
query = query.trim();
if (query.length() < 1) {
return suggestions;
}
String field = (String) UIComponent.getCurrentComponent(FacesContext.getCurrentInstance()).getAttributes().get("field");
temp = rolRepository.findRegex(field, query, true, new Document(field, 1));
if (rolList.isEmpty()) {
if (!temp.isEmpty()) {
suggestions = temp;
}
} else {
if (!temp.isEmpty()) {
for (Rol r : temp) {
found = false;
for(Rol r2:rolList){
if(r.getIdrol().equals(r2.getIdrol())){
found=true;
}
}
if(!found){
suggestions.add(r);
}
}
}
}
//suggestions= rolRepository.findRegex(field,query,true,new Document(field,1));
} catch (Exception e) {
JsfUtil.errorMessage("complete() " + e.getLocalizedMessage());
}
return suggestions;
}
Comments