Thursday, 30 July 2015

Autosuggest Behavior programmatically in ADF

This blog we will see how programmatically achieve the autosuggest behavior.
The below steps we will follow to achieve the autosuggest behavior
1. Create an af:inputText in your page.
2. Add af:autoSuggestBehavior inside the af:inputText with suggestItems. The code should looks like the below

<af:inputText label="Enter First Name : ">
            <af:autoSuggestBehavior suggestItems="#{AutosuggestPageBean.getSuggestedFirstName}"/>
 </af:inputText>

3. The below code needs to write for autosuggest

    public List getSuggestedFirstName(FacesContext facesContext,
                                      AutoSuggestUIHints autoSuggestUIHints) {
        // Add event code here...
        List<String> firstNameUpList = new ArrayList<String>();
        firstNameUpList.add("prabhat");
        firstNameUpList.add("Steven");
        firstNameUpList.add("Neena");
        firstNameUpList.add("Lex");
        firstNameUpList.add("Alexander");
        firstNameUpList.add("Bruce");
        firstNameUpList.add("David");
        firstNameUpList.add("Valli");
        firstNameUpList.add("Diana");
        firstNameUpList.add("Nancy");
        firstNameUpList.add("Daniel");
        firstNameUpList.add("John");
        firstNameUpList.add("Ismael");
        firstNameUpList.add("Jose Manuel");
        firstNameUpList.add("Luis");
        firstNameUpList.add("Den");
        firstNameUpList.add("Alexander");
       
        List suggestedFirstNames = new ArrayList<SelectItem>();

        List<String> firstListval = firstNameUpList;
        Iterator frstItr = firstListval.iterator();
        String inputValue = null;
        String fstNm = null;
        inputValue = autoSuggestUIHints.getSubmittedValue();

        while (frstItr.hasNext()) {
            String inputValueLower = inputValue.toLowerCase(Locale.ENGLISH);
            fstNm = (String)frstItr.next();
            String tempFn = fstNm.toLowerCase(Locale.ENGLISH);
            if (tempFn.startsWith(inputValueLower)) {
                javax.faces.model.SelectItem selectItem =
                    new javax.faces.model.SelectItem();
                selectItem.setLabel(fstNm);
                selectItem.setValue(fstNm);
                suggestedFirstNames.add(selectItem);
            }
        }
        return suggestedFirstNames;
    }

Now run your application it will looks like the below
You can download the code: Download

Thanks..

No comments:

Post a Comment