ADF 11g – the native AutoSuggest behavior

4

The recent ADF 11gR1 PS1 release introduced a component we have been waiting for: the inputText with AutoSuggest. An inputText component that will present suggested values to the user depending on whatever text the user has already entered. The implementation in ADF of the functionality that got AJAX started, based on the original Google Suggest feature:

The auto suggest mechanism in ADF is not so much a new input component as it is a behavior (component) that can be added to inputText components. By adding the properly configured autoSuggest to an inputText, it is turned into an inputText with autoSuggest:

<af:inputText label="Country" value="#{item.country}"
              columns="20" id="it5">
<strong>  &lt;af:autoSuggestBehavior suggestedItems=&quot;#{countrySuggester.getCountries}&quot;/&gt;
</strong>&lt;/af:inputText&gt;

Here we have added the autoSuggestBehavior to an inputText that is used for entering a country. Of course we could have used a dropdown component, combobox or list of values. However, the auto suggest is somewhat more light weight and elegant especially when our collection of countries is far from complete, and we only have a limited number of suggestions to offer – and we certainly would not want to suggest that the user can only choose from the values we are proposing.

When the input text has focus, a list of suggested values is presented. As soon as the user has typed a character, a filtered list of suggested values is presented. Well, that seems the obvious thing to do. However: what items are presented as suggested values is entirely up to us! The autoSuggestBehavior component is really nothing but a fairly fancy UI component. It does not filter, sort, suggest etc on its own accord. Everything that has to do with what values are suggested, how many of them and in what order must be programmed by us ourselves, in the method referred to by the attribute suggestedItems. If you only want to show suggestions when the user has typed at least 3 characters: again, you write that logic into the suggesteditems method.

The autoSuggestBehavior can be used whenever and wherever an inputText component can be used, for example inside a table:

Or inside a Carousel:

Note that the list of suggested values supports:

  • traversing the list by using the up and down arrow, starting with down arrow when the focus is still on the inputText component (it does not wrap around – i.e. when on the last item, typing down does nothing; it does not make the first item in the suggested values list the selected on)
  • selecting a suggested value from the list (either with mouse or enter key) and thus applying that value to the inputText component

Also note that since we control whatever values are shown in the suggested values list, we probably can find slightly alternative ways of using this functionality whereby the suggested values are not necessarily based on the alphabetic filtering of a predefined list based on the exact string entered by the user.

Implementing Auto Suggest Behavior

The only thing we need to do in order to implement the auto suggest functionality, apart from adding the autoSuggestBehavior tag to the inputText component of choice, is implement the method that returns the the suggested values. This method needs to have this signature:

 public List&lt;SelectItem&gt; methodName(String currentValueInInputText)

This method is entirely responsible for determining the values to be presented, the number of them, the order of them etc.

The suggested list of countries is produced by the countrySuggester bean:

public class CountrySuggester {

  private List&lt;SelectItem&gt; countries = new ArrayList&lt;SelectItem&gt;();

  public CountrySuggester() {
    countries.add(new SelectItem(&quot;Belgium&quot;));
    countries.add(new SelectItem(&quot;Bulgaria&quot;));
    ...
  }

<strong>  public List&lt;SelectItem&gt; getCountries(String country) {
    //todo
    List&lt;SelectItem&gt; items = new ArrayList&lt;SelectItem&gt;();
    for(SelectItem item:countries){
      if (item.getLabel().startsWith(country)) {
        items.add(item);
      }
    }
    return items;
  }
</strong>}

Resources

Download JDeveloper Fusion Application: DemoAppWithAutoSuggest.

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

4 Comments

  1. Is there any way to provide the listener for the selected item for the autosuggested list as I need the selected item label and also value?
     
    Any reference or any working example is really helpful.

  2. Richard Passmore on

    This looks like exactly what I’ve been looking for. I’m curious which version of JDeveloper you are using for this “Auto Suggest” feature..? I have JDev 11.1.1.1.0 installed and it doesn’t seem to know about this behaviour.

  3. Nice! Could the suggestedItems come from a ViewObject too? Declaratively might be difficult I suppose. Taking your example – we might not want to force the user to choose the country from our own list, as per an LOV (in case it is incomplete), but feed the suggested items from, say, a union between names in our “countries” ref table and those already entered by other users.