4 Replies Latest reply on Sep 5, 2009 3:26 PM by TheFlexGuy

    How do I display a Model's Attribute in another Model DataGrid?


      I have a DataGrid that displays usedVehicles.  Each used vehicle has a Style model associated to it.  I am able to bind the Style data to each Used Vehicle but dont know how to display a specific attribute in the UsedVehicle DataGrid.


      Here is the debug variables showing that I was able to bind the Style to each UsedVehicle:

      [0]     com.rapidcarsearch.Horizon.model.UsedVehicle (@3ac4bbf1)     
           active                    true     
           comment                    "See all our great deals at Saturn of Kearny Mesa - Conveniently located at the corner of Balboa and Convoy.  Most units come with a FREE 2 Year/100,000 MILE LIMITED WARRANTY,  With Prices starting as low as $3995, you wont find a better place to purchase your used car, truck, or SUV.  Call our FRIENDLY EXPERTS @ 619-507-0603"     
           enteredInventory       Date (@3ee05911)     
           equipment            "AM/FM Radio,CD Changer,Fog Lights,Metal Alloy Wheels,Air Conditioning,Cruise Control,Power Door Locks,Power Mirrors,Power Steering,Power Windows,Rear Window Defroster,Tilt Steering Column,Driver Front Airbag,Passenger Front Airbag,4-Wheel Disc Brakes,Daytime Running Lights,Tire Pressure Monitor,Cloth Seats,OnStar"     
           id                    54 [0x36]     
           internetPrice            0     
           mileage                    27 [0x1b]     
           ownerType            "Dealership"     
           price                    0     
           stock                    "381860"     
           style                    com.rapidcarsearch.Horizon.model.Style (@3edd1981)     
                                    baseInvoice     17180 [0x431c]     
                                    baseMsrp     18375 [0x47c7]     
                                    destination     620 [0x26c]     
                                    doors     2     
                                    gvrwRange     null     
                                    id     1     
                                    installedTechnicalEquipment     null     
                                    name     "3dr HB XR"     
                                    year     2008 [0x7d8]     
           transmission            ""     
           vin                    "W08AT271985094375"     


      In my view I have a dataGrid that displays all of the used vehicles, but when I run it, the Style Name is blank.


           <mx:DataGrid dataProvider="{model.usedVehicles}"
           width="100%" height="100%">
                     <mx:DataGridColumn dataField="id"               headerText="Key"/>
                     <mx:DataGridColumn dataField="style.name"           headerText="Style Name" />
                     <mx:DataGridColumn dataField="stock"               headerText="Stock Number"/>
                     <mx:DataGridColumn dataField="vin"               headerText="VIN"/>
                     <mx:DataGridColumn dataField="price"               headerText="Price"/>
                     <mx:DataGridColumn dataField="internetPrice"             headerText="Internet Price"/>
                     <mx:DataGridColumn dataField="transmission"          headerText="Transmission"/>


      What is the correct way to display the Style attributes in the dataGrid.

        • 1. Re: How do I display a Model's Attribute in another Model DataGrid?
          TheFlexGuy Level 2

          I'd say use a labelFunction for the DataGridColumn.  Your DataGridColumn would look like this:


          <mx:DataGridColumn labelFunction="styleLabelFunction" headerText="Style Name" />


          Then, you'd have this in your script block:


          private function styleLabelFunction(item:Object, column:DataGridColumn):String
               return item[column.dataField].style.name;




          -Marty [ http://www.theflexguy.com ]

          Helpful/Answered? Please mark it as such.

          1 person found this helpful
          • 2. Re: How do I display a Model's Attribute in another Model DataGrid?
            farrows76 Level 1

            Thanks for the reply... I wasn't able to get it working with the code that you provided... when I stuck the function call in the style DataGridColumn, it broke the app and didn't display any of the fields.  So I googled "LabelFunction Flex DataGrid" and found a similar solution where you use the LabelFunction to populate all of the fields:




            private function usedVehicleLabelFunction(item:Object, column:DataGridColumn):String
                    var sLabel:String = "";
                 var sHeaderText:String = column.headerText;
                 switch (sHeaderText) {
                      case "Key":
                      sLabel = item.id;
                      case "Style Name":
                      sLabel = item.style.name;
                      case "Stock Number":
                      sLabel = item.stock;
                      case "VIN":
                      sLabel = item.vin;
                      case "Price":
                      sLabel = item.price;
                      case "Internet Price":
                      sLabel = item.internetPrice;
                      case "Transmission":
                      sLabel = item.transmission;
                      return sLabel;
            <mx:DataGrid dataProvider="{model.usedVehicles}" labelFunction="usedVehicleLabelFunction"
                 width="100%" height="100%">
                           <mx:DataGridColumn headerText="Key"/>
                           <mx:DataGridColumn headerText="Style Name" />
                           <mx:DataGridColumn headerText="Stock Number"/>
                           <mx:DataGridColumn headerText="VIN"/>
                           <mx:DataGridColumn headerText="Price"/>
                           <mx:DataGridColumn headerText="Internet Price"/>
                           <mx:DataGridColumn headerText="Transmission"/>


            It worked for me setting it up this way.  But now my sorting doesn't work when I run the app.  It trys to sort once but the data gets jumbled and then the sort stops working. Im a step closer though.. hehehe


            Thanks again for your help.

            • 3. Re: How do I display a Model's Attribute in another Model DataGrid?
              farrows76 Level 1

              Hey Flex Guy... I was able to get your way to work too... I just had to remove :



              return item[column.dataField].style.name;

              To Make:


              return item.style.name;

              Doing it this way fixed the sorting in all of the columns except the style column...


              Thanks again!