I've implemented the autocomplete like in the mobile showcase example and it is working.
I have now the problem when i set the initial value. The id of the object is displayed, not the label set in the autocomplete...any help please, thanks.
xhtml:
Code: Select all
<pm:field>
<p:outputLabel value="#{msg['activity.contact']}" for="contactAutocomplete"/>
<p:autoComplete id="contactAutocomplete"
disabled="#{activityBean.readonly}" autocomplete="off"
queryDelay="1000" value="#{activityBean.contact}"
completeMethod="#{activityBean.contactOverviews}" var="c"
styleClass="ui-select"
itemLabel="#{c.familyName}, #{c.firstName}" itemValue="#{c}"
converter="contactOverviewConverter">
</p:autoComplete>
</pm:field>
Code: Select all
private ContactOverview contact;
...
public List<ContactOverview> contactOverviews(String query) {
List<ContactOverview> allContacts = constantsBean.getActivityConstants().getContacts();
List<ContactOverview>contacts = new ArrayList<ContactOverview>();
for (ContactOverview contact : allContacts) {
if (StringUtils.startsWithIgnoreCase(contact.getFamilyName(), query)
|| StringUtils.startsWithIgnoreCase(contact.getFirstName(), query)) {
contacts.add(contact);
}
}
return contacts;
}
...
Code: Select all
@FacesConverter("contactOverviewConverter")
public class ContactOverviewConverter implements Converter {
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (value instanceof ContactOverview) {
return Long.toString(((ContactOverview) value).getId());
}
return null;
}
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value != null && value.trim().length() > 0) {
return Long.parseLong(value);
}
return null;
}
}
Initial load:
Autocomplete:
Autocomplete selected item: