有没有办法在 UI 构建时根据字段的值选择 MXML 组件?

发布于 2024-07-28 11:52:00 字数 1066 浏览 5 评论 0原文

我有一个 Flex MXML UI,它使用 Repeater 组件构建一组单选按钮:

<mx:Repeater id="radios"
             dataProvider="{_lists.(@id == _question.single.@response_list).group.listItem}">
    <mx:RadioButton groupName="responses"
                    label="{radios.currentItem.@text}"
                    data="{radios.currentItem.@level}"/>
</mx:Repeater>

我想要做的是将组件放在转发器中 - 中的 RadioButton此示例 - 根据 radios.currentItem 的属性值进行选择:例如,如果 currentItem 的值为“foo”,我想要一个 Button 那里,或者如果它是“bar”,我想要一个 RadioButton。 是否可以在 MXML 组件中执行这种条件构造,或者我必须恢复到 ActionScript 才能执行此操作?

我正在考虑这样的事情:

<mx:Repeater id="r" dataProvider="{list}">
    <mx:If test="{r.currentItem.@type == 'radio'}">
        <mx:RadioButton label="{r.currentItem.@text}" />
    </mx:If>
    <mx:If test="{r.currentItem.@type == 'specify'}">
        <custom:Specify label="{r.currentItem.@text}" />
    </mx:If>
</mx:Repeater>

I have a flex MXML UI that is building a set of radio buttons using the Repeater component:

<mx:Repeater id="radios"
             dataProvider="{_lists.(@id == _question.single.@response_list).group.listItem}">
    <mx:RadioButton groupName="responses"
                    label="{radios.currentItem.@text}"
                    data="{radios.currentItem.@level}"/>
</mx:Repeater>

What I want to do is have the component within the repeater -- the RadioButton in this example -- be chosen based on the value of a property of radios.currentItem: If the value of currentItem is "foo", for example, I want a Button there, or if it's "bar" I want a RadioButton. Is it possible to perform this kind of conditional construction in an MXML component, or must I revert to ActionScript to do it?

I'm thinking of something along these lines:

<mx:Repeater id="r" dataProvider="{list}">
    <mx:If test="{r.currentItem.@type == 'radio'}">
        <mx:RadioButton label="{r.currentItem.@text}" />
    </mx:If>
    <mx:If test="{r.currentItem.@type == 'specify'}">
        <custom:Specify label="{r.currentItem.@text}" />
    </mx:If>
</mx:Repeater>

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

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

发布评论

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

评论(3

柠栀 2024-08-04 11:53:59

我会为此使用 AS3。 我的观点是,最好使用 mxml 进行显示,使用 AS3 进行逻辑...类似于 .Net 中的代码隐藏方式

I would use AS3 for this. My opinion is that its best to use mxml for the display and AS3 for the logic... similar to how in .Net you have the code-behind

泪意 2024-08-04 11:53:35

我认为你必须在任何情况下使用动作脚本。 mxml 中似乎不存在条件语句。 尽管您可以包含这两个元素并使用内联来设置可见状态。

<mx:Repeater id="r" dataProvider="{list}">
   <mx:RadioButton label="{r.currentItem.@text}" visible="{r.currentItem.@type == 'radio'}" />
   <custom:Specify label="{r.currentItem.@text}" visible="{r.currentItem.@type == 'specify'}" />
</mx:Repeater>

请参阅 http://www.firemoss.com/ post.cfm/Powerful-MXML-Bindings-with-Ternary--Operators 了解更多示例。

I think you have to use action script for any conditions. An conditional statement doesn't seem to exist in mxml. Although you could include both elements and use inline as to set the visible state.

<mx:Repeater id="r" dataProvider="{list}">
   <mx:RadioButton label="{r.currentItem.@text}" visible="{r.currentItem.@type == 'radio'}" />
   <custom:Specify label="{r.currentItem.@text}" visible="{r.currentItem.@type == 'specify'}" />
</mx:Repeater>

See http://www.firemoss.com/post.cfm/Powerful-MXML-Bindings-with-Ternary--Operators for more examples.

我喜欢麦丽素 2024-08-04 11:53:14

正确的(实际上也是唯一明智的)方法是使用普通的 for 循环和 ActionScript:

for each (var o:Object in yourDataProvider)
{
    if (o.someProperty)
    {
        var rb:RadioButton = new RadioButton();
        yourContainer.addChild(rb);
    }   
    else
    {
        var s:Specify = new Specify();
        yourContainer.addChild(s);
    }
}

您可以按照斜线建议的方式进行操作,只需在 Repeater 的每次迭代中添加这两个组件,基于某种测试来切换它们的显示(在这种情况下,我可能建议也包括 includeInLayout 属性),但这样做会导致显示列表膨胀,而且它不会无法扩展——最终,你还是只能在 ActionScript 中完成它。

The right (and really only sensible) way to do it would be with a plain ol' for loop and ActionScript:

for each (var o:Object in yourDataProvider)
{
    if (o.someProperty)
    {
        var rb:RadioButton = new RadioButton();
        yourContainer.addChild(rb);
    }   
    else
    {
        var s:Specify = new Specify();
        yourContainer.addChild(s);
    }
}

You could do as slashnick suggests, and just add both components with each iteration of the Repeater, toggling their display based on a test of some sort (in which case I'd probably suggest including the includeInLayout attribute as well), but you'd be bloating your display list by doing so, and it doesn't scale -- eventually, you just end up doing it in ActionScript anyway.

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