Monday, November 25, 2013

CustomRowColorDataGrid in flex 3


<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
  xmlns:s="library://ns.adobe.com/flex/spark"
  xmlns:mx="library://ns.adobe.com/flex/mx"
  minWidth="955"
  minHeight="600" xmlns:local="*"
   >
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
public var getPatList:ArrayCollection=new ArrayCollection([{id: "Select", value: "Select"}, {id: "Left", value: ""}, {id: 'Right', value: ''}]);


private function setSerialNo(obj:Object, column:int):String
{
var indx:int=getPatList.getItemIndex(obj) + 1;
return String(indx);
}
private function getRowBackgroundColor(item:Object, color:uint):uint{
if( item.value ){
return 0xDA81F5;
}
return color;

}
]]>
</fx:Script>
<local:CustomRowColorDataGrid id="contentAlertDataGrid"
  dataProvider="{getPatList}"
  width="100%"
  height="100%"
  rowHeight="27"
  verticalScrollPolicy="off"
  horizontalScrollPolicy="off"
  resizableColumns="true"
  rowColorFunction="getRowBackgroundColor">
<local:columns>
<mx:DataGridColumn headerText="SNo"
  width="80"
  labelFunction="setSerialNo"/>
<mx:DataGridColumn headerText="RegNo"
  width="80"
  dataField="regNo"/>
<mx:DataGridColumn headerText="DoctorNo"
  width="100"
  dataField="docNo"/>
</local:columns>
</local:CustomRowColorDataGrid>
</s:Application>


===================================================================

package  
{
import mx.controls.DataGrid;
import flash.display.Shape;
import mx.core.FlexShape;
import flash.display.Graphics;
import flash.display.Sprite;
import mx.rpc.events.AbstractEvent;
import mx.collections.ArrayCollection;
import flash.events.Event;
/**
* This is an extended version of the built-in Flex datagrid.
* This extended version has the correct override logic in it
* to draw the background color of the cells, based on the value of the
* data in the row.
**/
public class CustomRowColorDataGrid extends DataGrid
{
private var _rowColorFunction:Function;
public function CustomRowColorDataGrid()
{
super();
}
/**
* A user-defined function that will return the correct color of the
* row. Usually based on the row data.
*
* expected function signature:
* public function F(item:Object, defaultColor:uint):uint
**/
public function set rowColorFunction(f:Function):void
{
this._rowColorFunction = f;
}
public function get rowColorFunction():Function
{
return this._rowColorFunction;
}
// private var displayWidth:Number; // I wish this was protected, or internal so I didn't have to recalculate it myself.
// override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
// {
// super.updateDisplayList(unscaledWidth, unscaledHeight);
// if (displayWidth != unscaledWidth - viewMetrics.right - viewMetrics.left)
// {
// displayWidth = unscaledWidth - viewMetrics.right - viewMetrics.left;
// }
// }
//
/**
*  Draws a row background
*  at the position and height specified using the
*  color specified.  This implementation creates a Shape as a
*  child of the input Sprite and fills it with the appropriate color.
*  This method also uses the <code>backgroundAlpha</code> style property
*  setting to determine the transparency of the background color.
*
*  @param s A Sprite that will contain a display object
*  that contains the graphics for that row.
*
*  @param rowIndex The row's index in the set of displayed rows.  The
*  header does not count, the top most visible row has a row index of 0.
*  This is used to keep track of the objects used for drawing
*  backgrounds so a particular row can re-use the same display object
*  even though the index of the item that row is rendering has changed.
*
*  @param y The suggested y position for the background
*
*  @param height The suggested height for the indicator
*
*  @param color The suggested color for the indicator
*
*  @param dataIndex The index of the item for that row in the
*  data provider.  This can be used to color the 10th item differently
*  for example.
*/
override protected function drawRowBackground(s:Sprite, rowIndex:int,
 y:Number, height:Number, color:uint, dataIndex:int):void
{
if( this.rowColorFunction != null )
{
if( dataIndex < (this.dataProvider as ArrayCollection).length )
{
var item:Object = (this.dataProvider as ArrayCollection).getItemAt(dataIndex);
color = this.rowColorFunction.call(this, item, color);
}
}
super.drawRowBackground(s, rowIndex, y, height, color, dataIndex);
}
}
}

No comments:

Post a Comment