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

Filter list componet

Participant ,
Mar 31, 2012 Mar 31, 2012

Hi, I'm working on a dictionary using Flash components. I'm trying to implement a search field, I manage to filter word and letters, as you do on goggle. What I need is to filter from the beginning of the word as you do on a Dictionary.

Any Help ?

Regards

package com

{

    import flash.display.MovieClip;

    import flash.display.Sprite;

    import flash.net.URLLoader;

    import flash.events.*;

    import flash.net.URLRequest;

    import fl.data.DataProvider;

    import flash.text.Font;

    import flash.text.TextFormat;

    import fl.managers.StyleManager;

    import flash.media.Sound;

    import flash.media.SoundChannel;

    import flash.utils.Timer;

    import com.greensock.*;

    import com.greensock.easing.*;

    import fl.controls.TextInput;

    import fl.events.ComponentEvent;

    public class DictionaryC extends MovieClip

    {

        private var xmlLoader:URLLoader;

        private var dictXML:XML;

        private var nordic:Font = new NordicFont();

        private var arial:Font = new arialBlack();

        private var targetSound:String;

        private var mp3:Sound;

        private var CountDownTimerRestart:Timer;

        private var RestartToCountDown:Number;

        private var componentProviderEN:DataProvider = new DataProvider();

        private var componentProviderIT:DataProvider = new DataProvider();

        private var textInput:TextInput = new TextInput();

        public function DictionaryC()

        {

            //set back to dojo

            TweenMax.to(en_mc, 3, {rotation:7, repeat:-1, yoyo:true, ease:Linear.easeInOut});

            TweenMax.to(it_mc, 3, {rotation:7, repeat:-1, yoyo:true, ease:Linear.easeInOut});

            TweenMax.to(backBtn, 3, {rotation:7, repeat:-1, yoyo:true, ease:Linear.easeInOut});

            TweenMax.to(playAgainBtn, 3, {rotation:7, repeat:-1, yoyo:true, ease:Linear.easeInOut});

            // set font

            var myFormatBlack:TextFormat = new TextFormat();

            myFormatBlack.font = nordic.fontName;

            myFormatBlack.size = 24;

            myFormatBlack.color = 0xFFFFFF;

            var myFormatBlack2:TextFormat = new TextFormat();

            myFormatBlack2.font = arial.fontName;

            myFormatBlack2.size = 32;

            myFormatBlack2.color = 0xFFFFFF;

            // set styles

            dictEN.setRendererStyle("textFormat", myFormatBlack);

            dictEN.setRendererStyle("embedFonts", true);

            dictEN.setRendererStyle("textPadding", 20);

            dictEN.rowHeight = 35;

            dictIT.setRendererStyle("textFormat", myFormatBlack);

            dictIT.setRendererStyle("embedFonts", true);

            dictIT.setRendererStyle("textPadding", 20);

            dictIT.rowHeight = 35;

            dictIT.visible = false;

            //listeners

            dictEN.addEventListener(Event.CHANGE, showData);

            dictIT.addEventListener(Event.CHANGE, showData);

            playAgainBtn.addEventListener(MouseEvent.CLICK, playAgainMp3, false, 0, true);

            en_mc.ENbtn.addEventListener(MouseEvent.CLICK, hidePanelIT, false, 0, true);

            it_mc.ITbtn.addEventListener(MouseEvent.CLICK, hidePanelEN, false, 0, true);

            backBtn.addEventListener(MouseEvent.CLICK, fn_back, false, 0, true);

            textInput.move(230, 20);

            textInput.width = 280;

            textInput.height = 50;

            //Press the enter key to apply filter (search)

            textInput.addEventListener("enter",changeHandler);

            //Everytime the input box is changed, apply filter (search;//(can affect performance);

            textInput.addEventListener(Event.CHANGE, changeHandler);

            textInput.setStyle("textFormat", myFormatBlack2);

            var emptySkin:Sprite = new Sprite();

            textInput.setStyle("upSkin",emptySkin);

            textInput.setStyle("focusRectSkin", emptySkin);

            addChild(textInput);

           

            //timer restart;

            RestartToCountDown = 1;

            CountDownTimerRestart = new Timer(500,RestartToCountDown);

            CountDownTimerRestart.addEventListener(TimerEvent.TIMER, fl_CountDownTimerRestart,false, 0, true);

            CountDownTimerRestart.start();

        }

        private function fl_CountDownTimerRestart(event:TimerEvent):void

        {

            RestartToCountDown--;

           

            if (RestartToCountDown == 0)

            {

                xmlLoader = new URLLoader();

                xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);

                xmlLoader.load(new URLRequest("db/dictionary/dictionary.xml"));

            }

        }

        function changeHandler(event:Event):void

        {

            //apply the name filter

            var arr:Array = componentProviderEN.toArray();

            var filteredArr:Array = arr.filter(textInputFilter);

            dictEN.dataProvider = new DataProvider(filteredArr);

            var arr2:Array = componentProviderIT.toArray();

            var filteredArr2:Array = arr2.filter(textInputFilter);

            dictIT.dataProvider = new DataProvider(filteredArr2);

        }

        function textInputFilter(obj:Object, idx:int, arr:Array):Boolean

        {

            if (textInput.text == "")

            {

                return true;

            }//nothing to search

            //This will make one string out of every piece of data from the XML file.  Not from

            //the DataGrid.  This just filters the DataProvider of the DataGrid to show results

            if (obj.label)

            {

                var searchable:String = obj.Name;

                searchable +=  " " + obj.label;

                if (searchable.toLowerCase().search(textInput.text.toLowerCase()) >= 0)

                {

                    return true;// search value has been found!

                }

            }

            return false;//value not found

        }

        private function xmlLoaded(evt:Event):void

        {

            dictXML = new XML(xmlLoader.data);

            for (var i:uint = 0; i < dictXML.word.length(); i++)

            {

                componentProviderEN.addItem({label:dictXML.word.english, translation:dictXML.word.italian, sound:dictXML.word.@file});

                componentProviderIT.addItem({label:dictXML.word.italian, translation:dictXML.word.english, sound:dictXML.word.@file});

            }

            dictEN.dataProvider = componentProviderEN;

            dictIT.dataProvider = componentProviderIT;

            loading_txt.visible = false;

        }

        private function showData(evt:Event):void

        {

            translateBox.text = evt.target.selectedItem.translation.toString();

            targetSound = "db/dictionary/" + evt.target.selectedItem.sound;

            mp3 = new Sound();

            mp3.load(new URLRequest(targetSound));

            mp3.play();

        }

        private function playAgainMp3(e:MouseEvent):void

        {

            mp3.play();

        }

       

        private function hidePanelIT(e:MouseEvent):void

        {

            if(dictEN.visible == false) {

                dictEN.visible = true;

                dictIT.visible = false;

            }

        }

       

        private function hidePanelEN(e:MouseEvent):void

        {

            if(dictIT.visible == false) {

                dictIT.visible = true;

                dictEN.visible = false;

            }

        }

        private function fn_back(e:MouseEvent):void

        {

            Object(root).runeBackGround.symbolRuneContainer.alpha = 1;

            Object(root).menuScreen.visible = true;

            this.parent.removeChild(this);

        }

    }

}

