Skip to main content
Inspiring
May 28, 2015
Answered

Autocomplete search for any word in the sentence

  • May 28, 2015
  • 1 reply
  • 386 views

I'm using an autocomplete for my flash app. The autocomplete uses an external text file. When I'm typing the first word of the sentence, it display all the sentences that begins with this word.

Is it possible to display all sentence that have this word (and not just the begining of the sentence) ?

Exemple : I've got two phrases : "I'm going to school" and " I'm going to look for him".

I would like to be able to type "school" and that it displays the first sentence.

Do you know how I can do that ?

For now, I have to type "I'm going to s" in order to display the first sentence.

Here's my code :  Thank you !

urlLoader.load(new URLRequest("test.txt"));
            urlLoader.addEventListener(Event.COMPLETE, loadComplete);
            inputField.addEventListener(KeyboardEvent.KEY_UP, suggest);

function loadComplete(e:Event):void
        {
            suggestions = e.target.data.split(",");
        }

         function suggest(e:KeyboardEvent):void
        {
            suggested = [];

            for (var i:int = 0; i < textfields.length; i++)
            {
                removeChild(textfields);
            }

            textfields = [];

            for (var j:int = 0; j < suggestions.length; j++)
            {
                if (suggestions.indexOf(inputField.text.toLowerCase()) == 0)
                {
                    var term:TextField = new TextField();
                    term.width = 300;
                    term.height = 20;
                    term.x = 70;
                    term.y = (20 * suggested.length) + 314;
                    term.border = true;
                    term.borderColor = 0x353535;
                    term.background = true;
                    term.backgroundColor = 0xFF9900;
                    term.textColor = 0x4C311D;
                    term.defaultTextFormat = format;

                    term.addEventListener(MouseEvent.MOUSE_UP, useWord);
                    term.addEventListener(MouseEvent.MOUSE_OVER, hover);
                    term.addEventListener(MouseEvent.MOUSE_OUT, out);
                    term.addEventListener(MouseEvent.CLICK, tellMe);

                    addChild(term);
                    textfields.push(term);

                    suggested.push(suggestions);

                    term.text = suggestions;
                }

            }

            if (inputField.length == 0)
            {
                suggested = [];

                for (var k:int = 0; k < textfields.length; k++)
                {
                    removeChild(textfields);
                }

                textfields = [];
            }

            if(e.keyCode == Keyboard.DOWN && currentSelection < textfields.length-1)
            {
                currentSelection++;
                textfields[currentSelection].textColor = 0x4C311D;
            }

            if(e.keyCode == Keyboard.UP && currentSelection > 0)
            {
                currentSelection--;
                textfields[currentSelection].textColor = 0x4C311D;
            }

            if(e.keyCode == Keyboard.ENTER)
            {
                inputField.text = textfields[currentSelection].text;

                suggested = [];

                for (var l:int = 0; l < textfields.length; l++)
                {
                    removeChild(textfields);
                }

                textfields = [];
                currentSelection = 0;
            }
        }

         function useWord(e:MouseEvent):void
        {
            inputField.text = e.target.text;

            suggested = [];

            for (var i:int = 0; i < textfields.length; i++)
            {
                removeChild(textfields);
            }

            textfields = [];
        }

This topic has been closed for replies.
Correct answer Ned Murphy

Based on what I think I see you should only have to change one line...

if (suggestions.indexOf(inputField.text.toLowerCase()) == 0

should become...

if (suggestions.indexOf(inputField.text.toLowerCase()) != -1

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
May 28, 2015

Based on what I think I see you should only have to change one line...

if (suggestions.indexOf(inputField.text.toLowerCase()) == 0

should become...

if (suggestions.indexOf(inputField.text.toLowerCase()) != -1

Inspiring
May 28, 2015

Yes ! It was simply as that ! Thank you very much !