Monday, November 29, 2010

datagrid cell color

Hi

we can give color to datagrid cell based on the data using itemrenderers... here i am going to fill my color to datagrid cell in two ways. first is

by overriding the updateDisplayList method

override protected function updateDisplayList(unscaledWidth:Number,

unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth, unscaledHeight);
var g:Graphics= graphics;
if(data.Salary<=15000){ g.clear(); g.beginFill(0XFF0000); g.drawRect(0,0,unscaledWidth, unscaledHeight); } }


another

way is using label's opaqueBackground property...





<mx:datagridcolumn datafield="Name">

<mx:itemrenderer>

<mx:component>

<mx:label opaquebackground="{data.Salary <= 15000?'0xFF0000':null}"/>

</mx:component>

</mx:itemrenderer>

</mx:datagridcolumn>






output shows whos salary is low or eq 15000 in red color :



if we click on header of the datagrid to sort the column after savaral clicks ouput is like below
first datagrid is using updateDisplayList() method and second datagrid is using label opaqueBackground property...






so opaqueBackground property is simple and very effctive...



Happy Flexing....

Friday, November 26, 2010

get all values from array in Flex : with single line of code....

A tip to get all values in array in flex without writing a loop the following code will returns a string that contains array values with comma separation

var arr:Array= new Array("abc","xyz","$24","123");
var str:String = arr.join();
trace(str); // output : abc,xyz,$24,123

Happy flexing .....

Wednesday, November 24, 2010

String reverse in flex : single line of code

Hi,
A tip to reverse a String without looping.we don't have direct reverse() method in String class. so to reverse the String we need to split the string and reverse it and then join it. the code is below.

var str:String = "flexing";
var reStr:String = str.split("").reverse().join("");
trace(reStr); //Output : gnixelf

split the String with null delimiter, reverse it and join with null delimiter again..
Happy Flexing....

Friday, November 19, 2010

Security error : writing a file in Application Directory

Hi all,
A tip to write a file into Application directory in AIR
usauly we will get error if we try to write a file like this. Because application directory will be in readonly mode
myFileStream = new FileStream();
var myFile:File = File.applicationDirectory.resolvePath(path);
myFileStream.open(myFile, FileMode.WRITE);
myFileStream.writeUTFBytes(righe);
myFileStream.close();


Error:
SecurityError: fileWriteResource
at runtime::SecurityManager$/checkPrivilegeForCaller()
at flash.filesystem::FileStream/open()


instade of getting resolve path of the application directory and assigning to file object, simply provide application directory path as string .
code is like :
//get application directory path
var appDir:String = File.applicationDirectory.nativePath;
var inFile:File = File(appDir+"abc.txt");
myFileStream = new FileStream();
myFileStream.open(myFile, FileMode.WRITE);
myFileStream.writeUTF(righe);
myFileStream.close();


Happy Flexing..........

Monday, March 22, 2010

flex webservice example


http://www.adobe.com/2006/mxml" layout="absolute" xmlns="*"
creationComplete="getWeather()" >

import mx.controls.dataGridClasses.DataGridColumn;
import mx.rpc.events.ResultEvent;
import mx.managers.CursorManager;
import mx.controls.Alert;

default xml namespace = "http://www.webservicex.net"; //necessary to access the xml elements easily

[Bindable]private var _xmlResult:XML; //holds the result xml
[Bindable]private var _xlDayData:XMLList; //dataProvider for the day weather dataGrid
[Bindable]private var _sPlace:String;

/** invokes the web service operation to get the weather */
private function getWeather():void
{
CursorManager.setBusyCursor();
WS.GetWeatherByZipCode.send();
}

/** called by the WebService result event. Sets the dataProviders as necessary */
private function onResult(oEvent:ResultEvent):void
{
_xmlResult = XML(oEvent.result);
var xmlResultNode:XML = _xmlResult.GetWeatherByZipCodeResult[0];
var xmlDetailsNode:XML = xmlResultNode.Details[0];
outputInfo.text = _xmlResult.toXMLString();
_sPlace = xmlResultNode.PlaceName.text() + ", " + xmlResultNode.StateCode.text();
_xlDayData = xmlDetailsNode.WeatherData;
CursorManager.removeBusyCursor();
}//onResult

/** labelFunction for DataGrid. It seems that the namespace on the xml makes
* using the DataGridColumn dataField not work. At least I couldn't get it to work. */
private function lfDayData(oItem:Object, dgc:DataGridColumn):String
{
var sReturn:String = "";
var xmlItem:XML = XML(oItem); //get the item object cast as an xml object
var sHeaderText:String = dgc.headerText; //get the header text for this column
switch (sHeaderText) //logic to determine which node to get the data from
{
case "Day":
sReturn = xmlItem.Day.text();
break;
case "High":
sReturn = xmlItem.MaxTemperatureF.text();
break;
case "Low":
sReturn = xmlItem.MinTemperatureF.text();
break;
}

return sReturn;
}

]]>

http://www.webservicex.net/WeatherForecast.asmx?WSDL"
useProxy="false"
fault="Alert.show(event.fault.faultString), 'Error'"
result="onResult(event)" >



{zipcode.text}


paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10" id="myPanel">


text="Enter a zip code."/>