12 Replies Latest reply on Jan 16, 2010 10:23 AM by cheftimo

    My ComboBox will work if I hardcode the dataProvider, not if dataProvider from remote object

    cheftimo Level 2

      I have a ComboBox where the user can select a category of products to display. The ComboBox sits on a custom component and the ArrayCollection that populates it created in the same component that displays the list.

       

      I have two versions of the component containing the ComboBox.

       

      Version 1:

       

       

       

              // Create new variable tuped as ArrayCollection

              [Bindable]

              private var categoryList:ArrayCollection;

             

              // initData method invoked on creationComplete

              private function initData():void{

                  // put values in ArrayCollection

                  categoryList = new ArrayCollection([

                  "All Categories"

                        ,"Appetizers"

                  ,"Butcher Shop"

                  ,"Candy & Chocolate"

                  ,"Desserts"

                  ,"Fruit & Jams"

                  ,"Gifts"

                  ,"Grilling & Barbeque"

                  ,"Honey & Syrup"

                  ,"Kitchen Accessories"

                  ,"Little Darlins Childrens Fun Food"

                  ,"Oive & Other Oils"

                  ,"Oives & Olive Products"

                  ,"Pasta"

                  ,"Pate & Fish"

                  ,"Rice & Grains"

                  ,"Sauces"

                  ,"Seasonings And Condiments"

                  ,"Spreads"

                  ,"Truffles & Mushrooms"

                  ,"Vegetables"

                  ,"Vinegars"

                  ]);

                  // make this collection the data provider for catCombo

                  catCombo.dataProvider = categoryList;

              }

       

      Version 2 (result of a call to a ColdFusion cfc):

       

              // Create new variable tuped as ArrayCollection

              [Bindable]

              private var categoryList:ArrayCollection;

             

               private function categoryListHandler(event:ResultEvent):void{

                        categoryList = event.result as ArrayCollection;

                  var catObj:Object = new Object();

                  catObj.merchantCategory = "All Categories";

                  categoryList.addItemAt(catObj, 0);

                  catCombo.dataProvider = categoryList;

              }

       

      Everything else is identical in both versions of the custom component. The ComboBox is created like this:

       

          <mx:ComboBox id="catCombo" rowCount="16"

            styleName="glass"

            change="dispatchFilter();"

            labelField="merchantCategory"

            />

       

      In both cases, catCombo is populated correctly. But…

       

      In version 1, when I select a different category, it all works as expected.

       

      In version 2, when I change the category, the filter says "0 items selected" and nothing displays – even if I go back to "All Categories". It's like the filter sees nothing once there is a change in the catCombo selection.

       

      I would, of course, prefer Version 2 because the catalog - and its categories – changes month to month.

       

      So gurus, I would really like to start the new year with Version 2 working, so any ideas would be super appreciated. Anyone in the mood?

       

      Happy New Year,

       

      Carlos

        • 1. Re: My ComboBox will work if I hardcode the dataProvider, not if dataProvider from remote object
          cheftimo Level 2

          This thread was not eliciting replies, not even views, so I edited the "headline" (the subject) and this reply is meant to bump it.

           

          Anybody that wants to tackle this, please ask me for more information, if you need it.

           

          Prosperous New Year to all.

           

          Carlos

          • 2. Re: My ComboBox will work if I hardcode the dataProvider, not if dataProvider from remote object
            cheftimo Level 2

            Finding it hard to believe that no one in this forum knows anything about this.

             

            Bump!

            • 3. Re: My ComboBox will work if I hardcode the dataProvider, not if dataProvider from remote object
              archemedia Level 4

              A few issues:

              1. Is your result an Array or an ArrayCollection?

              2. Bind your categoryList to the dataProvider from startup

              3. Use the refresh() method of the ArrayCollection to trigger binding again.

              4. Why do you add 'All Categories' to the list? You could use the prompt property of a ComboBox to display a message when no item is selected!

               

              If this doesn't work, let me see how your ArrayCollection looks like when it's returned from the server.

               

              Does this help?

               

               

              [Bindable]

              private var categoryList:ArrayCollection = new ArrayCollection();

               

                     

               

                       private function categoryListHandler(event:ResultEvent):void

                       {

                             categoryList = event.result as ArrayCollection;

               

                      }

               

               

                  <mx:ComboBox id="catCombo" rowCount="16"

                   dataProvider = {categoryList}

                   prompt = "All Categories"

                    styleName="glass"

                    change="dispatchFilter();"

                    labelField="merchantCategory"

                    />

               

               

              • 4. Re: My ComboBox will work if I hardcode the dataProvider, not if dataProvider from remote object
                LaVengeance

                If I'm not wrong, what I think is happening is : the objects returned in your array collection are not defined. I mean there is nothing there to tell the

                compiler to look for an object which has 'merchantCategory' as one of it's members.

                 

                And when you do categoryList.addItemAt(catObj, 0); It could be replacing some data that is already at index 0 or it could be forming a new array with the previous contents of index 0 and the new contents.

                 

                What I suggest you do is to parse the returned array collection and form a new array collection and add well defined objects to it (a custom value object would be good programming).

                 

                 

                 

                [Bindable]
                        private var categoryList:ArrayCollection;

                       [Bindable]
                        private var finalList:ArrayCollection;


                         private function categoryListHandler(event:ResultEvent):void{
                            
                             finalList = new ArrayCollection();
                             var allCat:Object = new Object();
                             allCat.merchantCategory = "All Categories";
                             finalList.addItem(allCat);
                            
                             categoryList = event.result as ArrayCollection;
                                
                                 for each(var obj:Object in categoryList){
                                    
                                     var tempObj = new Object();
                                     tempObj.merchantCategory = obj.merchantCategory;
                                     finalList.addItem(tempObj);
                                    
                                 }
                                                 
                            catCombo.dataProvider = categoryList;
                         }
                     

                HTH,

                Cheers

                1 person found this helpful
                • 5. Re: My ComboBox will work if I hardcode the dataProvider, not if dataProvider from remote object
                  cheftimo Level 2

                  archemedia, here the prompt property of the ComboBox would not work: 'All categories' is not just a prompt to select something, it is one of the possible selections and affects how the catalog is filtered (or, in this case, NOT filtered). But I did not know about this prompt property, which helps with another problem I had recently and for which nobody came up with a solution.

                   

                  I read up on these issues over the week-end and already have plans to create a new categories class – a value object – that will hold not just category name and ID, but also the minimum and maximum prices of items in each category, data that I will have uses for later on.

                   

                  I think LaVengeance is pointing me in the right direction: the Flex debugger shows the categoryList items in filter as [object Object], which explains why the filter is not seeing anything after a change event in the ComboBox. For general information, I will report the results of whatever changes I make in the next couple of days.

                   

                  archemedia and LaVengeance thank you for providing great help. I shall return,

                   

                  Carlos

                  • 6. Re: My ComboBox will work if I hardcode the dataProvider, not if dataProvider from remote object
                    cheftimo Level 2

                    I am still having problems getting this ComboBox to work with the ArrayCollection as a dataProvider. To review, it works when I hard code the collection as shown in 'version 1' in the original post to this thread. To simplify things, I am no longer adding the "All Categories" object at the beginning of the collection.

                     

                    As can be seen in the attached debugger screen shot, the event listener is now generating a proper ArrayCollection of value objects. But the problem persists; I have done everything I can think of, but cannot get it to work.

                     

                    If anyone comes up with a solution, I will be extremely grateful. Any ideas?

                     

                    Best regards to all,

                     

                    Carlos

                    • 7. Re: My ComboBox will work if I hardcode the dataProvider, not if dataProvider from remote object
                      SpaghettiCoder Level 3

                      Try this, instead...

                       

                      [Bindable]

                      private var categoryList:ArrayCollection;

                       

                       

                      private function categoryListHandler(event:ResultEvent):void

                      {

                           categoryList = new ArrayCollection(event.result as Array);

                      }

                      • 8. Re: My ComboBox will work if I hardcode the dataProvider, not if dataProvider from remote object
                        SpaghettiCoder Level 3

                        I think I read the problem wrong, is the stuff not displaying in the combo-box?

                         

                        or

                         

                        Is it a problem with selecting something in the combo box not being passed into the filter?

                        • 9. Re: My ComboBox will work if I hardcode the dataProvider, not if dataProvider from remote object
                          cheftimo Level 2

                          Hi SpaghettiCoder:

                          I tried what you suggested, but doing it that way, the  ComboBox does not get populated. Here is what I do for the ArrayCollection to  come in as a collection of value objects, which is what I need:

                          private function categoryListHandler(event:ResultEvent):void{
                               var categs:ArrayCollection = event.result as ArrayCollection;
                               var tempList:ArrayCollection = new ArrayCollection;
                               for each (var obj:Object in categs){
                               var category:Category = new Category(Number(obj.catID),String(obj.catName),    Number(obj.catCount),Number(obj.minPrice),Number(obj.maxPrice));
                               tempList.addItem(category);
                               }
                               categoryList  = tempList;
                               }

                          As you can see in the attached image of the ComboBox,  this populates the CB BUT, the change event causes the filters to think 0 items  are selected. This does not happen when I hard code the dataProvider, as  explained in the original post; in that case, everything works as expected. So,  the answer to your second question is YES.

                          One thing I can't understand is: why is this seemingly  simple issue is such a mistery? So far, over 80 people have viewed this thread,  but as you see, very few want to touch it.

                           

                          Anyway, thank you for trying to help. I would appreciate  it if the ideas keep coming.

                           

                          Salud,

                           

                          Carlos

                          • 10. Re: My ComboBox will work if I hardcode the dataProvider, not if dataProvider from remote object
                            SpaghettiCoder Level 3

                            That code populates my combo box fine.

                             

                            I don't use a VO, or CFusion though...the thing with VO's I think is you can't do a direct selectedItem on it because it's like [object],[object] you actually have to extrapolate that info or make it .toString();

                             

                            Have you traced to see what is selected when you select something in the combobox? is it [object],[object]...

                             

                            I guess the cheapo way of doing it would be to just capture the combo-box selection into a variable.

                             

                            Then have the filter just filter from the variable which will have data,  rather than the filter directly from the combo box?

                             

                            It's hard enough to de-bug with all the code enough front of you, but when you are only provided only a snippet of the code it makes it even more difficult.

                            • 11. Re: My ComboBox will work if I hardcode the dataProvider, not if dataProvider from remote object
                              cheftimo Level 2

                              SpaghettiCoder:

                               

                              When passing the catName to the filter function, I have already  tried:

                              • catCombo.selectedItem as String and
                              • String(catCombo.selectedItem)
                              •  

                              In both cases, the app compiles, but I get either a  runtime error or a silent fail

                              .

                              But I think the solution lies in something like this. I will play with this a little more and try your "cheapo way" suggestion and report back on the results.

                               

                              Thank you very much for your help; I'll be back.

                               

                              Carlos

                              • 12. Re: My ComboBox will work if I hardcode the dataProvider, not if dataProvider from remote object
                                cheftimo Level 2

                                Issue resolved, thanks to SpaghettiCoder's suggestions.

                                 

                                The solution was to pass the catName to the filter function like so: {catCombo.selectedItem.toString()}.

                                 

                                The reason that was not working before was that in my Category class, I head defined the toString() method like this:

                                 

                                public function toString():String{
                                   return "Category:  " + catName;
                                }

                                 

                                I changed it to

                                 

                                public function toString():String{
                                   return catName;
                                }

                                 

                                and now everything is cool.

                                 

                                Thanks again to all those who helped with this issue, especially to SpaghettiCoder.

                                 

                                Salute,

                                 

                                Carlos