2011-10-07 71 views
3

我正在使用flex 4.5。我想創建Gauge組件Datagrid。Gauge或UI對象在Datagrid或AdvancedDataGrid中呈現在flex 4.5或flex 4中

我正在使用開源com.betterthantomorrow.components。我創建了這樣的自定義組件

<?xml version="1.0" encoding="utf-8"?> 
<s:BorderContainer xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/mx" 
        xmlns:bttc="com.betterthantomorrow.components.*" 
        xmlns:gauge="com.betterthantomorrow.components.gauge.*" 
        xmlns:objects="tekhnia.com.tekhniag.objects.*" 
        width="30%" height="65" backgroundColor="black" borderColor="black" 
        creationComplete="init(event)"> 
    <fx:Declarations> 
     <mx:NumberFormatter precision="1" id="formatter" rounding="nearest" /> 
    </fx:Declarations> 
    <fx:Script> 
     <![CDATA[ 
      import mx.events.FlexEvent; 
      import mx.messaging.channels.StreamingAMFChannel; 
      [Bindable] 
      public var cpuValue:Number; 
      [Bindable] 
      public var memoryValue:Number; 
      [Bindable] 
      public var diskValue:Number; 

      [Bindable] 
      public var mycomp:String; 
      [Bindable] 
      public var serverName:String; 

      [Bindable] 
      public var statusImage:String; 


      protected function init(event:FlexEvent):void 
      { 

       var strValues:String; 
       var strColors:String; 
       var strAlphas:String; 
       strColors="0x009900,0xFFFF00,0xDD0000"; 
       strValues="0,50,70,100"; 
       strAlphas=".8,.8,.8" 
       var values:Array=strValues.split(","); 
       var colors:Array=strColors.split(","); 
       var alphas:Array=strAlphas.split(","); 

       gauge1.setStyle("alertValues",values); 
       gauge1.setStyle("alertColors",colors); 
       gauge1.setStyle("alertAlphas",alphas); 

       gauge2.setStyle("alertValues",values); 
       gauge2.setStyle("alertColors",colors); 
       gauge2.setStyle("alertAlphas",alphas); 

       gauge.setStyle("alertValues",values); 
       gauge.setStyle("alertColors",colors); 
       gauge.setStyle("alertAlphas",alphas); 

       gauge.invalidateDisplayList(); 
       gauge1.invalidateDisplayList(); 
       gauge2.invalidateDisplayList(); 

      } 

     ]]> 
    </fx:Script> 
    <s:layout> 
     <s:HorizontalLayout/> 
    </s:layout> 
    <s:TileGroup width="101" paddingLeft="20" paddingRight="2"> 
     <bttc:Gauge id="gauge" 
        diameter="50" width="50" 
        verticalCenter="0" horizontalCenter="-111" 
        minValue="1" maxValue="10" value="{cpuValue}" valueFormatter="{formatter}" 
        bigTicks="9" smallTicks="45" showMinMax="false" showValue="false" pointerColor="white"/>   
    </s:TileGroup> 
    <s:TileGroup width="101" paddingLeft="20" paddingRight="2"> 
     <bttc:Gauge id="gauge1" 
        diameter="50" width="50" 
        verticalCenter="0" horizontalCenter="-111" 
        minValue="1" maxValue="10" value="{memoryValue}" valueFormatter="{formatter}" 
        bigTicks="9" smallTicks="45" showMinMax="false" showValue="false" pointerColor="white" automationName="T"/> 
    </s:TileGroup> 
    <s:TileGroup width="101" paddingLeft="20" paddingRight="2"> 
     <bttc:Gauge id="gauge2" 
        diameter="50" width="50" 
        verticalCenter="0" horizontalCenter="-111" 
        minValue="1" maxValue="10" value="{diskValue}" valueFormatter="{formatter}" 
        bigTicks="9" smallTicks="45" showMinMax="false" showValue="false" pointerColor="white"/> 
    </s:TileGroup> 
    <s:TileGroup width="40" paddingTop="3"> 
     <s:Image source="assets/led/big/{statusImage}" /> 
     <s:Label color="white" text="{serverName}" textAlign="center"/> 
    </s:TileGroup> 

</s:BorderContainer> 

我想在Datagrid中添加此組件。我在網上嘗試了很多。我沒有找到任何幫助。我也讀書。

請幫幫我。我在網站上的某處發現了一個班輪答案:編寫網格渲染器。我不知道如何編寫網格渲染器並將數據值傳遞給它。

我會更多的感謝滿員有人給我指向示例網格渲染器或代碼的指針。

回答

4

只是檢查這是否有幫助。您可以將它們放在單獨的色彩中,並提供適當的數據提供程序,而不是使用自定義組件將所有三個儀表放在一起。您也可以實現您想要的內容,但是您必須在組件中處理作爲數據提供者傳遞的數據。以下方法似乎更簡單。

