incremental search test

by uwi
♥0 | Line 52 | Modified 2009-07-20 02:07:33 | MIT License
play

ActionScript3 source code

/**
 * Copyright uwi ( http://wonderfl.net/user/uwi )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/hGLq
 */

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    creationComplete="onCreate()"
>
<!-- 上下キーで候補選択、Enterキーで確定 -->

<mx:Script>
    <![CDATA[
        import mx.controls.*;
        import mx.collections.*;
        import flash.events.*;
        
        [Bindable]
        private var _suggest : ArrayCollection = new ArrayCollection();

        private var _candi : Array = [123, 145, 345, 678];
    
        private function onCreate() : void
        {
            searchbox.text = "111";
            _suggest.source = _candi;
        }
        
        private function onChange(e : Event) : void
        {
            _suggest.filterFunction = function(item : Object) : Boolean {
// 中間一致                
                return item.toString().indexOf(searchbox.text) != -1;
// 前方一致
//                return item.toString().substring(0, searchbox.text.length) == searchbox.text;
            };
            _suggest.refresh();
        }
        
        private function onSBKeyDown(e : KeyboardEvent) : void
        {
            if(e.keyCode == Keyboard.DOWN || e.keyCode == Keyboard.UP){
                suggest.setFocus();
                suggest.selectedIndex = 0;
            }
        }
        
        private function onSGKeyDown(e : KeyboardEvent) : void
        {
            if(e.keyCode == Keyboard.ENTER){
                searchbox.setFocus();
                searchbox.callLater(function() : void {
                    if(suggest.selectedItem != null){
                        searchbox.text = suggest.selectedItem.toString();
                        searchbox.selectionBeginIndex = searchbox.text.length;
                        searchbox.selectionEndIndex = searchbox.text.length;
                        onChange(null);
                    }
                });
            }
        }
    ]]>
</mx:Script>
<mx:TextInput id="searchbox" editable="true" restrict=" -&#xffee;" change="onChange(event)" keyDown="onSBKeyDown(event)"/>
<mx:List id="suggest" dataProvider="{_suggest}" keyDown="onSGKeyDown(event)" selectionDuration="0" useRollOver="false" />
<mx:Label id="fordebug" />
</mx:Application>