3 Replies Latest reply on Aug 16, 2008 4:22 AM by amthekkel

    Binding to the results of a function

      I have an MXML component that has a DataGrid that's supposed to show a list of line items. Each line item will display an item name as well as other information. But the dataProvider of the DataGrid has an ArrayCollection of line item data, which has an itemID rather than an item name. I want to bind the DataGrid to this ArrayCollection, but I want to show the item name rather than the itemID. Furthermore, when the itemID changes, I want the item name to change. I can write a translation function that converts an item ID to an item name, but does anyone know how to put this all together and make it work?

        • 1. Re: Binding to the results of a function
          amthekkel Level 1
          From your explanation, I take it that the ArrayCollection being used as a dataprovider for the datagrid does not contain the ItemName. So, where are the corresponding ItemNames kept. Somewhere there should exist another collection that maps the ItemID with the ItemName.

          • 2. Re: Binding to the results of a function
            fourevermi Level 1
            Yes, as I mentioned, the ArrayCollection that the DataGrid is bound to has line items with an item ID but no item name. There is another ArrayCollection that has all the possible items in it--this data has both the item ID and item name. Of course I can't bind to that ArrayCollection, though, because that is a list of all possible items whereas my DataGrid has a list of line items (on a sales transaction, for example)--which is different data.
            • 3. Re: Binding to the results of a function
              amthekkel Level 1
              Hi christine.

              Not sure if you have sorted out your problem but this might help.
              i had a similar problem trying to display an item from an associated list.
              in your datagrid, when you define the datacolumn for Item_id use the Label function attribute as well. eg.

              <mx:DataGridColumn headerText="Item" dataField="item_ID" labelFunction="getItemName" />

              {note: there is no () in the labelFunction call, ie its getItemName and not getItemName. You dont specify any parameters here, it gets automatically passed.

              In your script section define the function

              public function getItemName(item:Object,column:DataGridColumn):String
              return ItemShiftIdMap[item.item_ID].itemName;

              In the above function, the two parameters are automatically passed on by the labelFunction call in the datagridColumn.
              ItemShiftIdMap is an object that functions as an associative array(hashes). this maps the item of its item.
              so the function is basically returning the itemName attribute of the ItemShiftIdmap array element which matches the item.item_ID. the item.Item_ID is the datagridcolumn value.

              hope that helps. let me know how you get on.