<mx:DataGrid id="yourGrid" 
      height="388" width="663" 
      dataProvider="{yourDP}" 

      > 
    <mx:columns> 
     <mx:DataGridColumn headerText="Type" width="80"> 
      <mx:itemRenderer> 
       <mx:Component> 
        <bttc:Gauge id="gauge1" diameter="50" width="50" verticalCenter="0" horizontalCenter="-111" minValue="1" maxValue="10" value="{cpuValue}" valueFormatter="{formatter}" 
           bigTicks="9" smallTicks="45" showMinMax="false" showValue="false" 
           pointerColor="white" automationName="T"/> 
       </mx:Component> 
      </mx:itemRenderer> 
     </mx:DataGridColumn> 

     <mx:DataGridColumn headerText="Type" width="80"> 
      <mx:itemRenderer> 
       <mx:Component> 
        <bttc:Gauge id="gauge2" diameter="50" width="50" verticalCenter="0" horizontalCenter="-111" minValue="1" maxValue="10" value="{memoryValue}" valueFormatter="{formatter}" 
           bigTicks="9" smallTicks="45" showMinMax="false" showValue="false" 
           pointerColor="white" automationName="T"/> 
       </mx:Component> 
      </mx:itemRenderer> 
     </mx:DataGridColumn> 

     <mx:DataGridColumn headerText="Type" width="80"> 
      <mx:itemRenderer> 
       <mx:Component> 
        <bttc:Gauge id="gauge3" diameter="50" width="50" verticalCenter="0" horizontalCenter="-111" minValue="1" maxValue="10" value="{diskValue}" valueFormatter="{formatter}" 
           bigTicks="9" smallTicks="45" showMinMax="false" showValue="false" 
           pointerColor="white" automationName="T"/> 
       </mx:Component> 
      </mx:itemRenderer> 
     </mx:DataGridColumn> 

     <mx:DataGridColumn headerText="Type" width="80"> 
      <mx:itemRenderer> 
       <mx:Component> 
        <s:Image source="assets/led/big/{statusImage}" /> 
        <s:Label color="white" text="{serverName}" textAlign="center"/> 
       </mx:Component> 
      </mx:itemRenderer> 
     </mx:DataGridColumn> 


    </mx:columns> 
</mx:DataGrid> 

另一種方式是通過你的整個組件作爲項目渲染採取線索從上面:

<mx:DataGrid id="yourGrid" 
      height="388" width="663" 
      dataProvider="{yourDP}" 

      > 
    <mx:columns> 
     <mx:DataGridColumn headerText="Type" width="80"> 
      <mx:itemRenderer> 
       <mx:Component> 
        <someNameSpace:YourComponent cpuvalue={cpuValue} diskValue={diskValue}/> 
       </mx:Component> 
      </mx:itemRenderer> 
     </mx:DataGridColumn> 



    </mx:columns> 
</mx:DataGrid> 

還有一點,使這裏如果被分配到DataGrid的數據提供程序具有的所有值那麼你的組件裏,你可以像data.variableName訪問它們:

<s:TileGroup width="101" paddingLeft="20" paddingRight="2"> 
    <bttc:Gauge id="gauge" 
       diameter="50" width="50" 
       verticalCenter="0" horizontalCenter="-111" 
       minValue="1" maxValue="10" value="{data.cpuValue}" valueFormatter="{formatter}" 
       bigTicks="9" smallTicks="45" showMinMax="false" showValue="false" pointerColor="white"/>   
</s:TileGroup> 
<s:TileGroup width="101" paddingLeft="20" paddingRight="2"> 
    <bttc:Gauge id="gauge1" 
       diameter="50" width="50" 
       verticalCenter="0" horizontalCenter="-111" 
       minValue="1" maxValue="10" value="{data.memoryValue}" valueFormatter="{formatter}" 
       bigTicks="9" smallTicks="45" showMinMax="false" showValue="false" pointerColor="white" automationName="T"/> 
</s:TileGroup> 
<s:TileGroup width="101" paddingLeft="20" paddingRight="2"> 
    <bttc:Gauge id="gauge2" 
       diameter="50" width="50" 
       verticalCenter="0" horizontalCenter="-111" 
       minValue="1" maxValue="10" value="{data.diskValue}" valueFormatter="{formatter}" 
       bigTicks="9" smallTicks="45" showMinMax="false" showValue="false" pointerColor="white"/> 
</s:TileGroup> 
<s:TileGroup width="40" paddingTop="3"> 
    <s:Image source="assets/led/big/{statusImage}" /> 
    <s:Label color="white" text="{data.serverName}" textAlign="center"/> 
</s:TileGroup> 

在這種情況下,你可以在你的組件通過作爲項目渲染如下:

<mx:DataGrid dataProvider="{yourDP}" > 
     <mx:columns> 
      <mx:DataGridColumn itemRenderer="com.somePath.yourComponent"/> 
     </mx:columns> 
</mx:DataGrid> 

希望這有些幫助。