TOPICS
ActionScript
1.0K
Translate
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

Participant , Apr 01, 2012 Apr 01, 2012

I solved it myseft !

You have now a working code to use

function textInputFilter(obj:Object, idx:int, arr:Array):Boolean

        {

            if (textInput.text == "")

            {

                return true;

            }//nothing to search

            //This will make one string out of every piece of data from the XML file.  Not from

            //the DataGrid.  This just filters the DataProvider of the DataGrid to show results

            if (obj.label)

            {

                var searchable:St

...
Translate
Participant ,
Apr 01, 2012 Apr 01, 2012
LATEST

I solved it myseft !

You have now a working code to use

function textInputFilter(obj:Object, idx:int, arr:Array):Boolean

        {

            if (textInput.text == "")

            {

                return true;

            }//nothing to search

            //This will make one string out of every piece of data from the XML file.  Not from

            //the DataGrid.  This just filters the DataProvider of the DataGrid to show results

            if (obj.label)

            {

                var searchable:String = obj.Name;

                searchable +=  " " + obj.label.substr(0, textInput.text.length);  //THIS CONSTRAINS THE STARTING OF THE SEARCH

                if (searchable.toLowerCase().search(textInput.text.toLowerCase()) >= 0)

                {

                    return true;

                    // search value has been found!

                }

            }

            return false;//value not found

        }

Translate
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