I'm not sure if this is possible ... but I'd like to use the autoComplete component, where the value attribute is of type String and where the completeMethod returns a List of some heavy object.
It is also a requirement for me to use forceSelection="false"
This is what I think should work (but doesn't):
<p:autoComplete id="it_demandeur"
value="#{utilisateurDemandeurCtrl.critereRechercheDemandeur}"
var="demandeurItem"
itemLabel="#{demandeurItem ne null ? demandeurItem.numeroOW.toString().concat(' - ').concat(demandeurItem.nom) : ''}"
itemValue="#{demandeurItem.nom}"
completeMethod="#{utilisateurDemandeurCtrl.completeDemandeur}"
minQueryLength="3"
cacheTimeout="10000">
<p:column>
#{demandeurItem.numeroOW} - #{demandeurItem.nom}
</p:column>
</p:autoComplete>
This is the method that returns the list of suggestions:
@SuppressWarnings("unchecked")
public List<Demandeur> completeDemandeur(String query) {
StringBuilder jpql = new StringBuilder(128);
jpql.append("SELECT d");
jpql.append(" FROM Demandeur d");
jpql.append(" WHERE UPPER(d.nom) LIKE :query");
jpql.append(" OR d.numeroOW LIKE :query");
Query demandeurQuery = em.createQuery(jpql.toString());
demandeurQuery.setParameter("query", "%" + query.toUpperCase() + "%");
return (List<Demandeur>) demandeurQuery.getResultList();
}
If the user selects a suggestion, it would set the itemValue to the name of the selected suggestion, but display a concatenated string of 2 values from the Demandeur object.
The suggestions do appear and I can select them, but unfortunely, I get this error when submitting the page:
Exception message: /page/utilisateur.xhtml at line 27 and column 50 itemLabel="#{demandeurItem ne null ? demandeurItem.numeroOW.toString().concat(' - ').concat(demandeurItem.nom) : ''}": Property 'numeroOW' not found on type java.lang.String
My understanding is that the var attribute of the autoComplete component is an iterator on the suggestions, so in my case of type Demandeur, not String.
Any help would be appreciated!
Thanks
I am using primefaces 3.5.11, MyFaces implementation for JSF on Websphere 8.5.5.0
Edit:
Here is the converter I tried with
@FacesConverter(value = "demandeurUIConverter")
public class DemandeurConverter implements Serializable, Converter {
private static final long serialVersionUID = 1L;
@Override
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) throws ConverterException {
if (value == null || value.length() == 0) {
return null;
}
ConverterCtrl cc = EJB.lookup(ConverterCtrl.class);
Demandeur d = cc.getDemandeurFromCle(value);
if (d == null) {
d = new Demandeur();
d.setNom(value);
d.setNumeroOW(value);
}
return d;
}
@Override
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) throws ConverterException {
if (value == null) {
return "";
}
Demandeur demandeur = (Demandeur) value;
return demandeur.getNom();
}
}