Flex中通过扩展Label对DataGrid进行格式化显示的例子

By Minidxer | July 29, 2008

接下来的例子演示了Flex中如何通过扩展Label,对DataGrid进行格式化显示。例子中如果金额大小小于0,则红色显示,否则为黑色。注意标签是自定义的Label。

让我们先来看一下Demo可以右键View Source或点击这里察看源代码):


下面是完整实现代码(或点击这里察看):

Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- http://blog.flexexamples.com/2007/08/20/formatting-a-flex-datagrid-control-using-a-custom-item-renderer/ -->
  3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  4.         layout="vertical"
  5.         verticalAlign="middle"
  6.         backgroundColor="white">
  7.  
  8.     <mx:Script>
  9.         <![CDATA[
  10.             import mx.controls.dataGridClasses.DataGridColumn;
  11.             import mx.utils.ObjectUtil;
  12.  
  13.             private function price_labelFunc(item:Object, column:DataGridColumn):String {
  14.                 return currencyFormatter.format(item.@price);
  15.             }
  16.  
  17.             private function price_sortCompareFunc(itemA:Object, itemB:Object):int {
  18.                 return ObjectUtil.numericCompare(itemA.@price, itemB.@price);
  19.             }
  20.         ]]>
  21.     </mx:Script>
  22.  
  23.     <mx:XML id="itemsXML">
  24.         <items>
  25.             <item name="Item 1" price="1.32" />
  26.             <item name="Item 2" price="-12.23" />
  27.             <item name="Item 3" price="4.96" />
  28.             <item name="Item 4" price="-0.94" />
  29.         </items>
  30.     </mx:XML>
  31.  
  32.     <mx:Style>
  33.         .centered {
  34.             text-align: center;
  35.         }
  36.     </mx:Style>
  37.  
  38.     <mx:CurrencyFormatter id="currencyFormatter"
  39.             precision="2"
  40.             useNegativeSign="false" />
  41.  
  42.     <mx:DataGrid id="dataGrid" dataProvider="{itemsXML.item}">
  43.         <mx:columns>
  44.             <mx:DataGridColumn dataField="@name"
  45.                     headerText="Name:"
  46.                     headerStyleName="centered" />
  47.  
  48.             <mx:DataGridColumn dataField="@price"
  49.                     headerText="Price:"
  50.                     textAlign="right"
  51.                     headerStyleName="centered"
  52.                     labelFunction="price_labelFunc"
  53.                     sortCompareFunction="price_sortCompareFunc"
  54.                     itemRenderer="PriceLabel" />
  55.         </mx:columns>
  56.     </mx:DataGrid>
  57.  
  58. </mx:Application>
下面是自定义的Label扩展部分代码:
Download: PriceLabel.as
  1. package {
  2.     import mx.controls.Label;
  3.     import mx.controls.listClasses.*;
  4.  
  5.     public class PriceLabel extends Label {
  6.  
  7.         private const POSITIVE_COLOR:uint = 0x000000; // Black
  8.         private const NEGATIVE_COLOR:uint = 0xFF0000; // Red
  9.  
  10.         override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
  11.             super.updateDisplayList(unscaledWidth, unscaledHeight);
  12.  
  13.             /* Set the font color based on the item price. */
  14.             setStyle("color", (data.@price <= 0) ? NEGATIVE_COLOR : POSITIVE_COLOR);
  15.         }
  16.     }
  17. }
代码:Peter deHaan 翻译/整理/编译:minidxer

Topics: Flex | Tags: , , ,

Related Post

Leave a Comment

Name(*):

E-Mail(*) :

Website :

Comments :

Search Posts

Archives

Sponsored Ads