Sorting date columns in a DataGrid | Flex Examples

2 Comments

Sorting date columns in a DataGrid

by Peter deHaan on August 12, 2007

in DataGrid, ObjectUtil

Here’s an example of sorting a column of dates in a Flex DataGrid. The dates start out as Strings (such as “04/14/1980″) so you create a custom sortCompareFunction on that DataGrid column which converts the strings to dates so Flex will sort the dates in sequential order (as oppsed to string order). Hope that helps somebody out there.

I also created a little tooltip on the date column which shows the dates in a somewhat more readable form (”April 14 1980″) using the DataGridColumn object’s showDataTips and dataTipFunction properties.

Full code after the jump.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.utils.ObjectUtil;

            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.dob));
                var dateB:Date = new Date(Date.parse(itemB.dob));
                return ObjectUtil.dateCompare(dateA, dateB);
            }

            private function date_dataTipFunc(item:Object):String {
                return dateFormatter.format(item.dob);
            }
        ]]>
    </mx:Script>

    <mx:ArrayCollection id="arrColl">
        <mx:source>
            <mx:Array>
                <mx:Object name="User A" dob="04/14/1980" />
                <mx:Object name="User B" dob="01/02/1975" />
                <mx:Object name="User C" dob="12/30/1977" />
                <mx:Object name="User D" dob="10/27/1968" />
            </mx:Array>
        </mx:source>
    </mx:ArrayCollection>

    <mx:DateFormatter id="dateFormatter" formatString="MMMM D, YYYY" />

    <mx:DataGrid id="dataGrid" dataProvider="{arrColl}">
        <mx:columns>
            <mx:DataGridColumn dataField="name"
                    headerText="Name:" />

            <mx:DataGridColumn dataField="dob"
                    headerText="Date of birth:"
                    sortCompareFunction="date_sortCompareFunc"
                    showDataTips="true"
                    dataTipFunction="date_dataTipFunc" />
        </mx:columns>
    </mx:DataGrid>

</mx:Application>

Stored as a note on how to create a custom compare function in Flex

Displaying the sort arrow in a Flex DataGrid control without having to click a column | Flex Examples

Leave a comment

Displaying the sort arrow in a Flex DataGrid control without having to click a column

by Peter deHaan on February 28, 2008

in DataGrid, Sort, SortField

I’ve seen this question come up a few times recently in various forums/lists and even in my blog comments (see “Changing the default sort arrow skin on a Flex DataGrid control”).

The following example shows how you can display the sort arrow in a DataGrid control in Flex without having the user click on a column header in the DataGrid control.

Full code after the jump.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application name="DataGrid_dataProvider_sort_fields_test">
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.collections.Sort;
            import mx.collections.SortField;

            private function init():void {
                arrColl.sort = new Sort();
                arrColl.sort.fields = [new SortField("idx", false, true)];
                arrColl.refresh();
            }
        ]]>
    </mx:Script>

    <mx:ArrayCollection id="arrColl">
        <mx:source>
            <mx:Array>
                <mx:Object idx="1" c1="One.1" c2="One.2" />
                <mx:Object idx="2" c1="Two.1" c2="Two.2" />
                <mx:Object idx="3" c1="Three.1" c2="Three.2" />
                <mx:Object idx="4" c1="Four.1" c2="Four.2" />
                <mx:Object idx="5" c1="Five.1" c2="Five.2" />
                <mx:Object idx="6" c1="Six.1" c2="Six.2" />
                <mx:Object idx="7" c1="Seven.1" c2="Seven.2" />
                <mx:Object idx="8" c1="Eight.1" c2="Eight.2" />
                <mx:Object idx="9" c1="Nine.1" c2="Nine.2" />
                <mx:Object idx="10" c1="Ten.1" c2="Ten.2" />
                <mx:Object idx="11" c1="Eleven.1" c2="Eleven.2" />
                <mx:Object idx="12" c1="Twelve.1" c2="Twelve.2" />
                <mx:Object idx="13" c1="Thirteen.1" c2="Thirteen.2" />
            </mx:Array>
        </mx:source>
    </mx:ArrayCollection>

    <mx:DataGrid id="dataGrid"
            dataProvider="{arrColl}"
            creationComplete="init();">
        <mx:columns>
            <mx:DataGridColumn dataField="idx" />
            <mx:DataGridColumn dataField="c1" />
            <mx:DataGridColumn dataField="c2" />
        </mx:columns>
    </mx:DataGrid>

