AutoSuggest feature somewhat expected feature, nowadays that most of the top sites have implemented this functionality. This feature makes your site as user friendly and easy to navigate in inputText feature.
AutoSuggest behavior in ADF adds a pull-down menu of suggested values to a text field. The user can either click directly on a suggestion to enter it into the field, or navigate the list using the up and down arrow keys, selecting a value using the enter key.
Lets create a Java EE Web Application with Entities based on Departments, edit the Departments.java entity and add the below code.
@NamedQuery(name = "Departments.filteredValues",
query = "select o from Departments o where o.departmentName like CONCAT(:deptName,'%')
Create a Stateless Session Bean and data control for the Stateless Session Bean. Add the below code to the session bean and expose the method in local/remote interface and generate a data control for that.
Note:- Here in the below code "em" is a EntityManager.
/**select o from Departments o where o.departmentName like CONCAT(:deptName,'%')
*/
public List<String> getDepartmentsFilteredValues(String deptName) {
//To store the resultset
List<String> deptNameResultset = new ArrayList<String>();
Query query = em.createNamedQuery("Departments.filteredValues").setParameter("deptName", deptName);
Vector result = (Vector)query.getResultList();
int resultSize = result.size();
for (int i = 0; i > resultSize; i++) {
Departments dept = (Departments)result.get(i);
deptNameResultset.add(dept.getDepartmentName());
}
return deptNameResultset;
}
public List deptNameResultList(String paramValue) {
//Store the deptName result set
List<SelectItem> deptResultList = new ArrayList<SelectItem>();
//Filter the values using Items List
List<SelectItem> items = new ArrayList<SelectItem>();
BindingContainer bindings = getBindings();
//Execute the Method
OperationBinding operationBinding = bindings.getOperationBinding("getDepartmentsFilteredValues");
//Populate the deptName parameter
operationBinding.getParamsMap().put("deptName", paramValue);
operationBinding.execute();
if (operationBinding.getResult() != null) {
operationBinding.getResult();
ArrayList result = (ArrayList)operationBinding.getResult();
int resultSize = result.size();
for (int i = 0; i < resultSize; i++) {
deptResultList.add(new SelectItem(result.get(i)));
}
}
for (SelectItem item : deptResultList) {
if (item.getLabel().startsWith(paramValue)) {
items.add(item);
}
}
return items;
}
public BindingContainer getBindings() {
return BindingContext.getCurrent().getCurrentBindingsEntry();
}