开发者

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开发者_StackOverflow中文版 entered.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