apache-flex Flex,高级数据网格,自定义项目渲染器

icomxhvb  于 2022-11-01  发布在  Apache
关注(0)|答案(1)|浏览(184)

当涉及到flex itemrenderer时,我似乎遗漏了一些关键概念,特别是当它应用于AdvancedDataGrid时。我正在做很多其他人正在尝试做的事情:根据行中的数据更改字段的背景颜色。我的问题似乎是在访问数据字段方面。基本上,当加载时,什么也没有出现。如果我从渲染器中删除我试图访问字段数据的部分,它会工作,但有点违背目的。
这是我得到的信息

<mx:AdvancedDataGrid width="100%" height="100%" 
    id="topAccountsGrid" 
    borderStyle="solid" dropShadowEnabled="true" treeColumn="{list_name}" editable="false" selectionMode="singleRow"
    dragEnabled="true" dropEnabled="true" dragMoveEnabled="true" dragDrop="topAccountsGrid_dragDropHandler(event)"
    doubleClickEnabled="true" itemDoubleClick="topAccountsGrid_itemDoubleClickHandler(event)"
    sort="topAccountsGrid_sortHandler(event)" backgroundColor="#ffffff">
    <mx:dataProvider>
        <mx:HierarchicalData source="{filteredList}"
           childrenField="children" />
    </mx:dataProvider>
    <mx:columns>
       <mx:AdvancedDataGridColumn id="colRank" headerText="Rank" dataField="Rank__c" width="60">
            <mx:itemRenderer>
                <fx:Component>
                    <mx:HBox paddingLeft="2">
                       <s:Label id="tempLabel" text="{data.Rank__c}" />
                       <fx:Script>
                           <![CDATA[
                                override public function set data(value:Object) : void{
                                   super.data = value;
                                   if(data.Health__c == 0){ 
                                       setStyle("backgroundColor",0xFF5050);
                                       } else if(data.Health__c == 50){
                                            setStyle("backgroundColor",0xFFFF99);
                                       } else if(data.Health__c == 100){
                                            setStyle("backgroundColor",0x66FF66);
                                   }
                                }
                            ]]>
                        </fx:Script>
                    </mx:HBox>
                </fx:Component>
            </mx:itemRenderer>
        </mx:AdvancedDataGridColumn>
        <mx:AdvancedDataGridColumn id="list_name" headerText="Name" dataField="Name" />
        <mx:AdvancedDataGridColumn id="colPrevRank" headerText="Previous Rank" dataField="Previous_Rank__c" />
        <mx:AdvancedDataGridColumn id="colType" headerText="Type" dataField="Type" />
        <mx:AdvancedDataGridColumn id="colContacts" headerText="# Contacts" dataField="Contacts__c" />
        <mx:AdvancedDataGridColumn id="colDeals" headerText="# Deals" dataField="Deals__c" />
    </mx:columns> 
</mx:AdvancedDataGrid>
lf3rwulv

lf3rwulv1#

我在过去使用HierarchicalData时也遇到过类似的问题,我解决这个问题的方法是使用AdvancedDataGridRendererProvider
下面是一些示例代码:

<mx:AdvancedDataGrid width="100%" height="100%" 
    id="topAccountsGrid" 
    backgroundColor="#ffffff">
    <mx:dataProvider>
        <mx:HierarchicalData source="{filteredList}"
           childrenField="children" />
    </mx:dataProvider>
    <mx:columns>
       <mx:AdvancedDataGridColumn id="colRank" headerText="Rank" dataField="Rank__c" width="60" />
        <mx:AdvancedDataGridColumn id="list_name" headerText="Name" dataField="Name" />
        <mx:AdvancedDataGridColumn id="colPrevRank" headerText="Previous Rank" dataField="Previous_Rank__c" />
        <mx:AdvancedDataGridColumn id="colType" headerText="Type" dataField="Type" />
        <mx:AdvancedDataGridColumn id="colContacts" headerText="# Contacts" dataField="Contacts__c" />
        <mx:AdvancedDataGridColumn id="colDeals" headerText="# Deals" dataField="Deals__c" />
    </mx:columns>
    <mx:rendererProviders>
      <mx:AdvancedDataGridRendererProvider column="{colRank}" depth="1" dataField="Rank__c" renderer="AdvancedDataGridRankCRenderer" />
    </mx:rendererProviders>
</mx:AdvancedDataGrid>

和高级数据网格呈现器提供程序:

<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:fx="http://ns.adobe.com/mxml/2009" 
             xmlns:s="library://ns.adobe.com/flex/spark" 
             xmlns:mx="library://ns.adobe.com/flex/mx" 
             backgroundColor="{SetBackgroundColor(data)}" 
             paddingLeft="2" paddingRight="2" paddingTop="2"
             horizontalScrollPolicy="off"
             verticalScrollPolicy="off"
             height="22">
    <fx:Script>
        <![CDATA[
            [Bindable] private var bgColor:uint = 0xD6E5FF;

            private function SetBackgroundColor(obj:Object):uint
            {
                var returnColor:uint = 0xFF5050;

                if (obj["Rank__c"] != null)
                {
                    switch (obj["Rank__c"].toString().toUpperCase())
                    {
                        case "0":
                            returnColor = 0xFF5050;
                            break;
                        case "50":
                            returnColor = 0xFFFF99;
                            break;
                        case "100":
                            returnColor = 0x66FF66;
                            break;
                        default:
                            returnColor = 0xFF5050;
                                                    break;
                    }
                }

                return returnColor;
            }

            override public function set data(value:Object):void
            {
                super.data = value;
                rankLabel.text = value["Rank__c"].toString();               
                validateDisplayList();
            }
        ]]>
    </fx:Script>
    <mx:Label id="rankLabel" />
</mx:HBox>

相关问题