</mx:Application>

View source is enabled in the following example.

A big thanks to Rob for the heads up with the solution!

For more information, see http://bugs.adobe.com/jira/browse/SDK-14663.

–>

Good tutorial for Flex development. Shows how to apply a client side sort which in turn will affect how the data grid’s array is displayed.

Episode with Subversive AssertionFailedException

Leave a comment

I have been using Subversive as my main SVN plug-in for my instances of Eclipse both at home and at work since it is now part of the Eclipse foundation. It woks pretty much like SubEclipse which is the SVN plug-in I used since I started using Eclipse for my Flex and ColdFusion development.

I had a new unpack of Eclipse 3.4.2 Ganymede (Eclipse is not installed, just unpacked from a zip archive in case you did not know) and I needed to use SVN to check out some code. As usual I added the repository for the Subversive plug-in, clicked install and let it rip. It installed with no problem which it should and as with all plug-in installs for Eclipse, it requires an application restart. Everything is happening by the numbers thus far.

After Eclipse restarted, I proceeded to checkout an SVN project like you normally would, some of you who are attentive might have noticed something by now. Anyway I pasted the URL link into the repository URL and guess what? An Error got thrown. To those of you who saw this coming, kudos to you.

As all who had used Subversive before, the plug-in and SVN connectors are in two different repositories. From what I understand this is due to licensing issues but that is a story for another time. The point is that after you install the plug-in, you are supposed to install the connectors which I did not do.

So I thought no biggie, I will just have to install the connectors. Then my episode of getting AssetionFailedException errors kept on happening when I tried to install the connectors. So tried a different connector repository and still no dice. I was like what in the name god happened? Then I tried doing an update for all my plug-ins which is no small undertaking considering the fact that my friend here blew my home bandwidth cap and after an agonizing 11 minutes, same error came up.

At this point I felt that maybe I should just unpack a fresh Eclipse and start over but then I tought could this be a plug-in issue because Subversive ran without its dependent plug-in which perhaps caused some corruption? I closed Eclipse and fired my command prompt and did this :

c:\> cd eclipse

c:\eclipse> eclipse –clean

The clean argument passed onto Eclipse as you probably know is something used to basically start Eclipse and re-links all the plug-ins together and flushes everything save for your user preferences, at least thats what I think it does. I hoped this would flush out the corruption in my plug-ins wherever they might be.

After long waits and loads of resource consumption (note to self: running aTunes, Pidgin, Chrome and Windows Update is a bad idea when running eclipse -clean), I tried again to install my SVN connectors. Now it worked like its supposed to and then I decided to blog about this. Hope this helps somebody else who had the same issue as I did.

Useful Links :

http://www.polarion.com/products/svn/subversive/download.php

http://www.eclipse.org/subversive/

Twitter Lists

Leave a comment

One of the reasons I use Brizzly was because I was able to manage my Twitter streams. Now it is on Twitter itself as well. It is called Twitter lists whereby you can create categories than can be followed by other people as well or in the words of TWiT’s Leo Laporte, curate your tweets and influence. Unlike Brizzly, it allows more than 5 groups which is a big plus for me.

Does this mean I will drop Brizzly and go Twitter all the way? Aside from the fact that there is unlimited lists and it loads much faster on my sorry excuse of a net connection, Brizzly’s interface is just much better so I guess there is still use Brizzly as my primary Twitter application.

Perhaps Twitter should just buy over Brizzly and integrate their UI as the default front end?

N97 Lifer Review

1 Comment

For those of you who read Top Gear magazine, the lifer reviews are not people who review things after a few hours or days with the product. They write their review after a significant amount of time with it which is usually months or close to a year. As you probably have figured out by now, this blog entry is my review of the N97 after 3 months with the phone.

 nokia-n97-black-running-skype

