如何在 Flex 中制作自定义自动完成组件?

发布于 2024-10-05 06:13:15 字数 1163 浏览 0 评论 0原文

我需要在 Flex 中制作一个自动完成组件,使用 Web 服务从远程数据库获取自动完成结果。我已经解决了网络服务和查询部分。我已经通过扩展 VBox 在动作脚本中制作了自定义组件。但是,我无法弄清楚如何生成应该显示在自动完成文本框中的文本输入下方的弹出窗口。

目前我正在使用类似

PopUpManager.addPopUp(popup, parentComponent);

My popup class extends VBox 的东西,它扩展了 createChildren 方法,如下所示。

protected override function createChildren():void
{
  for (var i:int = 0; i < results.length; i++) {
    var itemC:UIComponent =
        factory.getComponent(results[i]);
    addChild(itemC);
    itemC.addEventListener(MouseEvent.CLICK,
        getClickFunction(i));
}

private function getClickFunction(index:int):Function {
    return function (event:MouseEvent):void
        {
            selectedIndex = index;
        };
}

不幸的是,当 webservice 检索其结果并调用 addPopUp 时,没有任何显示。

当前factory.getComponent方法正在执行此代码

public function getComponent(user:Object):UIComponent
{
    var email:Label = new Label();
    email.text = user.email;
    var name:Label = new Label();
    name.text = user.displayName;
    var vbox:VBox = new VBox();
    vbox.addChild(name);
    vbox.addChild(email);
    return vbox;
}

I need to make an auto complete component in flex that fetches the auto complete results from a remote database using a webservice. I have the webservice and querying part worked out. I've already made custom components in action script by extending VBoxes. However I cannot figure out how to generate the popup window that is supposed to show under the text input in my auto complete text box.

Currently I am using something like

PopUpManager.addPopUp(popup, parentComponent);

My popup class extends VBox and it extends the createChildren method as follows

protected override function createChildren():void
{
  for (var i:int = 0; i < results.length; i++) {
    var itemC:UIComponent =
        factory.getComponent(results[i]);
    addChild(itemC);
    itemC.addEventListener(MouseEvent.CLICK,
        getClickFunction(i));
}

private function getClickFunction(index:int):Function {
    return function (event:MouseEvent):void
        {
            selectedIndex = index;
        };
}

Unfortunately when the webservice retrieves its results and addPopUp is called, nothing shows up.

Currently the factory.getComponent method is executing this code

public function getComponent(user:Object):UIComponent
{
    var email:Label = new Label();
    email.text = user.email;
    var name:Label = new Label();
    name.text = user.displayName;
    var vbox:VBox = new VBox();
    vbox.addChild(name);
    vbox.addChild(email);
    return vbox;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠栀 2024-10-12 06:13:15

我认为你应该寻找已经实施过这一点的人。虽然您的问题可能与在调用 addPopup() 之前未定位和调整组件大小有关,即使我们帮助您解决了您仍然有很多工作要做的问题。 (顺便说一句,在您的覆盖中调用 super.createChildren ,否则会发生不好的事情)。无论如何,请查看:

http://www.adobe .com/cfusion/exchange/index.cfm?event=extensionDetail&extid=1047291

I think you ought to look for someone who has already implemented this. While your issue is probably related to not positioning and sizing the component before calling addPopup() even if we helped you solve that you still have a lot more work todo. (BTW call super.createChildren in your override or else bad things will happen). Anyway, check this out:

http://www.adobe.com/cfusion/exchange/index.cfm?event=extensionDetail&extid=1047291

我爱人 2024-10-12 06:13:15

最后我弄清楚了如何使用 List 控件,并且不再使用工厂来生成组件,而是使用列表控件中的 itemRenderer 功能。我还用它来替换自定义弹出类,并添加了一个稍后调用的定位函数。通过组合这些东西,我能够使下拉菜单按预期显示。看来某些组件不能很好地用作弹出窗口。

无论如何,工作弹出代码位于

我的自动完成组件内部,它扩展了

dropDownList = new List();
dropDownList.itemRenderer = itemRenderer;
dropDownList.dataProvider = results;
dropDownList.labelFunction = labelFunction;
dropDownList.rowCount = results.length;
dropDownList.labelFunction = labelFunction==null ?
    defaultLabelFunction : labelFunction;
dropDownList.tabFocusEnabled = false;
dropDownList.owner = this;
PopUpManager.addPopUp(IFlexDisplayObject(dropDownList), DisplayObject(this));
callLater(positionDropDownList);

自动完成组件中的 HBox 方法(textInput 是我的文本字段)

public function positionDropDownList():void {
    var localPoint:Point = new Point(0, textInput.y);
    var globalPoint:Point = localToGlobal(localPoint);
    dropDownList.x = globalPoint.x;
    var fitsBelow:Boolean = parentApplication.height - globalPoint.y - textInput.height > dropDownList.height;
    var fitsAbove:Boolean = globalPoint.y > dropDownList.height;
    if (fitsBelow || !fitsAbove) {
        dropDownList.y = globalPoint.y + textInput.measuredHeight;
    } else {
        dropDownList.y = globalPoint.y - dropDownList.height;
    }
}

位置函数是我从 http://hillelcoren.com/flex-autocomplete/

Finally I figured out how to use the List control and I stopped using a factory to generate components, instead I use the itemRenderer feature in the list control. I also used that to replace the custom popup class, and I added a positioning function to be called later. By combining these things I was able to get the drop down to display as expected. It appears that some components do not work well as pop ups.

Regardless, the working pop up code is

Inside my autocomplete component which extends HBox

dropDownList = new List();
dropDownList.itemRenderer = itemRenderer;
dropDownList.dataProvider = results;
dropDownList.labelFunction = labelFunction;
dropDownList.rowCount = results.length;
dropDownList.labelFunction = labelFunction==null ?
    defaultLabelFunction : labelFunction;
dropDownList.tabFocusEnabled = false;
dropDownList.owner = this;
PopUpManager.addPopUp(IFlexDisplayObject(dropDownList), DisplayObject(this));
callLater(positionDropDownList);

Method in the autocomplete component (textInput is my text field)

public function positionDropDownList():void {
    var localPoint:Point = new Point(0, textInput.y);
    var globalPoint:Point = localToGlobal(localPoint);
    dropDownList.x = globalPoint.x;
    var fitsBelow:Boolean = parentApplication.height - globalPoint.y - textInput.height > dropDownList.height;
    var fitsAbove:Boolean = globalPoint.y > dropDownList.height;
    if (fitsBelow || !fitsAbove) {
        dropDownList.y = globalPoint.y + textInput.measuredHeight;
    } else {
        dropDownList.y = globalPoint.y - dropDownList.height;
    }
}

The position function was code that I borrowed from http://hillelcoren.com/flex-autocomplete/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文