使用组合框构建自动完成搜索输入

时间:2013-05-22 16:59:52

标签: actionscript-3 flash

我正在尝试创建一个组合框,它将填充可靠的自动完成结果,我首先将数组中的所有单词与用户输入进行比较,然后将其放入switch语句中,将单词添加到组合框中。但是,当输入改变时,我无法从组合框中删除结果。

var dictionary:Vector.<String> = new Vector.<String>();
dictionary.push("University Chapel");
dictionary.push("IT Building");
dictionary.push("Student Centre");
dictionary.push("EMS Building");
dictionary.push("EMB Building");
dictionary.push("Monastry Hall");
dictionary.push("Conference Centre");
dictionary.push("Client Service Centre");
var objects:Vector.<MovieClip> = new Vector.<MovieClip>();
stage.focus = inputBox;
inputBox.addEventListener(Event.CHANGE, onCompletions);
function onCompletions(event:Event):void{
for(var i:int = 0; i < dictionary.length; ++i){
    if(dictionary[i].indexOf(inputBox.text) >= 0){
        switch(dictionary[i])   {
            case 'IT Building':
                cbox.addItemAt({label:"IT Building", data:"screenData" + newRow},0);  
                break;
            case 'University Chapel':
                cbox.addItemAt({label:"University Chapel", data:"screenData" + newRow},0);
                break;

            case 'Student Centre':
                cbox.addItemAt({label:"Student Centre", data:"screenData" + newRow},0);
                break;

            case 'EMS Building':
                cbox.addItemAt({label:"EMS Building", data:"screenData" + newRow},0);
                break;

            case 'EMB Building':
                cbox.addItemAt({label:"EMB Building", data:"screenData" + newRow},0);
                break;

            case 'Monastry Hall':
                cbox.addItemAt({label:"Monastry Hall", data:"screenData" + newRow},0);
                break;

            case 'Conference Centre':
                cbox.addItemAt({label:"Conference Centre", data:"screenData" + newRow},0);
                break;

            case 'Client Service Centre':
                cbox.addItemAt({label:"Client Service Centre", data:"screenData" + newRow},0);
                break;
        }
    }
    else    {
        //cbox.removeAll(); //Where I attempted to remove all the results
    }
}

}

所以我试图从组合框中删除结果并重新评估然后重新插入。正如一个侧面的问题是有没有办法通过actionscript扩展组合框?

提前致谢

1 个答案:

答案 0 :(得分:0)

如果有人有兴趣,我会让它发挥作用,所以这基本上会创建一个自动完成搜索输入框。

var dictionary:Vector.<String> = new Vector.<String>();
dictionary.push("University Chapel");
dictionary.push("IT Building");
dictionary.push("Student Centre");
dictionary.push("EMS Building");
dictionary.push("EMB Building");
dictionary.push("Monastry Hall");
dictionary.push("Conference Centre");
dictionary.push("Client Service Centre");

stage.focus = inputBox;

inputBox.addEventListener(Event.CHANGE, onCompletions);
cbox.open();
function onCompletions(event:Event):void{
cbox.removeAll();
cbox.close();
for(var i:int = 0; i < dictionary.length; ++i){
    if(dictionary[i].indexOf(inputBox.text) >= 0 && inputBox.text != ''){
        switch(dictionary[i])   {
            case 'IT Building':
                cbox.addItemAt({label:"IT Building", data:"screenData"},0); 
                cbox.open();
                break;
            case 'University Chapel':
                cbox.addItemAt({label:"University Chapel", data:"screenData"},0);
                cbox.open();
                break;

            case 'Student Centre':
                cbox.addItemAt({label:"Student Centre", data:"screenData"},0);
                cbox.open();
                break;

            case 'EMS Building':
                cbox.addItemAt({label:"EMS Building", data:"screenData"},0);
                cbox.open();
                break;

            case 'EMB Building':
                cbox.addItemAt({label:"EMB Building", data:"screenData"},0);
                cbox.open();
                break;

            case 'Monastry Hall':
                cbox.addItemAt({label:"Monastry Hall", data:"screenData"},0);
                cbox.open();
                break;

            case 'Conference Centre':
                cbox.addItemAt({label:"Conference Centre", data:"screenData"},0);
                cbox.open();
                break;

            case 'Client Service Centre':
                cbox.addItemAt({label:"Client Service Centre", data:"screenData"},0);
                cbox.open();
                break;
        }
    }
}

}