5 Replies Latest reply on Nov 19, 2010 4:59 PM by learningflex

    How to create a lookup table in flex

    learningflex

      Hi,

       

      I am not sure how to construct and use a lookup table in flex, i have a string which needs to replaced with a string in the lookup table. Can someone let me know can i can do this.

       

      Thanks

        • 1. Re: How to create a lookup table in flex
          rtalton Level 4

          Look into an Associative Array.

          • 2. Re: How to create a lookup table in flex
            UbuntuPenguin Level 4

            To expound on rtalton you could also use an Object {} , or a Dictionary.


            • 3. Re: How to create a lookup table in flex
              learningflex Level 1

              Thanks rtalton, can you give me an example. Let say i want something like if the webservice returned me Person , i should display prsn in the data grid instead of Person. I should create an associative array first right as below and then how do i replace each name in the datagrid with abbr?

               

              [Bindable]
              private var dataList:ArrayCollection = new ArrayCollection([
              {name:'Person',abbr:'prsn'},
              {name:'Student',abbr:'sdt'},
              {name:'Teacher, abbr:'tcr' },

              {name:Employee, abbr:'emp'}]);

               

              Person   prsn

              Student   sdt

              Teacher   tcr

              Employee emp

              • 4. Re: How to create a lookup table in flex
                BhaskerChari Level 4

                @learningflex,

                 

                Here is the simple example of doing this..

                 

                 

                <?xml version="1.0" encoding="utf-8"?>
                <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="onCreationComplete();">
                   <mx:Script>
                   <![CDATA[
                     import mx.collections.ArrayCollection;
                     import flash.utils.setInterval;
                     import mx.controls.Alert;
                     
                  [Bindable]
                  private var dataList:ArrayCollection = new ArrayCollection([
                  {name:'Person'},
                  {name:'Student'},
                  {name:'Teacher'}, 
                  {name:'Employee'}]);

                    private function onCreationComplete():void
                    {
                      for(var i:int=0;i<dataList.length;i++)
                      {
                       var obj:Object = dataList.getItemAt(i);
                       obj.abbr = getAbbrevatedName(obj.name);
                       dataList.setItemAt(obj,i);           
                      }
                      dataGrid.dataProvider = dataList;
                    }
                    private function getAbbrevatedName(name:String):String
                    {
                      switch(name)
                      {
                       case "Person":
                        return "prsn";
                       case "Student":
                        return "sdt";
                       case "Teacher":
                        return "tcr";
                       case "Employee":
                        return "emp";
                       default:
                        return "";
                      }
                    }
                   ]]>
                   </mx:Script>

                <mx:DataGrid id="dataGrid" x="10" y="177" visible="true">          
                  <mx:columns>
                   <mx:DataGridColumn  headerText="Actual Name" dataField="name"/>           
                   <mx:DataGridColumn width="150" headerText="Abbrevated Name" dataField="abbr"/>
                  </mx:columns>
                </mx:DataGrid>
                </mx:Application>

                 

                See I have displayed both the FullNames in one column and also the abbrevated names in another column. So you can make use of the second column.

                 

                 

                Thanks,

                Bhasker

                • 5. Re: How to create a lookup table in flex
                  learningflex Level 1

                  Thanks Bhasker,

                   

                  I was out of country and returned and tried your solution. It worked. Thanks for your reply