4 Replies Latest reply on Sep 5, 2009 9:55 AM by TheFlexGuy

    Datagrid in viewstack and filter function

    Marlene Level 1

      My prelaunch site is at http://67.199.72.153/yumyum.html. I am still learning flex. So if you click on RESTAURANTS BY CUISINE this makes the linkBar named "cuisineLinkBar" visible. The cuisineLinkBar is populated with a viewstack named subNavigation. The subnavigation is five vboxes with the first one being "All". I have a datagrid that is hooked up to a database and populated with an ArrayCollection and that is all working fine. When the user clicks on "American" for instance, I want to run a filterFunction on the data and filter for cuisine=American. I know how to run the filter function by using the following code

       

      public function cuisineFilter(filterName:Function):void {

                     dataAC.filterFunction=filterName;

                        //sortList();

                        dataAC.refresh();     

                  }

                  private function americanCuisineFilter(item:Object):Boolean {

                      return (item.restaurantCuisine=="american");

                  }

       

      I don't know where to put a click handler to trigger function cuisineFilter.

       

      I have enabled source view so hopefully someone has enough information to be able to help.

        • 1. Re: Datagrid in viewstack and filter function
          Gregory Lafrance Level 6

          I'm thinking you don't need a ViewStack and the one DataGrid per cuisine.

           

          I would think that you could just have one DataGrid, and then the LinkBar buttons click handler can call the cuisineFilter method.

           

          If this post answered your question or helped, please mark it as such.

          1 person found this helpful
          • 2. Re: Datagrid in viewstack and filter function
            TheFlexGuy Level 2

            Greg is correct, you could just have one Datagrid.  The click event handler for the link bar would call a function with a keyword based on what they clicked (ie. American) and set a variable to hold the cuisine to use in the filter function.

             

            In resultHandler, I'd add the line

            dataAC.filterFunction = myFilter;

             

            Then you could have something like this (a quick sample to give you the idea):

            <mx:Script>
                 <![CDATA[
                      import mx.events.ItemClickEvent;
                      
                      private var _cuisines:Array = new Array("All", "American", "Asian");
                      private var _choice:String;
                      
                      private function clickHandler(event:ItemClickEvent):void
                      {
                           _choice = event.label;
                           dataAC.refresh();
                      }
                      
                      private function myFilter(item:Object):Boolean
                      {
                           if(item.cuisine == _choice || _choice == "All")
                           {
                                return true;
                           }
                           return false;
                      }
                      
                      dataAC.filterFunction = myFilter;
                 ]]>
            </mx:Script>
            <mx:LinkBar dataProvider="{cuisines}" itemClick="clickHandler(event)"/>
            

            Did that answer your question?

            1 person found this helpful
            • 3. Re: Datagrid in viewstack and filter function
              Marlene Level 1

              OK. With you guys helping I got the filter function working but I do not understand how to get the first value in the linkbar array ("all" to show all the entries. I think I am looking for a way to say do not apply a filter function if the value of item.cuisine is "all". Please let me know if I should make this another question rather than taking on to this question. I updated the site and source files at http://67.199.72.153/yumyum.html

               

              Thank you.

              • 4. Re: Datagrid in viewstack and filter function
                TheFlexGuy Level 2

                Good point, I'd just set the inital value of _choice to "All", like this:

                private var _choice:String = "All";

                 

                That way, it starts off showing all, and starts changing as people click other cuisines.