8 Replies Latest reply on Jul 31, 2009 11:35 PM by cghrmauritius

    Databinding FROM and TO ComboBox

    atanazyb
      I would like to bind ComboBox and model bidirectionally. Saving selected value to model works perfectly but I cannot automatically initiate selected item from model.

      Could you explain my following ActionScript code is working:
      ph_familyStatus.selectedItem = dictionaries.dictionary.(@name=='FamilyStatusDict').entry.(@data==mycontact.person.family Status.id);

      But mx:Binding has no effect:
      <mx:Binding destination="ph_familyStatus.selectedItem" source="dictionaries.dictionary.(@name=='FamilyStatusDict').entry.(@data==mycontact.perso n.familyStatus.id)"/>

      I have above 30 to show with initial values. Is there any possibility to automatically initiate ComboBoxes?
        • 1. Re:  Databinding FROM and TO ComboBox
          rtalton Level 4
          I think your XML, "dictionaries", cannot be used as a binding source.
          I found this in the help docs under "Defining a data model ":
          Note: You currently cannot use a node within an <mx:XML> data model as a binding source.
          • 2. Re:  Databinding FROM and TO ComboBox
            atanazyb Level 1
            I've tested bidirectional binding (mx:Binding) of XML model (E4X way) for text form controls and it worked for me. Only list such as ComboBox are not working. Also dictionary structure is successfully utilized as dataProvider in ComboBox.
            Is there any simple way to achieve goal I've presented?
            • 3. Re:  Databinding FROM and TO ComboBox
              ntsiii Level 3
              The docs are incorrect, XML is definitely bindable.

              The problem is that you cannot set selectedItem like that. You can only set selectedItem directly if the dataProvider items are primitive values like Number or string. XML nodes are complex object and will not work this way.

              To initialize a ComboBox at a particular value, you must loop over the dataProvider and compare the desired value to the appropriate item property value. When you find a match, use the loop index o set the ComboBox.selectedIndex.

              If you need to do this often, extend ComboBox. I have an example on www.cflex.net, and there are many other examples available.

              Tracy
              • 4. Re:  Databinding FROM and TO ComboBox
                atanazyb Level 1
                I cannot understand why the same expression (to remind dictionaries.dictionary.(@name=='FamilyStatusDict').entry.(@data==mycontact.person.familyS tatus.id)) is working in value assignement such as:
                quote:


                ph_familyStatus.selectedItem = dictionaries.dictionary.(@name=='FamilyStatusDict').entry.(@data==mycontact.person.family Status.id);


                but is not working in declarative binding.

                I'm new to Flex (one week) but this feature of language is really odd for me. Despite I'm not British but this behaviour breaks common sense in programming.

                Because binding to XML is working for simple values, as dataProvider and in ActionScript value assignment I'm treating this behaviour as a nasty BUG.
                • 5. Re:  Databinding FROM and TO ComboBox
                  ntsiii Level 3
                  Looking closer, I see that the way you are setting selecteditem will work, because the value you are assigning is a *reference* to the item in the DataProvider.

                  I would expect the declarative binding to work.

                  That is a fairly complex e4x expression, perhaps it uncovers a bug in the XML event dispatching functionality.

                  Have you figured out a workaround? I see some possibilities.

                  Tracy
                  • 6. Re:  Databinding FROM and TO ComboBox
                    atanazyb Level 1
                    I've lost too much hours on this issue. In prototype I'm working on I will simply initiate all combos on applicationComplete event (look at code in first post).

                    Deluxe version should replace declarative approach isn't working. I suppose that creating custom component based on original ComboBox and adding watchers or events to it and handling it using AS. Another way is to create custom AS library with such binding feature (based as above on events) and simplified bidirectional binding (another nasty lack).
                    And what you suggest?

                    And last question - can I somehow submit a bug with noticed behaviour?
                    • 7. Re:  Databinding FROM and TO ComboBox
                      ntsiii Level 3
                      flex Bug reports:
                      https://bugs.adobe.com/flex/

                      "...initiate all combos on applicationComplete..." Will that work for you? If so, then you do not want to use binding anyway. There is too much overhead for just a single initial assignment use.

                      If you DO need the Combos selectedItems to change in response to run-time data changes, then I would use bindable instance vars, or even a bindable value object. Bind to those, then set their values progrmatically as needed.

                      Tracy
                      • 8. Re:  Databinding FROM and TO ComboBox
                        cghrmauritius

                        Hi incase anyone is still looking for a clean solution to this problem here is a little class and useage i personally use..

                         

                        Create a DataUtil class

                         

                        public class DataUtils {

                         

                             [Bindable(event="dataChanged")]

                             public static function getBoundIndex( dataProvider:XMLList, attributeName:String, searchValue:String ):Number {

                         

                                  for ( var i:Number = 0; i < dataProvider.length(); i++ ){

                         

                                       var xml:XML = dataProvider[ i ];

                         

                                       if ( xml.attribute( attributeName ) == searchValue ){

                         

                                            return i;

                         

                                       }

                         

                                  }

                         

                                  return -1;

                             }

                         

                        }

                         

                         

                         

                        useage

                         

                        <mx:ComboBox

                        id = "admissionType0"

                        dataProvider = "{ model.medicationItems..stockItem }"

                        selectedIndex ="{ DataUtils.getBoundIndex( model.medicationItems..stockItem, 'id', model.selectedPrescription.@stockItemId )}"

                         

                        labelField = "@name"

                        prompt = "-Select-"

                        width = "100%"

                         

                        />

                         

                         

                        i hope this helps the usage is very simple.