Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Senior Coder nikos101's Avatar
    Join Date
    Dec 2006
    Location
    London
    Posts
    1,008
    Thanks
    59
    Thanked 10 Times in 10 Posts

    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



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •