有没有办法在 UI 构建时根据字段的值选择 MXML 组件?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会为此使用 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
我认为你必须在任何情况下使用动作脚本。 mxml 中似乎不存在条件语句。 尽管您可以包含这两个元素并使用内联来设置可见状态。
请参阅 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.
See http://www.firemoss.com/post.cfm/Powerful-MXML-Bindings-with-Ternary--Operators for more examples.
正确的(实际上也是唯一明智的)方法是使用普通的
for
循环和 ActionScript:您可以按照斜线建议的方式进行操作,只需在 Repeater 的每次迭代中添加这两个组件,基于某种测试来切换它们的显示(在这种情况下,我可能建议也包括
includeInLayout
属性),但这样做会导致显示列表膨胀,而且它不会无法扩展——最终,你还是只能在 ActionScript 中完成它。The right (and really only sensible) way to do it would be with a plain ol'
for
loop and ActionScript: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.