Go Back   CodingForums.com > :: Client side development > Flash & ActionScript > Adobe Flex

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-03-2008, 12:36 PM   PM User | #1
stinkytofu
New to the CF scene

 
Join Date: Sep 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
stinkytofu is an unknown quantity at this point
Flex DataGrid and percentage formating

I am having some difficulty rendering percentages in the DataGrid. I have the following JSON file structure:

{"n":"Zimbabwe",
"o":"63%",
"m":"38%",
"g":"1,900"
}

When I import these values into the DataGrid, there is a problem when I try to sort the percentage columns. The percentages are not sorted in the correct order, resulting in something like this:

0%
100%
14%
17%
etc..

Instead of:

0%
14%
17%
100%

So, I tried changing the JSON file to:

{"n":"Zimbabwe",
"o":"63",
"m":"38",
"g":"1900"
}

This took care of the sorting problem. However, now I am having problems getting Flex to display a percentage symbol next to the numbers in the DataGrid. Any idea what I need to do to get this working? What is the right approach to take when working with number formatting in the DataGrid? Should I be formatting the numbers inside the data source before feeding into Flex, or should I be storing the raw numbers in the JSON file and then formatting them with Flex?

Great if someone can point me in the right direction, because I really can't find any information about it anywhere on the Internet.

Many thanks!
stinkytofu is offline   Reply With Quote
Old 09-04-2008, 02:11 PM   PM User | #2
nikos101
Senior Coder

 
nikos101's Avatar
 
Join Date: Dec 2006
Location: London
Posts: 1,004
Thanks: 58
Thanked 10 Times in 10 Posts
nikos101 is an unknown quantity at this point
Cool

You need to use a custom sort function for example sorting dates:

Your column
Code:
 <mx:DataGridColumn dataField="dateAdded" labelFunction="cellDateFormatter"
                               headerText="Date of Comment"
                               sortCompareFunction="date_sortCompareFunc"
                               showDataTips="true"
                               dataTipFunction="date_dataTipFunc" 
                               />
Your function

Code:
private function date_sortCompareFunc(itemA:Object, itemB:Object):int {
                /* Date.parse() returns an int, but
                   ObjectUtil.dateCompare() expects two
                   Date objects, so convert String to
                   int to Date. */
                var dateA:Date = new Date(Date.parse(itemA.dateAdded));
                var dateB:Date = new Date(Date.parse(itemB.dateAdded));
                return ObjectUtil.dateCompare(dateA, dateB);
            }
In your case use regex to split up the percentage numbers
__________________

nikos101 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:06 AM.


Advertisement
Log in to turn off these ads.