Flex 组合框中为空
如何制作一个用户可以选择 null
的组合框?
如果您只是在数据提供程序中创建一个带有 null
的组合框,则会显示该值,但用户无法选择它:
<mx:ComboBox id="cb" dataProvider="{[null, 'foo', 'bar']}" />
有没有办法使该 null 可选择?
解决方法是向 dataProvider 添加一个不为 null 但“代表”null 的项目; 然后每次访问组合框时在 null 和该对象之间进行映射。 但这并不是一个真正优雅的解决方案。 您始终必须在访问“可为空”组合框的所有代码中牢记此映射...
编辑:扩展为什么我不喜欢解决方法: 当然,这可以在子类中完成,但是要么我引入新属性(例如 nullableSelectedItem
); 但您必须始终小心使用这些属性。 或者我重写 ComboBoxes selectedItem
; 但我担心这会破坏基类:它可能不喜欢某些东西改变其对当前所选项目的想法。 即使这个脆弱的黑客工作,在 selectedItem
和 dataProvider
之上,这个 nullItem 也需要在 data
和 listData 中进行特殊处理
对于渲染器,在 labelFunction
中,然后它可能仍然在 ComboBox 发送的事件中公开...... 它可能会起作用,但只是为了解决以下问题:如果用户单击该项目,它不会被激活(对于其余的,ComboBox 可以很好地处理 null ),这是一个相当大的黑客。 (另一种选择是将 ui 组件委托给 ComboBox,但这需要更多代码才能避免这个小问题)
How can you make a ComboBox where the user can select null
?
If you simply create a combobox with null
in the dataprovider, the value appears but the user cannot select it:
<mx:ComboBox id="cb" dataProvider="{[null, 'foo', 'bar']}" />
Is there a way to make that null selectable?
A workaround is to add an item into the dataProvider that is not null but 'represents' null; and then map between null and that object every time you access the combobox. But that's not really an elegant solution; you would always have to keep this mapping in mind in all code that accesses a 'nullable' combobox...
Edit: expanding on why I don't like the workaround:
It could be done in a subclass of course, but either I introduce new attributes (like nullableSelectedItem
); but then you have to be careful to always use these attributes. Or I override ComboBoxes selectedItem
; but I'm affraid that is going to break the base class: it might not like something changing its idea of what the current selected item is from within. And even this fragile hack works, on top of selectedItem
and dataProvider
this nullItem then also needs to be handled special in data
and listData
for renderers, in labelFunction
, and then it's probably still being exposed in events the ComboBox sends...
It might work, but it's quite a hack just to fix the problem that if the user clicks on the item it isn't activated (for the rest the ComboBox handles null fine).
(Another alternative is to have a ui component delegate to a ComboBox, but that's even much more code just to avoid this small problem)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
requireSelection="false" 将允许空白值,并且提示允许您输入用于该空白值的文本(如果您愿意)。
requireSelection="false" will allow a blank value and prompt allows you to enter the text use for that blank value if you'd like.
似乎正确管理空项目的唯一方法是将项目实际添加到组合框的数据提供程序中。 以下子类将自动处理此问题。
为了支持数据提供者的更改,在项目添加/删除以及数据提供者本身的完全重新分配方面,实现有点棘手(只需考虑 arraycollections 绑定到远程服务的响应)。
<代码>
package {
关于该类的一些注释:
它将自动在列表中插入空对象。这对我的场景来说是一个强烈的要求:数据提供程序由远程服务返回而且它们不能仅包含其他元素来支持 Flex UI,我也无法手动监视每个集合以在每次集合刷新时创建空项目。
由于它必须处理集合的内容,因此它将在内部创建具有相同项目实例和空实例的原始集合的副本,因此根本不会触及原始集合的实例,并且可以在其他上下文中重用。
它将侦听原始数据提供程序上的更改,允许对其进行处理,甚至分配一个全新的数据提供程序:将自动重新创建空项目。
您可以通过emptyItem属性控制提供用作“空项目”的实际实例:这允许与集合的其余部分保持一致的数据类型(如果您使用类型化对象),或者定义一个自定义标签。
使用它的一些示例代码...
> <代码>
It seems that the only way to correctly manage an empty item is to actually add an item to the dataprovider of the combobox. The following subclass will handle this automatically.
The implementation is a little tricky in order to support changes on dataprovider, in terms of items add/remove and of complete reassignment of the dataprovider itself (just think about arraycollections bindings to remote services' response).
package {
Some notes about the class:
It will insert the empty object automatically on the list.. this was a strong requirements for my scenario: the dataproviders was returned by remote services and they couldn't include additional elements just to support the Flex UI, neither I could manually watch every collection to create empty items on each collection refresh.
Since it has to work with the contents of the collection, it will internally create a copy of the original one with the same items' instances AND the empty one, so the instance of the original collection will not be touched at all and can be reused in other context.
It will listen for changes on the original dataprovider, allowing to work on it and even to assign a competely new one: the empty item will be recreated automatically.
You are in control providing the actual instance to use as the "empty item" with the emptyItem attribute: this allows to keep a consistent data type with the rest of the collection (if you use typed objects), or to define a custom label on it.
Some example code using it...
一个非常简单但也非常有限的解决方案是添加一个prompt=""属性。
这将阻止ComboBox自动选择dataProvider中的第一个项目,但一旦用户选择一个项目,空行将不会不再显示。
A really simple, but also very limited solution is to add a prompt="" attribute..
This will prevent the ComboBox from selecting the first item in the dataProvider automatically, but as soon as the user select an item, the empty row will not be displayed anymore.
以下解决方案可能是最简单的解决方案:
并使用 cb.selectedItem.data 访问数据。
但是,正如 Wouter 提到的,这个简单的解决方案并不适合绑定。
因此,这里有一个更棘手的解决方案,它将允许选择 null 对象:
其中 NullList 是以下类:
The following solution is likely the simpliest one:
and access the data using
cb.selectedItem.data
However, this simple solution is not binding-friendly, as Wouter mentioned.
So here is a more tricky solution that will allow selecting null objects:
Where NullList is the following class:
不幸的是,这是不可能的。
然而,一个不会让您“必须牢记此映射”的好解决方案是创建一个继承自 ComboBox 的类,并具有自己的 DataProvider 属性。
此属性设置器将处理空值并在超级 ComboBox 类上有一个表示形式。
Unfortunately, this is impossible.
How ever, a good solution that will not make you "have to keep this mapping in mind" is to create a class inherits from ComboBox with its own DataProvider property.
this property setter will handle the null values and have a representation for it on the super ComboBox class.