views:

36

answers:

1

There is an inbuilt tag for this purpose.

User enters a character in the textbox, Strings which start with the character entered should be displayed in the form of a list. The item selected from the list should be populated in the textbox.

P.S: The examples and demo available display Strings that contain the character entered. But I want only those strings to be displayed that start with the character entered.

+1  A: 

A way to do that is shown in the wiki page of the pluguin where it says: Autocompleter that handle a JSON Result. Yo just set that code in your jsp, and then you implement something like this in your action:

    private static String[] staticLanguages = { ...a list... };                                                                                      
    private String term;
    private String[] languages  = Autocompleter.staticLanguages;
    public String execute() throws Exception {
            if (term != null && term.length() > 1)
            {
                    ArrayList<String> tmp = new ArrayList<String>();
                    for (int i = 0; i < staticLanguages.length; i++)
                    {
                            if (StringUtils.contains(staticLanguages[i].toLowerCase(), term.toLowerCase()))
                            {
                                    tmp.add(staticLanguages[i]);
                            }
                    }
                    languages = tmp.toArray(new String[tmp.size()]);
            }
            return SUCCESS;
    }

Just change StringUtils.contains line and check instead if the begining is the same.

The jsp tag would be:

<sj:autocompleter 
    name="term"
    id="languages" 
    href="%{remoteurl}" 
    delay="50" 
    loadMinimumCount="2"
/>

I think this should work. Just take a look at the example code in the wiki and try it out.

1000i1