简体   繁体   English

Flex3 AdvancedDataGrid:如何在现有列的基础上添加新列?

[英]Flex3 AdvancedDataGrid : how to add a new column based on existing one?

I have a AdvancedDataGrid in flex3 (Flex 3) with 4 columns: 我在flex3(Flex 3)中有一个AdvancedDataGrid,其中有4列:

  • id : int id:int
  • category : String 类别:字符串
  • name : String 名称:字串
  • isPreferred : Boolean isPreferred:布尔值

And I would like to add a fifth column 我想添加第五列

  • favorite : Image 最喜欢的:图像

    The value of favorite will be based on the value of isPreferred : if true, then favorite will be a read-heart-icon, if false, a grey-heart-icon. 最爱的值将基于isPreferred的值:如果为true,则最爱将为read-heart-icon,如果为false,则为grey-heart-icon。
    Thanks for your help. 谢谢你的帮助。

Below is my code : 下面是我的代码:

  • the mxml content mxml内容

    <xml version="1.0"?> <xml版本=“ 1.0”?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" applicationComplete="init()"> <mx:Application xmlns:mx =“ http://www.adobe.com/2006/mxml” applicationComplete =“ init()”>
    <mx:Script> <mx:Script>
    <![CDATA[ <![CDATA [
    import mx.collections.ArrayCollection; 导入mx.collections.ArrayCollection;
    import com.test.Purchase; 进口com.test.Purchase;
    [Embed(source="..\\assets\\coeur_rouge.png")] [嵌入(source =“ .. \\ assets \\ coeur_rouge.png”)]
    public static const ICON_FAVORITE:Class; 公共静态const ICON_FAVORITE:Class;
    [Embed(source="..\\assets\\coeur_gris.png")] [嵌入(source =“ .. \\ assets \\ coeur_gris.png”)]]
    public static const ICON_NEUTRAL:Class; 公共静态const ICON_NEUTRAL:Class;
    [Bindable] [装订]
    public var myAC:ArrayCollection = new ArrayCollection(); 公共var myAC:ArrayCollection = new ArrayCollection();
    public function init() :void { 公共功能init():void {
    var aPurchase:Purchase=new Purchase(); var aPurchase:Purchase = new Purchase();
    var anotherPurchase:Purchase= new Purchase(); var anotherPurchase:Purchase = new Purchase();
    aPurchase.id=120; aPurchase.id = 120;
    aPurchase.category="category1"; aPurchase.category =“ category1”;
    aPurchase.name="advantage 2"; aPurchase.name =“ advantage 2”;
    aPurchase.isPreferred=true; aPurchase.isPreferred = true;
    myAC.addItem(aPurchase); myAC.addItem(aPurchase);
    anotherPurchase.id=220; anotherPurchase.id = 220;
    anotherPurchase.category="category2"; anotherPurchase.category =“ category2”;
    anotherPurchase.name="Nintendo DS"; anotherPurchase.name =“ Nintendo DS”;
    anotherPurchase.isPreferred=false; anotherPurchase.isPreferred = false;
    myAC.addItem(anotherPurchase);} myAC.addItem(anotherPurchase);}
    ]]> ]]>
    </mx:Script> </ mx:Script>
    <?mx:AdvancedDataGrid id="dg" width="500" height="150" dataProvider="{myAC}"> <?mx:AdvancedDataGrid id =“ dg” width =“ 500” height =“ 150” dataProvider =“ {myAC}”>
    <mx:groupedColumns> <mx:groupedColumns>
    <mx:AdvancedDataGridColumn dataField="id" headerText="ID" width="300"/> <mx:AdvancedDataGridColumn dataField="category" headerText="Category" width="400"/> <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 dataField =“名称” headerText =“名称” width =“ 900” />
    <mx:AdvancedDataGridColumn headerText="Fav?" <mx:AdvancedDataGridColumn headerText =“收藏夹?” dataField="isPreferred" width="700"/> dataField =“ isPreferred” width =“ 700” />
    </mx:groupedColumns> </ mx:groupedColumns>
    </mx:AdvancedDataGrid> </ mx:AdvancedDataGrid>
    </mx:Application> </ mx:Application>

    • the data object in action script public class Purchase { public function Purchase() { 动作脚本中的数据对象public class Purchase {public function Purchase(){

      } }

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

      public function get id():int { return _id; 公共功能get id():int {return _id; } }

      public function set id(pId:int):void { _id = pId; 公共功能集id(pId:int):void {_id = pId; } }

      public function get category():String { return _category; 公共函数get category():String {return _category; } }

      public function set category(pCategory:String):void { _category = pCategory; 公共功能集类别(pCategory:String):void {_category = pCategory;

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

      } }

      public function get name():String { return _productName; 公共函数get name():String {return _productName; } }

      public function set name(pName:String):void { _productName = pName; 公共功能集名称(pName:String):void {_productName = pName;

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

      } }

      public function get isPreferred() : Boolean { return _preferred; 公共函数get isPreferred():布尔值{return _preferred; } }

      public function set isPreferred(pPreferred:Boolean) :void { _preferred=pPreferred; 公共功能集isPreferred(pPreferred:Boolean):void {_preferred = pPreferred; } } }}

In order to do this you'll need an itemRenderer . 为此,您需要一个itemRenderer Something like this should work: 这样的事情应该起作用:

<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>

Please keep in mind that this piece of code is not reusable. 请记住,这段代码不可重用。 If you need to use columns that show images a lot I recommend implementing a custom ImageColumn that extends mx:AdvancedDataGridColumn , has some kind of imageFunction as property and uses a custom itemRenderer which would use the column's imageFunction to show the appropriate image. 如果您需要使用大量显示图像的列,我建议实现一个自定义ImageColumn ,它扩展了mx:AdvancedDataGridColumn ,具有某种imageFunction作为属性,并使用一个自定义itemRenderer ,它将使用列的imageFunction来显示适当的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM