4 Replies Latest reply on Jun 14, 2009 2:26 PM by WhatDoesThatDo

    'Object Orientated' Array Collection Filter?

    WhatDoesThatDo

      Hi,

       

      Below is some code (which works) but it seems a bit of a kludge and I wanted to get other peoples ideas.

       

      What I'm trying to achive is seperation between products and pages (in a catalgoue) as a product can appear on more than one page and a page can have more than one product. A many-to-many situation so I'm using a third array to link the two ArrayCollections together. If you search for a products alt code, pages that contain the product are returned. I have no idea if this is the right way to do this so would love to get some feedback/ideas.

       

      Thanks!

       

       

      <?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/halo" minWidth="1024" minHeight="768" height="231" width="326">
          <fx:Script>
              <![CDATA[
                  import spark.events.TextOperationEvent;
                  import mx.events.ListEvent;
                  import mx.collections.ArrayCollection;
                  import mx.events.FlexEvent;
                 
                  [Bindable]
                  public var pages:ArrayCollection = new ArrayCollection([
                      {"name":"Page 1 - Widgets & Gizmos", "id":1},
                      {"name":"Page 2 - Gizmos Cont.", "id":2},
                      {"name":"Page 3 - Gadgets", "id":3}]);
                 
                  [Bindable]
                  public var products:ArrayCollection = new ArrayCollection([
                      {"alt":"WIDGET", "id":1},
                      {"alt":"GIZMO", "id":2},
                      {"alt":"GADGET", "id":3}]);
                     
                  public var page_products:Array = new Array(
                      {"prodid":1,"pageid":1},
                      {"prodid":2,"pageid":1},
                      {"prodid":2,"pageid":2},
                      {"prodid":3,"pageid":3});
                     
                  public function filter():void {
                      pages.filterFunction = filterFirst;
                      pages.refresh();
                  }

       

                  public function filterFirst(item:Object):Boolean
                  {
                      for(var i:int=0; i<products.length; i++) {
                          if(products[i].alt.search(new RegExp(search.text, 'i')) == 0) {
                              for(var x:int=0; x<page_products.length; x++) {
                                  if(page_products[x].prodid == products[i].id){
                                      for(var y:int=0; y<pages.length; y++){
                                          if(item.id == page_products[x].pageid)
                                          {
                                              return true;
                                          }
                                      }
                                  }
                              }
                          }
                      }
                      return false;
                  }

       

              ]]>
          </fx:Script>
         
          <mx:DataGrid dataProvider="{pages}" height="179" x="7" y="41" width="311">
              <mx:columns>
                  <mx:DataGridColumn dataField="id" headerText="ID" width="30"/>
                  <mx:DataGridColumn dataField="name" headerText="Page Title"/>
              </mx:columns>
          </mx:DataGrid>
          <s:TextInput  id="search" x="7" y="12" change="filter()" width="311"/>
         
      </s:Application>

        • 1. Re: 'Object Orientated' Array Collection Filter?
          leybniz Level 4

          Dig into Dictionary class, it may ease up your life a bit.

          • 2. Re: 'Object Orientated' Array Collection Filter?
            WhatDoesThatDo Level 1

            Thanks for that I will look in to the dictionary class. Could you provide an example which would work in this situation using the dictionary class? Thanks.

            • 3. Re: 'Object Orientated' Array Collection Filter?
              leybniz Level 4

              With the great pleasure

               

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

              <mx:Application

               

              xmlns:mx="

              http://www.adobe.com/2006/mxml"

              preinitialize="onInit()"

               

              >

               

               

              <mx:Script>

              <![CDATA[

               

               

              public var products:Dictionary = new Dictionary();

               

              public var pages:Dictionary = new Dictionary();

               

               

              private function onInit():void {

              pages[1] = {name:

               

              "Page 1 - Widgets & Gizmos", id:1};

              pages[2] = {name:

               

              "Page 2 - Gizmos Cont.", id:2};

              pages[3] = {name:

               

              "Page 3 - Gadgets", id:3};

               

              products[1] = {alt:

               

              "WIDGET", p:[pages[1]]};

              products[2] = {alt:

               

              "GIZMO", p:[pages[1],pages[2]]};

              products[3] = {alt:

               

              "GADGET", p:[pages[3]]};

               

              callLater(filter);

              }

               

               

               

              public function filter():void {

              grid.dataProvider = matchedPagesList;

              }

               

               

              public function get matchedPagesList():Array

              {

               

              for (var productKey:* in products)

              if (products[productKey].alt.search(new RegExp(search.text, 'i')) == 0)

              return products[productKey].p;

               

               

              return [];

              }

               

               

              ]]>

               

              </mx:Script>

               

               

              <mx:DataGrid id="grid" height="179" x="7" y="41" width="311">

               

              <mx:columns>

               

              <mx:DataGridColumn dataField="id" headerText="ID" width="30"/>

               

              <mx:DataGridColumn dataField="name" headerText="Page Title"/>

               

              </mx:columns>

               

              </mx:DataGrid>

               

               

              <mx:TextInput id="search" x="7" y="12" change="filter()" width="311" />

               

              </mx:Application>

               

               

               

               

               

              • 4. Re: 'Object Orientated' Array Collection Filter?
                WhatDoesThatDo Level 1

                Thanks very much for taking the time to provide an example to my problem. I have two questions though; One - when the application first starts only one page is displayed, is it possible to get all pages to be displayed initially? Two - the products are explicitly linked to pages i.e. The products know what pages they are on, is there anyway round this using your solution so the pages and products know nothing of each other? Again thank you for your help.