I must say, when I first got the phone I was very exited with it as it is actually my first premium quality handset. It looked great and felt great, the hardware that is. When I started it up, its interface was what I liked, polished, simple and FAR better than my Sony Ericsson P1i. It would have been nice to have the crazy transitions found in the Android based HTC Hero, Palm Pre or the vaunted Apple iPhone but for me that was not a requirement. While I do not mind the overall look of the interface as much as other people online who just can’t stop slamming it, I do have some gripes regarding the interface.

For one thing it is not as smooth and intuitive as the aforementioned phones. While it gets the job done, I do get the feeling that it could use an update. The interface feels very long in the tooth and they are times where I felt that things very buried too deep under too many menus. I also wish there were multiple home screens/work spaces like those found in the HTC Hero. In the N97’s home screen there is this somewhat bizarre two screen thing going on. The default one has all your widgets and shortcuts and when you swipe your finger the right or left, it basically clears all your widgets. The empty screen basically just gives you an uncluttered view of your wallpaper that does not seem overly useful. At this time of writing, my phone’s firmware is RM-505 V12 and there is this very weird bug whereby the transition effects would just disappear once in awhile for no apparent reason but other than that all the earlier bugs found were resolved or at least those that I know of. Kinetic scrolling would be coming to all parts of the phone when Firmware V20 is released, currently I still have to drag the scrollbar on the side of the screen which while is easier than expected, is not the most intuitive way of scrolling. Currently only the Web Browser has kinetic scrolling.

In terms of performance, this phone was a step up from my previous phone the P1i most of them time. Due to the ridiculously low system partition (C:\) space, I guess some slowdowns are expected when running loads of apps. This is somewhat mitigated by the fact that all the apps that can be installed on the mass memory was re-installed on mass memory for me (Facebook, Ovi Contacts, Friendster etc) and the speed was so much better after I was forced to hard reset my phone which wiped a lot of crap from my phone. According to what I read, the OS optimizations and system partition space increase will address this problem in the next firmware (V20) . The default Symbian S60 5th Edition task manager is one of the areas where I actually miss my UIQ based Sony Ericsson P1i. The default task manager in the N97 and S60 5th Edition in general is just pathetic when compared to the P1i, not to mention its competition. 

When the press people in Nokia said this was a phone focused on media capture, they really meant media capture. Why you might ask? Well this thing’s media playback capability is very lacking. It does not allow me to play WMV, AVI or M4V. All of which are very popular media formats for online video podcasts and not to mention desktop video. On a brighter note on its multimedia capabilities, it does support Real Media and has a very good camera. The camera is not as good as a N86 and is eating the smoke of some Samsung. LG and Sony Ericsson mid range feature phones but it is more than sufficient for my needs. I am not very into photography so for folks like me, as long as the image is not blur or grainy, its good enough for me.

Its PIM functions(calendar, notes and tasks) were very good. Nothing I can really complain here. I am not a CEO with loads of meetings and all that so I guess my word does not really count. I basically use my phone to store my contacts and keeping my events in the calendar plus storing the minutes of the meetings in my company that relate to me. I also use Mail for Exchange and Ovi to backup my data to Google and Ovi respectively and those work well too. One thing that I wish that they did much better is Ovi Contacts which basically tries to emulate Palm Synergy/Motorola MotoBlur whereby it tries to tie in all your contacts into a single view from MSN, Facebook, Yahoo and all that. In this case however all that they can tie in is Google Talk and Nokia Ovi Messenger. I wish they would bring in the other ones such as Skype, MSN, Facebook and Yahoo which have much more use, at least in Malaysia they do.

The Ovi suite is a mixed bag. I do like the features that it provides but the interface just seems a whole lot more clunky when compared to iTunes and the fact that it is a memory hog does endear it to me. The only tools that I used often are Nokia Music and Nokia Suite. For pictures I use Picasa and due to the bad codec support of the N97 and the very slow and memory intensive video conversion in the Ovi Suite, I use Real Player to convert the files and just copy over the video via Ovi Suite or Windows Explorer. I heard the new Ovi 2.0 which got released yesterday is very much improved. Will blog abut it when I get the time.

The Ovi service while not perfect, is very good in my personal opinion. The services are not as good as the Google services like Google Calendar, GMail and all that but in true Nokia fashion, they get the job done. Its calendar and contacts is now my main PIM backup service with Google being the backup in case of Ovi actually ending up like Danger/Microsoft’s Sidekick fiasco.

