Flex3 AdvancedDataGrid:如何基于现有列添加新列?

时间:2011-04-06 05:22:28

标签: flex advanceddatagrid

我在flex3(Flex 3)中有一个包含4列的AdvancedDataGrid:

  • id:int
  • category:String
  • name:String
  • isPreferred:Boolean

我想补充第五栏

  • 最爱:图片

    收藏夹的值将基于isPreferred的值:如果为true,则收藏夹将为读心形图标,如果为false,则为灰心图标。
    谢谢你的帮助。

以下是我的代码:

  • mxml内容

    < xml version =“1.0”?>
    < MX:应用  xmlns:mx =“http://www.adobe.com/2006/mxml”applicationComplete =“init()”>
        < MX:脚本>
      <![CDATA [
      import mx.collections.ArrayCollection;
      import com.test.Purchase;
      [嵌入(源= “.. \资产\ coeur_rouge.png”)]
      public static const ICON_FAVORITE:Class;
      [嵌入(源= “.. \资产\ coeur_gris.png”)]
      public static const ICON_NEUTRAL:Class;
      [绑定]
      public var myAC:ArrayCollection = new ArrayCollection();
      public function init():void {
    var aPurchase:Purchase = new Purchase();
    var anotherPurchase:Purchase = new Purchase();
    aPurchase.id = 120;
    aPurchase.category = “类别1”;
    aPurchase.name =“优势2”;
    aPurchase.isPreferred = TRUE;
    myAC.addItem(aPurchase);
    anotherPurchase.id = 220;
    anotherPurchase.category = “类别2”;
    anotherPurchase.name =“Nintendo DS”;
    anotherPurchase.isPreferred = FALSE;
        myAC.addItem(anotherPurchase);}
    ]>
          < / MX:脚本>
    <?mx:AdvancedDataGrid id =“dg”width =“500”height =“150”dataProvider =“{myAC}”>

          < MX:groupedColumns>
          < mx:AdvancedDataGridColumn dataField =“id”headerText =“ID”width =“300”/>       < mx:AdvancedDataGridColumn dataField =“category”headerText =“Category”width =“400”/>

          < mx:AdvancedDataGridColumn dataField =“name”headerText =“Name”width =“900”/>

          < mx:AdvancedDataGridColumn headerText =“Fav?” dataField =“isPreferred”width =“700”/>
          < / MX:groupedColumns>
          < / MX:AdvancedDataGrid>
        < / MX:应用程序>

    • 动作脚本中的数据对象 公共课购买 { 公共职能购买(){

      }

      private var _id:int = -1; private var _category:String = null; private var _productName:String = null;
      private var _preferred:Boolean = false;

      public function get id():int {     return _id; }

      public function set id(pId:int):void {     _id = pId; }

      public function get category():String {     return _category; }

      公共函数集类别(pCategory:String):void {     _category = pCategory;

      if ((_category == null) || (_category == "")) {               
          _category = "Default Category";
      }
      

      }

      public function get name():String {     return _productName; }

      公共函数集名称(pName:String):void {     _productName = pName;

      if ((_productName == null) || (_productName == "")) {
          _productName = "default product name";
          category = _productName;
       }
      

      }

      public function get isPreferred():Boolean {     return _preferred; }

      public function set isPreferred(pPreferred:Boolean):void {     _preferred = pPreferred; } }

1 个答案:

答案 0 :(得分:0)

为了做到这一点,你需要一个itemRenderer。这样的事情应该有效:

<mx:AdvancedDataGridColumn headerText="favorite">
    <mx:itemRenderer>
        <mx:Component>
            <mx:Image source="{data.isPreferred ? ICON_FAVORITE : ICON_NEUTRAL}">
                <mx:Script>
                    <![CDATA[
                        [Embed(source="..\assets\coeur_rouge.png")]
                        public static const ICON_FAVORITE:Class;

                        [Embed(source="..\assets\coeur_gris.png")]
                        public static const ICON_NEUTRAL:Class;
                    ]]>
                </mx:Script>
            </mx:Image>
        </mx:Component>
    </mx:itemRenderer>
</mx:AdvancedDataGridColumn>

请记住,这段代码不可重复使用。如果您需要使用大量显示图片的列,我建议您实施扩展ImageColumn的自定义mx:AdvancedDataGridColumn,将某种imageFunction作为属性并使用自定义itemRenderer将使用列的imageFunction来显示相应的图像。