af:autosuggestBehavior – intro


This component in adf is used to provide a declarative way of providing suggestions for adf input components
the three main attributes are
suggestItems – mapped to a bean which returns List of SelectItems.
maxSuggestedItems – number of suggestedItems. -1 will fetch everything from the server. if the number is limited then a more link is displayed to fetch remaining items from the server
smartList – this list is added to show intial result. then the entire result from the server is fetched if no operation is done by the user

problem: I have a destination inputText which takes city/state combination. I want to implement an autosuggestbehavior for the inputText. When I enter more than three character the suggestion box should appear which City/States.

1) create an af:inputText, the value of the inputText is mapped to a managedBean to store the value that is entered by the user

<af:inputText label="Destination" id="end"
contentStyle="width: 133px;margin-left:5px;"
value="#{mapBean.dest}"/>

//managedbean code
private String dest;
public void setDest(String dest) {
this.dest = dest;
}

public String getDest() {
return dest;
}

2) now add an af:autosuggestbehavior tag to the inputText. This takes two attribute values. suggestItems and maxSuggestItems. provide maxSuggestItems as ‘5’ to display 5 values. suggestItems is bounded to the managedbean with a method ‘destination’ which returns a List of SelectItems.

<af:inputText label="Destination" id="end"
value="#{mapBean.dest}">
<af:autoSuggestBehavior suggestItems="#{mapBean.destination}"
maxSuggestedItems="5"/>
</af:inputText>

//code
public List destination(FacesContext facesContext,
AutoSuggestUIHints autoSuggestUIHints) {
//create suggestion list
List<SelectItem> items = new ArrayList<SelectItem>();
// the list should activate after three character
if(autoSuggestUIHints.getSubmittedValue().length() >= 3){
//get the binding from the pagedef
DCIteratorBinding bindings = getIteratorBinding("CityStateIterator");

OperationBinding operation = null;
String value = autoSuggestUIHints.getSubmittedValue();

if(value.contains(",")){
//executing the operation binding that will execute a viewcrtieria from ViewImpl
operation = bindings.getBindingContainer().getOperationBinding("searchByCityState");
//bind variable for city and state
String city = value.substring(0, value.indexOf(","));
String state = value.substring(value.indexOf(","), value.length());
operation.getParamsMap().put("city", city);
operation.getParamsMap().put("state", state.replace(", ", ""));
//execute the view criteria
operation.execute();
}

if (operation.getResult() != null) {
RowSet result = (RowSet) operation.getResult(); // cast to the expected result type
//the result is stored in the list
items = populateSuggestionList(items, result);
}
}
//show suggestions
return items;
}
//populate the values in a List of SelectItems
private List<SelectItem> populateSuggestionList(List<SelectItem> items,
RowSet vo) {
//populate the suggestion items
RowSet rs = vo.getRowSet();
while (rs.hasNext()) {
Row rw = rs.next();
items.add(new SelectItem(rw.getAttribute("CITY").toString().trim() + ", " + rw.getAttribute("STATE").toString().trim(),
((String)rw.getAttribute("CITY")).trim() + ", " +
((String)rw.getAttribute("STATE")).trim()));
}

return items;
}

output:

to learn more you can refer these links
http://www.oracle.com/technetwork/developer-tools/adf/learnmore/62-autosuggestbehavior-177811.pdf
http://www.baigzeeshan.com/2010/09/using-afautosuggestbehavior-in-oracle.html

Advertisements

7 thoughts on “af:autosuggestBehavior – intro

  1. Ann

    In line no 15 ,DCIteratorBinding bindings = getIteratorBinding(“CityStateIterator”); can you please give the getIteratorBinding() method code also

    Reply
  2. vtkrishn Post author

    You should change the logic according to your need here..
    // the list should activate after three character
    if(autoSuggestUIHints.getSubmittedValue().length() >= 3){

    add and else condition to put your logic. or change the character length to more than 0

    Reply
    1. Nicolas Pilot

      Thanks for replying. My need is that the autosuggest triggers only when more than 3 characters are entered. When the user enters less than 3 characters, nothing should happen (I don’t want to see the No results found message). But I don’t think that it is possible… Please advise?

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s