I'd like to build an input field with the autocomplete widget, so the user can search for one of our dealer locations.

To do this, I've created a class Dealer and a class AutocompleteSource which should provide the data structure and the data itself. They look like this:

AutocompleteSource.java

package ch.company.ivy.investitionsantrag;

import java.util.ArrayList;
import java.util.List;

import ch.company.ivy.investitionsantrag.Dealer;
import ch.ivyteam.ivy.environment.Ivy;

import com.unboundid.ldap.sdk.LDAPConnection;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldap.sdk.LDAPSearchException;
import com.unboundid.ldap.sdk.SearchResult;
import com.unboundid.ldap.sdk.SearchResultEntry;
import com.unboundid.ldap.sdk.SearchScope;

public class AutocompleteSource {
    public LDAPConnection ldapServer;

    public AutocompleteSource() throws LDAPException {
        this.ldapServer = new LDAPConnection(Ivy.var().get("LDAPHost"), Integer.parseInt(Ivy.var().get("LDAPPort")));
        ldapServer.bind(Ivy.var().get("LDAPUser"), Ivy.var().get("LDAPPassword"));
    }

    public List<Dealer> searchDealers(String query) throws LDAPSearchException {
        List<Dealer> dealers = new ArrayList<Dealer>();
        SearchResult searchResult = this.ldapServer.search("dc=COMPANY,dc=CH", SearchScope.SUB, "(&(|(imDealerNetwork=CHE001)(imDealerNetwork=CHE017)(imDealerNetwork=CHE003)(imDealerNetwork=CHE010))(|(cn=*" + query + "*)(imCity=*" + query + "*)))");

        for (SearchResultEntry entry : searchResult.getSearchEntries()) {
             dealers.add(new Dealer(entry.getAttributeValue("cn"), entry.getAttributeValue("imCity")));
         }

        return dealers;
    }
}

Dealer.java

package ch.company.ivy.investitionsantrag;

public class Dealer {
    public String id;
    public String city;
    public String label;

    public Dealer(String id, String city) {
        id = id.replaceFirst("^0+(?!$)", "");
        this.id = id;
        this.city = city;
        this.label = id + " " + city;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

}

I've created a method dealers which takes the query string and executes the following code:

import ch.company.ivy.investitionsantrag.*;
AutocompleteSource source = new AutocompleteSource();
in.dealers = source.searchDealers(in.query);

And finally, my call of the autocomplete in the view:

<p:autoComplete id="dealer" completeMethod="#{logic.dealers}" required="false" var="dealer" itemLabel="#{dealer.label}" itemValue="#{dealer.id}" forceSelection="true" value="#{data.dealer}" />

The autocomplete works, it takes the query and gets the results from the LDAP server. But when I select one of the suggestions it puts the whole label into the input field instead of only the id. Am I doing something wrong?

Update: I just noticed that it in fact does use the value specified in itemValue - it's just in the background. Anyway to tell primefaces to make the visible value in the input field the same as in the background?

asked 19.01.2014 at 18:43

ahatius's gravatar image

ahatius
(suspended)
accept rate: 0%

edited 19.01.2014 at 19:58


itemLabel specifies what is displayed in the list and in the input field. If you want to have different entries in the autocomplete list than in the input field you can specify <p:column> child elements to specify the list entries. See also the primefaces demo AutoComplete - Pojo the field "Custom Content" shows a list of players inclusive their image. If you select one then only the name is written in the text field.

link

answered 20.01.2014 at 10:08

Reto%20Weiss's gravatar image

Reto Weiss ♦♦
4.9k202857
accept rate: 74%

edited 20.01.2014 at 10:09

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×2

Asked: 19.01.2014 at 18:43

Seen: 4,429 times

Last updated: 20.01.2014 at 10:09