Sorting datagrid columns numerically

Posted on Saturday 14 January 2006

The v2 Datagrid component features column sorting which can be triggered by clicking on a header. The trouble is that it only sorts columns alphabetically; if we have a column of numbers then 100, 20, 3000 is a valid ascending alphabetical order, not 20, 100, 3000 as you would expect. There is no simple way to do this unfortunately, even though it seems like a no-brainer.

The solution:

  1. Disable automatic sorting on a column
  2. Listen to the headerRelease event
  3. Sort manually on headerRelease

The code:


import mx.utils.Delegate;

//Sort column 1 numerically
dg.getColumnAt(1).sortOnHeaderRelease = false;
dg.addEventListener("headerRelease", Delegate.create(this, sortNumerically));

function sortNumerically(evtObj)
{
        var i = evtObj.columnIndex;
        var col = scores_dg.getColumnAt(i);
        if(i == 1)
        {
                //Sort this column numerically
                var dir = col.headerCell.asc ? Array.DESCENDING : 0;
                var dp = dg.dataProvider;
                if(dp instanceof mx.remoting.RecordSet)
                {
                        dp.sortItemsBy(col.columnName, null, dir | Array.NUMERIC );
                }
                else
                {
                        dp.sortItemsBy(col.columnName, dir | Array.NUMERIC );
                }
               
                col.headerCell.asc = !col.headerCell.asc;
        }
}
 

The rant:

I'll let Jesse fill in that section.


WordPress database error: [Can't open file: 'wp_comments.MYD'. (errno: 145)]
SELECT * FROM wp_comments WHERE comment_post_ID = '173' AND comment_approved = '1' ORDER BY comment_date

No comments have been added to this post yet.

Leave a comment




Your e-mail address is never displayed. If you run into issues with SpamKarma blocking you, email me at $patrick->5etdemi(com)


RSS feed for comments on this post | TrackBack URI