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)