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
(suspended)
accept rate:
0%