Jaguar
Jaguar

Reputation: 1

I need to implement an Auto complete Utility using Struts2-JQuery plugin

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.

Upvotes: 0

Views: 1677

Answers (2)

Nirmal Dhara
Nirmal Dhara

Reputation: 2141

I made my own utility for autocomplete using Struts2 and Jquery which read the data from oracle and shows the suggestions list,you can change accordingly for your requirement. Please download the code from here http://javaant.com/dynamic-autocomplete-using-jquery-struts2-and-oracle/#.V0RxL5N96Hs

Upvotes: 0

1000i1
1000i1

Reputation: 115

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.

Upvotes: 1

Related Questions