Autocomplete DropDownChoice

322 views Asked by At

I am looking for the simplest way to apply autocomplete to a wicket 8 dropdownchoice with a list of names.

I just want a hint..

List<User> list = getUsers();

final DropDownChoice<User> dropdown = new DropDownChoice<User>("dropdown",
                new PropertyModel<User>(this, "selected"), list, renderer) { //code };
2

There are 2 answers

0
martin-g On

DropDownChoice component produces plain HTML <select> element.

Your options are:

  1. use JavaScript library that converts plain HTML Select elements to autocompleter like Select2.js, Chosen.js and similar. You can use WicketStuff-Select2 or just do $.select2('#yourSelectId') manually
  2. use Wicket-Extensions AutoCompleteTextField instead of DropDownChoice
0
AudioBubble On

I am using this piece of code but I am not sure if it works well..

dropdown.add(new AutoCompleteBehavior(new StringAutoCompleteRenderer()){
                /**
                 * 
                 */
                private static final long serialVersionUID = 1L;

                @Override
                protected Iterator getChoices(String input) {
                    List<String> completions = new ArrayList();
                    Iterator iter = list.iterator();
                    while(iter.hasNext()){
                        String user  = ((User) iter.next()).getAddress();
                        if(user.startsWith(input)){
                            completions.add(user);
                        }
                    }                   
                    return completions.iterator();
                }               
            });