4 Replies Latest reply on Jul 1, 2010 5:28 AM by rootsounds

    Radio Button to Filter Datagrid, buggy

    djh88ukwb Level 1

      Hello All.

       

      I have a datagrid being poplated by data form a mysql database,  the data is stored in an Array Collection.

       

      Here is my datagrid

       

                  <mx:DataGrid x="1" y="20" width="296" dataProvider="{YourCandidatesArr}" height="325" itemClick="itemClickHandler(event)">
                      <mx:columns>
                          <mx:DataGridColumn headerText="ID:" width="35" dataField="ref"/>
                          <mx:DataGridColumn headerText="Firstname" dataField="firstname"/>
                          <mx:DataGridColumn headerText="Surname" dataField="secondname"/>
                      </mx:columns>
                  </mx:DataGrid>

       

      Here are my radio buttons

       

              <s:RadioButton x="447" y="180" label="Live" groupName="radiogroup1" id="CB_live" change="GetYourCandidates_filterFunc(event)"/>
              <s:RadioButton x="447" y="206" label="Archieved" groupName="radiogroup1" id="CB_Archieved" change="GetYourCandidates_filterFunc(event)"/>
              <s:RadioButton x="447" y="232" label="All" groupName="radiogroup1" id="CB_All" change="GetYourCandidates_filterFunc(event)"/>

       

      And finally my script

       

                  protected function GetYourCandidates_filterFunc(item:Object):void
                  {
                      if (CB_live.selected )
                      {
                      output.text="a";
                      YourCandidatesArr.filterFunction = case_a_filterFunc;
                      YourCandidatesArr.refresh();
                      }
                      else if (CB_Archieved.selected)
                      {
                      output.text="b";
                      YourCandidatesArr.filterFunction = case_b_filterFunc;
                      YourCandidatesArr.refresh();
                      }
                      else if (CB_All.selected)
                      {
                      output.text="c";
                      YourCandidatesArr.filterFunction = case_c_filterFunc;
                      YourCandidatesArr.refresh();
                      }
                  }
                 
                  //Filters   
                  protected function case_a_filterFunc(item:Object):Boolean
                  {
                      if (item.locked == 'no')               
                      {
                          return true    
                      }
                      else
                          return false;
                  }
                  protected function case_b_filterFunc(item:Object):Boolean
                  {
                      if (item.locked == 'yes')               
                      {
                          return true    
                      }
                      else
                          return false;
                  }
                  protected function case_c_filterFunc(item:Object):Boolean
                  {
                      if (item.locked == 'yes')               
                      {
                          return true    
                      }
                      else if (item.locked =='no')
                      {
                          return true
                      }
                      else
                          return false;
                  }

       

      Now my problem is, the buttons do filter correctly when i click them first, however if i click thru them in a random order they dont work as they should.

        • 1. Re: Radio Button to Filter Datagrid, buggy
          David_F57 Level 5

          hi,

           

          try doing the code like below, it should sort your problem

           

           

          David

           

          ps: sorry about last night got back to Canberra really late and just sorta crashed for a few of hours 

          (600km of travel and an aggressive meeting in the same day has that affect ).

           

          <?xml version="1.0" encoding="utf-8"?>

          <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

             xmlns:s="library://ns.adobe.com/flex/spark"

             xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

          <fx:Script>

          <![CDATA[

          import mx.collections.ArrayCollection;

          import mx.events.ItemClickEvent;

           

          [Bindable] private var isLocked:String = "All";

          [Bindable] private var myArray:ArrayCollection = new ArrayCollection();

           

          protected function radiogroup1_itemClickHandler(event:ItemClickEvent):void

          {

          isLocked =event.label;

          myArray.filterFunction = lockFilter;

          myArray.refresh();

          }

           

          protected function lockFilter(item:Object):Boolean

          {

          switch(isLocked)

          {

          case "Live" : if (item.locked == 'no') return true else return false;

            break;

          case "Archive" : if (item.locked == 'yes') return true else return false;

            break;

          case "All" : if (item.locked == 'yes' || item.locked == "no") return true else return false;

            break;

          }

           

          }

           

          ]]>

          </fx:Script>

          <fx:Declarations>

          <s:RadioButtonGroup id="radiogroup1" itemClick="radiogroup1_itemClickHandler(event)"/>

          <!-- Place non-visual elements (e.g., services, value objects) here -->

          </fx:Declarations>

          <s:RadioButton x="175" y="100" label="Live" groupName="radiogroup1"/>

          <s:RadioButton x="175" y="126" label="Archive" groupName="radiogroup1"/>

          <s:RadioButton x="175" y="152" label="All" groupName="radiogroup1"/>

          </s:Application>

          • 2. Re: Radio Button to Filter Datagrid, buggy
            djh88ukwb Level 1

            Thanks for this

             

            ALthough i get an error on

             

                        protected function lockFilter(item:Object):Boolean
                        {
                            switch(isLocked)
                            {
                                case "Live" : if (item.locked == 'no') return true else return false;
                                    break;
                                case "Archive" : if (item.locked == 'yes') return true else return false;
                                    break;
                                case "All" : if (item.locked == 'yes' || item.locked == "no") return true else return false;
                                    break;
                            }
                        }

             

            The error is 1170 Function dose not return a value.

             

            ANy ideas why ?

            • 3. Re: Radio Button to Filter Datagrid, buggy
              rootsounds Level 4

              That's because it is possible to make it through the switch without returning a value. It needs a default case.

              1 person found this helpful
              • 4. Re: Radio Button to Filter Datagrid, buggy
                David_F57 Level 5

                hi,

                 

                You only have 3 states, Live Archive All, so unless the radiobutton label isn't matching one of those three you should always get a result, just add a default value to make the compiler happy.

                 

                 

                                    case "Live" : if (item.locked == 'no') return true else return false;
                                        break;
                                    case "Archive" : if (item.locked == 'yes') return true else return false;
                                        break;
                                    case "All" : if (item.locked == 'yes' || item.locked == "no") return true else return false;
                                        break;

                                    default : return false;

                1 person found this helpful