How can you create a combobox where the user can choose null?
If you just create a combobox with nullin the dataprovider, this value appears, but the user cannot select it:
<mx:ComboBox id="cb" dataProvider="{[null, 'foo', 'bar']}" />
Is there any way to make this choice null?
The workaround is to add an element to the dataProvider, which is not null but represents "null"; and then map between zero and this object every time you access dropdown lists. But this is not a very elegant solution; you always had to consider this mapping in all the code that refers to the "nullable" combobox ...
Edit: extension on why I don't like the workaround: Of course, this can be done in a subclass, but either I introduce new attributes (for example, nullableSelectedItem); but then you have to be careful to always use these attributes. Or I redefine ComboBoxes selectedItem; but I'm afraid this will break the base class: it might not like something that changes its idea of ββwhat is from the currently selected item from the inside. And even this fragile hacking works, on top of selectedItemand dataProviderthis nullItem also needs to be handled special in dataand listDatafor renderers inlabelFunctionand then itβs probably still affected by the event the ComboBox sends ... This may work, but it is enough to hack to fix the problem, if the user clicks on an element, it is not activated (for the rest, the ComboBox handles a zero penalty). (Another alternative is to have a ui component delegate for the ComboBox, but this is even much more code to avoid this small problem)
source
share