Introduction:
Developers may like to give their end-user ease of dealing with text fields where end-user suppose to fill any information. Adf faces give us functionality to use Auto-suggest behavior for any input field.
USE CASE: End user is filling some information into some input text field. There what if user will gets a drop down list with some values to be selected and displayed. Refer screen shot for example,
The jspx code for this example is very simple:
<af:inputText label=”Currency” id=”i21″> <af:autoSuggestBehavior suggestedItems=”#{InputSuggestBean.onCurrencySuggest}”/> </af:inputText>Developers just need to bind their af: autoSuggestBehavior tag to a backing bean method returning a list of items to be suggested. The method has only one String argument containing submitted user’s value. Depending on this value you can implement your own logic for the returning list like filtering, sorting, etc. For example, the following backing bean method returns currency codes that match user’s input string:
private final static String[] ccys = {“USD”, “EUR”, “UAH”, “CAD”}; public List onCurrencySuggest(String inputValue) { ArrayList<SelectItem> suggestItems = new ArrayList<SelectItem>(); for (String s: ccys) { if (s.startsWith(inputValue)) suggestItems.add(new SelectItem(s)); } return suggestItems; }Developers can also implement your backing bean method getting suggested items from ADF BC layer in order to retrieve them from database or from other data sources.
Auto Suggestion on demand:
USE CASE: when users don’t need any auto-suggestion for their input, and they want to be suggested on demand only. For example, by pressing “Ctrl-H”, a user is provided by previously submitted values for this input field. Something likes a history of values.
In this post I’m going to show two different implementations of this use-case. The first one is based on modified af: autoSuggestBehavior tag and the second implementation is built using usual af: popup tag.
Modified af:autoSuggestBehavior
The jspx definition of inputText looks as usual:
And the backing bean method looks like this:
public List onSuggest(String string) { ArrayList<SelectItem> selectItems = new ArrayList<SelectItem>(); selectItems.add(new SelectItem(“One”)); selectItems.add(new SelectItem(“Two”)); return selectItems; }af:autoSuggestBehavior tag renders some JavaScript object AdfAutoSuggestBehavior responsible for auto-suggestion functionality. It adds a number of event listeners to the inputText for different event types like onKeyUp, onBlur, onFocus, etc.
In order to prevent default behavior of af: autoSuggestBehavior tag I have to override its onKeyUp listener and do some JavaScript coding:
I’m going to call initMySuggestBehavior JavaScript function in a phase listener written for the f:view tag:
<f:view beforePhase=”#{InputSuggestBean.viewPhaseListener}”>
Code in Managed Bean :
public void viewPhaseListener(PhaseEvent phaseEvent) {
if (phaseEvent.getPhaseId() == PhaseId.RENDER_RESPONSE) {
FacesContext fctx = FacesContext.getCurrentInstance();
ExtendedRenderKitService ks =
Service.getRenderKitService(fctx, ExtendedRenderKitService.class);
StringBuffer script = new StringBuffer();
script.append(“window.initMySuggestBehavior()”);
ks.addScript(fctx, script.toString()); } }
The result of our modifications looks like this:
Using af:popup
The jspx page for this implementation looks like this:
<af:popup id=”ctrlHPopup” contentDelivery=”lazyUncached” animate=”false”> <af:selectOneListbox id=”sol4″ simple=”true” autoSubmit=”false” > <f:selectItems value=”#{InputSuggestBean.popupSuggestion}” id=”si7″/> <af:clientListener type=”keyDown” method=”enterSelection”/> <af:clientListener type=”click” method=”clickSelection”/> </af:selectOneListbox> </af:popup> <af:panelLabelAndMessage label=”Input with popup” id=”plam1″> <af:inputText id=”it2″ simple=”true”> <af:clientListener method=”ctrlHKeyDown” type=”keyDown”/> </af:inputText> </af:panelLabelAndMessage>I have a popup and selectOneListbox component inside it. The listbox get suggested items from the backing bean property popupSuggestion and it has client listeners to submit selected values when Enter is pressed or mouse is clicked. The inputText has a listener to fire popup with suggested items when Ctrl-H is pressed.
The backing bean has the following method:
And the result of our work looks like this:
You can try to enhance this sample based on different use cases.
Sample application can be downloaded from :InputSuggestion
— Email us to get the Application Zip