2

My title may be a bit fuzzy but I want to make my suggestbox to suggest words like this when I type letters into the textbox:

Letter typed in suggestbox: A

A lpaca

A pple

A rgon

NOT like this:

Letter typed in suggestbox:A

A lpaca a cute

A pple a nion

A rgon a ttire

is there any way to make the suggest box to behave like this? I just want the first word couple of words that matches the letter, not including the string with multiple words.

I am using GWT in Eclipse by the way.

EDIT: I suck at formatting, the words are

Alpaca

Apple

Argon


Alpaca acute

Apple anion

Argon attire

EDIT AGAIN: I want them to be appear like this:

Entry: Exec

Suggestion:

Execute

Execution

Executor

Entry: Execution t

Suggestion:

Execution time

Execution timer

Execution title

Basically I want it to work like google searches, where the multiple worded suggestions won't appear as long as I haven't typed a second word.

NOOB_USER
  • 163
  • 1
  • 2
  • 10
  • Are you using any of the GWT frameworks like SmartGWT or something? – biplav Oct 30 '13 at 07:17
  • I think you have to create your own version of SuggestOracle. – Fedy2 Oct 30 '13 at 07:35
  • @biplav just the gwt plugin for eclipse indigo, which can be downloaded in the Install New Software. – NOOB_USER Oct 30 '13 at 07:42
  • Have you checked this? http://www.gwtproject.org/javadoc/latest/com/google/gwt/user/client/ui/SuggestBox.DefaultSuggestionDisplay.html You might override showSuggestions to exclude/filter. – biplav Oct 30 '13 at 07:51
  • so you would like to ignore all words which appear after first "space" ? If is that you can prepare your list of strings before adding it to MultiWordOracle. For every string, leave only the first word. – fascynacja Oct 30 '13 at 08:13
  • @biplav, I'm currently sifting through it, thanks. – NOOB_USER Oct 30 '13 at 08:18
  • @fascynacja, not really ignore, but I don't want multiple words to appear if I haven't typed in them yet, ex: Input> E, result> Execute Etc. Input>Execution tim, result> Execution time Execution timer – NOOB_USER Oct 30 '13 at 08:23

1 Answers1

2

Extending SuggestOracle is the way to go! Please check the code below... Once your implementation is correct, pass a new instance of your oracle to your SuggestBox.

s.startsWith(userInput) answers the core of your needs. But you can write other conditions of course.

   public class MySuggestOracle extends SuggestOracle {

        private List<String> data;

        public MySuggestOracle(List<String> data) {
            this.data = data;
        }

        @Override
        public void requestSuggestions(final Request request, final Callback callback) {
            String userInput = request.getQuery();
            List<Suggestion> suggestions = new LinkedList<Suggestion>();
            for (final String s : data) {
                if (s.startsWith(userInput)) {
                    suggestions.add(new Suggestion() {
                        @Override
                        public String getReplacementString() {
                            return s;
                        }

                        @Override
                        public String getDisplayString() {
                            return s;
                        }
                    });
                }
            }
            Response response = new Response(suggestions);
            callback.onSuggestionsReady(request, response);
        }
    }
Olivier Tonglet
  • 3,312
  • 24
  • 40