My wish list :

  1. Integrated Music Player + Podcast Player – While they are currently loosely linked as in the music player can access the podcasts downloaded, it is not seamlessly integrated. Like say when I listen to the podcast in the podcast player, it would show as played in the podcast player but shows as new in the music player. They have already integrated the Video Player with the Video Podcast player, why can’t the integrate this?
  2. Better Codec Support – As I stated earlier, the codec support in the N97 is not the best in the world. Adding support for .m4v and .avi is not too much to ask now is it?
  3. Better Nokia Messaging Integration – Currently the UI does not really blend with the phone and Nokia Messaging is accessed as a separate application. Integration to the central system messaging or even better integrated to Ovi Contacts and Contacts like Motoblur.
  4. Wider Network Support – Ovi Contacts would really rock of they integrated MSN/Windows Live Messnger, Facebook, Linked In & Tweeter. Again this is done to great effect in Motorola MotoBlur and Palm Synergy.
  5. More Home Screens – See HTC Hero’s 7 home screens and you will know what I mean.
  6. Threaded Messaging – This can be seen in the iPhone, HTC Hero and for gods sake it has been done on the latest Ovi Suite. Please put this into the messaging center.
  7. Task Manager – As stated earlier, the task manager in the N97 is really weak. The multi task management in the Palm Pre and HTC Hero just seems to much more intuitive.

Those are what I REALLY want in the N97 and I hope that they can do it soon as I really like this phone, just that it could be so much better and the competition is starting to really supersede this phone.

Nokia N900 Preview

Leave a comment

Two videos that preview the latest Nokia N900 smartphone.

Nokia N900 preview @ Amsterdam (part 1) – BelgiqueMobile.be

Nokia N900 preview @ Amsterdam (part 2) – BelgiqueMobile.be

Not quite the iPhone killer IMO but definitely something that would make things difficult for Google Android Phones. More importantly, I WANT ONE !!

ColdFusion 9 ORM

Leave a comment

When I first heard that ColdFusion 9 will have Object Relational Mapping which is otherwise known as ORM baked in, I really did not know what to make of it. While I really did like the idea of portable SQL agnostic DAO code, I dreaded the configuration hell that I encountered with Hibernate and NHibernate. While I did have an okay experiance with the Castle Project’s ActiveRecord implementation of NHibernate, it did not feel very intuitive, at least to me.

Today I spent the better part of the day coding with the new release of ColdFusion and I was blown away with how much it has improved, particularly when used in conjunction with the Eclipse based ColdFusion Builder/Bolt IDE. I particularly love the new ColdFusion(CF) Builder IDE, it looks very polished and has code completion capabilities that felt more like Visual Studio which is a step up from cfeclipse. ColdFusion Builder also has very good integration with ColdFusion servers as it now can start, stop and monitor the ColdFusion servers which is a very big step up. Previously I had to jump between the CF Web Admin/CF Server Monitor and Eclipse/Dreamweaver when I was doing CF development which is very tedious. With this now I can do most of the stuff from within CF Builder which makes life very easy. I tried installing it on my instance of Eclipse that has Flex Builder 3 and it works great. I smell fun times on the horizon when coding apps.

Now back to the main point of this post, Adobe’s implementation of Hibernate which in true CF tradition, is very intuitive and simple. I was seriously having loads of fun playing with it. Here is some of the stuff I did today :

* I will be putting in my 2 cents/commentary on the benefits I see as comments in the code as I believe it is easier to understand that way.

Application.cfc

Application.cfc

Books.cfc

Books.cfc

Writers.cfc

Writers.cfc

index.cfm

index.cfm

Sample Output

Sample Output

As you can see above, it is is really easy to retrieve items from the database now with the new ORM framework baked in. The entityLoad(“Writers”) method basically did this :

SELECT a.*, b.* FROM Authors a INNER JOIN Books B ON A.AuthorID = B.AuthorID

Notice that now it is possible to write ColdFusion Components totally based on CFScript which is quite close to ActionScript/JavaScript/ECMAScript. I person love option of writing classes in CFScript but I still think that CFML still has its place. I know I for one prefer to loop though collections using cfloop.

I will blog more about other CF 9 ORM methods and setups in the days to come.