ricardo.poblete

Adobe Flex, ActionScript 3, etc…

Archive for June, 2008

Restrict characters into a TextInput

Posted by ripoblet on June 11, 2008

You must use restrict property if you don’t want the user put a special char, like spaces, ‘, <,>, etc.

If you want restrict the space you must use <mx:TextInput id=”txtInput” restrict=”^ ” />

If you want restrict single quote you must use <mx:TextInput id=”txtInput” restrict=”^’” />

If you want restrict double quote you must use <mx:TextInput id=”txtInput” restrict=”^\”" />

If you want restrict single and double quote you must use <mx:TextInput id=”txtInput” restrict=”^’\”" />

If you want restrict < or > you must use <mx:TextInput id=”txtInput” restrict=”^<>” />

 

Summary

If you want restrict some character you must use the restrict property and put into all characters that you don’t want user put into the text, but always beginnig with a ^ character.

 

Regards

 

ripoblet

 

Posted in Flex | Leave a Comment »

How to sort dates correctly with a datagrid column

Posted by ripoblet on June 11, 2008

When we have datagrid column showing is a date field, but when retrive from database sometimes formated “DD/MM/YYYY HH24:mi” (oracle format), the datagrid column can’t sort correct because it’s trying to sort strings alphabetically and strings dates are not words, are dates. (obviously).

Example Preconditions: Objects in dataprovider have a “DATE” property with a string date, example “11/06/2008 17:45″

 

This time the solution is

1) Transform strings format dates to date format dates

2) Create sort function

3) Set datagrid colum with the property “sortCompareFunction”

 Code

1) Transform function called String2Date

 private function String2Date(date:String):Date{

var day_month:Array = date.split(‘/’); //cut day, month and year with hour 

      var day:int = new Number(day_month[0]);

      var month:int = new Number(day_month[1]);

      var year_hour:String = day_month[2];

     

      var arr:Array = year_hour.split(‘ ‘); //cut year and hours with minutes separated by a space ” “

      var year:int = new Number(arr[0]);

      var hour_minutes:String = arr[1];

                   

      var arr2:Array = hour_minutes.split(‘:’); // cut hour and minutes seperated by “:”

      var hour:int = new Number(arr2[0]);

      var minutes:int = new Number(arr2[1]);

                 

      var date_date:Date = new Date(year, month -1, day, year, minutes, 0, 0);

             

      return date_date;

}

 

2) Compare function called “sortDates”

 

public function sortDates(itemA:Object, itemB:Object):int {

var dateA:Date = String2Date(itemA.FECHA);

      var dateB:Date = String2Date(itemB.FECHA);

      return ObjectUtil.dateCompare(dateA, dateB);

}

3) Finally set datagrid column, o suppose you have a defined column datagrid

 

<mx:DataGridColumn headerText=”Date” dataField=”DATE” sortCompareFunction=”sortDates/>

 

Now you can sort dates in a datagrid.

 

PD: If you have a simple date string format like “DD/MMM/YYYY” modify String2Date function.

END

 

Regards

 

ripoblet

 

Posted in Flex | Tagged: | 1 Comment »