13 Replies Latest reply on Jul 10, 2009 4:06 PM by MicheleBerg

    Optional ComboBox field?

    jonesf
      Hello,
      This problem has been bugging me for a long time. Surely, I'm overlooking something obvious.
      For non-required fields that use a combobox, how do select 'nothing' (null)? Like everywhere else on the web, I would expect that if the field is not required, that the top option in the list would be reserved for 'nothing'.
      Giving the combobox a prompt doesn't work either, because it doesn't let you go back and select the prompt after you've selected another value.
      I've hacked together a custom combobox to help me with this, but it's not pretty.
      Any suggestions? Thank You.
        • 1. Re: Optional ComboBox field?
          Peter Hahmann Level 1
          Just add an object to the first (0) positition on the ComboBox dataProvider.

          dp.addItemAt({id: 0 , item="None"},0)
          cb.selectedIndex = 0;

          Regards,
          peter
          • 2. Optional ComboBox field?
            jonesf Level 1
            Hi Peter,
            Thanks for the suggestion. I guess I should've been a little more clear. Since I usually bind the field directly to a property in a model object, I need the actual value of the 'None' item to be null. When I add something with a label field but null data field to my data provider (i'd also prefer not to manipulate my data provider directly), the combobox doesn't like that either.
            I guess I was hoping that I was just missing a property in the combobox that would just handle this for me. I'm really surprised this isn't considered a bigger issue in the community. A google search turns up nothing.
            • 3. Re: Optional ComboBox field?
              Peter Hahmann Level 1
              I'm kind a confused now. I couldn't understand what you need and why my solution wouldn't work. Do you mind posting the code so I could understand it?

              Regards,
              Peter
              • 4. Optional ComboBox field?
                jonesf Level 1
                Hi Peter,
                I can't think of how to show this in code, so I'll try explaining it better.

                I have an object, PurchaseOrder, with a 'customer' property. The user sometimes needs to fill in the the 'customer' property with a Customer object. So I get a list of customers from the server. This collection of Customer objects will be my data provider (dp) for the combobox (cb) for the user to select from.
                However, keep in mind that this field is optional. But when I assign my dp to the db, it automatically selects the first item in the dp. There is no option to select null (as in "no customer").
                I use databinding to bind the value (selectedItem) of this cb to the 'customer' field in my PurchaseOrder object. Using your method (as I understand it), my 'customer' property in my PurchaseOrder object would be assigned to object {id=1,item='None'}.. instead of null. Actually it would throw a type coercion error... but I wouldn't want Object{id=1,item='None'} anyway... I want null.

                Does that make sense?

                • 5. Optional ComboBox field?
                  Peter Hahmann Level 1
                  Yes! Now i got it. You can change your PurchaseOrder object. make the customer property private and create a getter/setter. In the setter method create a validation:

                  private var _customer:CustomerVo;

                  public function get custumer():CustomerVo{
                  return _customer;
                  }

                  public function set customer(customer:CustomerVo):void{
                  if(customer.id != 0){
                  _customer= customer;
                  }else{
                  _customer= null;
                  }

                  So you can create a "none" customer object and when it is selected the validation will take care of it.

                  Regards,
                  peter
                  • 6. Re: Optional ComboBox field?
                    jonesf Level 1
                    You are right, that would work.. and that's one option I don't think I thought of. The only things I don't like about it are that it requires customization of all fields that require this (I have a lot of them) and it requires manipulating the data provider each time one is loaded. I can't believe there's not just a simple "optional" flag we can set on the combobox... which is what I created in a customized version. I think you did confirm that no such thing exists in the out-of-the-box version which is what I wanted to know. I guess I'll just stick with custom combobox for now.
                    Thanks a bunch for your ideas!
                    • 7. Re: Optional ComboBox field?
                      Peter Hahmann Level 1
                      If you analyze the purpose of the combobox, it is meant to choose an option and "none" is an option. So if your "none" option should be treated as null, you will have to treat it yourself. If you lot at Ruby for instance, there are only getters and setters and in most of flex/as classes you'll find the same.
                      I understand your deception, but i think although "none" means null, when saving data null means "not set" and not "none set". Do you understand what i mean?

                      Regards,
                      Peter
                      • 8. Optional ComboBox field?
                        jonesf Level 1
                        I don't know...I like to think of myself as a pragmatist ;) To me it's very simple without getting into the true meaning of null or the original intent of something. I've worked at a lot of different places on a lot of different systems and if the technology didn't have this basic feature, I added it... every single time.
                        I rarely need to distinguish between 'nothing' and 'not set'. If I did need to, then the solution would be simple, add a 'nothing' item to my data provider! But the Flex combobox, just assumes I want to do that rather than giving me the option of the two. It doesn't allow the use to 'unset' something. It enforces a less common use case IMO.

                        I'll just boil it down this....
                        a) A combobox with an "optional=true" (or maybe notSetValue="{null}"). This would cover the vast majority of related use cases I've seen. This is not special to my project.

                        -OR-

                        b) Customizing all of my persistent fields wherever they may have an optional association to convert special objects to null and intercepting all of my dataprovider collections to add a special object to them before they're assigned to a combobox.


                        That said... obviously I'm in the minority since, as I said before, I cannot find any discussion on this. I thought everyone else was crazy... but I guess this makes me the crazy one ;)

                        • 9. Re: Optional ComboBox field?
                          Peter Hahmann Level 1
                          Hahahahha...

                          I understood your point and it makes sense. As you said just the minority needs this "none" option. Thanks god we are able to create custom components!!

                          Regards,
                          peter
                          • 10. Re: Optional ComboBox field?
                            Level 7

                            "jonesf" <webforumsuser@macromedia.com> wrote in message
                            news:gr06go$fqm$1@forums.macromedia.com...
                            > Hello,
                            > This problem has been bugging me for a long time. Surely, I'm overlooking
                            > something obvious.
                            > For non-required fields that use a combobox, how do select 'nothing'
                            > (null)?
                            > Like everywhere else on the web, I would expect that if the field is not
                            > required, that the top option in the list would be reserved for 'nothing'.
                            > Giving the combobox a prompt doesn't work either, because it doesn't let
                            > you
                            > go back and select the prompt after you've selected another value.
                            > I've hacked together a custom combobox to help me with this, but it's not
                            > pretty.
                            > Any suggestions? Thank You.

                            You might want to consider creating a custom IList similar to the approach
                            here:
                            http://blogs.adobe.com/aharui/2008/03/custom_arraycollections_adding.html


                            • 11. Re: Optional ComboBox field?
                              MicheleBerg

                              Just set the prompt property of the ComboBox to " ", as in prompt=" ".  Modifying data objects for purposes of affecting display is a Bad Idea, in my experience.

                              • 12. Re: Optional ComboBox field?
                                Ansury Level 3

                                I have a custom class that extends ComboBox where I handle this and some other annoying issues.  What you could do is override a few functions like set dataProvider().  I have a flag on the ComboBox to enable/disable automagically adding a "" blank string at the 0 index of the dataprovider (whenever the dataprovider is set).  As long as you aren't doing anything weird like saving these "reference data" lists back to the server at some point, this should work fine.  (Although, if you were doing that, you could just add a blank entry to the table storing that data and be done with it.)

                                 

                                I also override the getter for selectedItem() so that when super.selectedItem == "", it returns a null instead.

                                 

                                Can't remember if there's something else you need to account for or not (I'm changing more than one aspect of it's behavior) but I think for a simple case that should do it.  Now, if you're working with objects in your dataprovider because the values you want to display to the user and store in the database are different, that's a slightly more complex ball game, but it's also possible.

                                 

                                I forget why, but I don't think the prompt attribute was working out in my case, but if that works for your case obviously it's a better solution.

                                 

                                And there's nothing wrong with a custom ComboBox, it's not "hacked", AS3 has "extend" in it for a reason.  There are books that recommend doing exactly this kind of thing early on.

                                 

                                Edit: Just noticed the newly resurrected status of this thread. ah well.

                                • 13. Re: Optional ComboBox field?
                                  MicheleBerg Level 1

                                  I'm not saving the "reference data" lists back, but I am pulling information

                                  out of them to write user choices back to the database.  If I start altering

                                  data, it seems like asking for trouble.  Especially when you consider that

                                  the "prompt" property already exists, doesn't require a custom class, and

                                  poses no risk, it just seems like a no-brainer.

                                   

                                  I'm sure you have valid reasons for making the choices you have in your

                                  programs, and I don't mean to sound like I think they're hacks.  It just

                                  sounded like the person who submitted the question was looking for the

                                  simplest possible solution, and I was providing options.

                                   

                                  Michele