Autocomplete with Pojo preselected value

UI Components for JSF
Post Reply
fleske
Posts: 4
Joined: 20 Oct 2015, 13:42

02 Feb 2016, 11:17

Hello,

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>
ActivityBean:

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;
	}

...


converter:

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;
	}
}
On page load it should already look like on image 3, with the label and not the id like on the first image below:

Initial load:
Image

Autocomplete:
Image

Autocomplete selected item:
Image

MarkWinton
Posts: 50
Joined: 18 Oct 2013, 23:40

02 Feb 2016, 15:45

There's no problem here, your converter is doing exactly what you asked it to do.

fleske
Posts: 4
Joined: 20 Oct 2015, 13:42

03 Feb 2016, 14:16

Is it? I've declared the itemLabel for the autocomplete and it accepts an POJO as value, but it is not used for the initial value.

If you use a String label in the getAsString method (example Lastname, Firsname) then the value will be also String and you cannot do the getAsObject method correctly.

The problem is that it should maybe initially search and select the value, or display the object like it is stated in itemLabel.

This way you cannot have an autocomplete with initial POJO value.

pimetix
Posts: 6
Joined: 29 May 2015, 07:27

08 Dec 2016, 20:56

I can confirm this bug. Its only a problem with the mobile version though.

The exact same code works perfectly on normal render-kit.

A fix would be greatly appreciated.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: Google [Bot] and 25 guests