• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Autocomplete search for any word in the sentence

Contributor ,
May 27, 2015 May 27, 2015

Copy link to clipboard

Copied

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 = [];
        }

TOPICS
ActionScript

Views

319

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , May 28, 2015 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

Votes

Translate

Translate
LEGEND ,
May 28, 2015 May 28, 2015

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
May 28, 2015 May 28, 2015

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines