Changing the default sort arrow skin on a Flex DataGrid control

by Peter deHaan on February 23, 2008

The following example shows how you can change the sort arrow skin on a DataGrid control in Flex by setting the sortArrowSkin style.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/02/23/changing-the-default-sort-arrow-skin-on-a-flex-datagrid-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Array id="arr">
        <mx:Object c1="Item 1A" c2="Item 1B" />
        <mx:Object c1="Item 2A" c2="Item 2B" />
        <mx:Object c1="Item 3A" c2="Item 3B" />
        <mx:Object c1="Item 4A" c2="Item 4B" />
        <mx:Object c1="Item 5A" c2="Item 5B" />
        <mx:Object c1="Item 6A" c2="Item 6B" />
        <mx:Object c1="Item 7A" c2="Item 7B" />
        <mx:Object c1="Item 8A" c2="Item 8B" />
        <mx:Object c1="Item 9A" c2="Item 9B" />
    </mx:Array>

    <mx:DataGrid id="dataGrid"
            dataProvider="{arr}"
            sortArrowSkin="@Embed('assets/thumb_down.png')"
            width="300"
            rowCount="8">
        <mx:columns>
            <mx:DataGridColumn dataField="c1"
                    headerText="Column 1:" />
            <mx:DataGridColumn dataField="c2"
                    headerText="Column 2:" />
        </mx:columns>
    </mx:DataGrid>

</mx:Application>

View source is enabled in the following example.

You can also set the sortArrowSkin style in an external .CSS file or <mx:Style /> block, as shown in the following snippet:

<mx:Style>
    DataGrid {
        sortArrowSkin: Embed("assets/thumb_down.png");
    }
</mx:Style>

Or, you can set the sortArrowSkin style using ActionScript, using the following snippet:

<mx:Script>
    <![CDATA[
        [Embed("assets/thumb_down.png")]
        private var thumbDownIcon:Class;

        private function init():void {
            dataGrid.setStyle("sortArrowSkin", thumbDownIcon);
        }
    ]]>
</mx:Script>

{ 6 comments… read them below or add one }

Steve February 25, 2008 at 1:49 am

Thanks for another great Flex tip. One question I have about the sort arrow is how do you get the sort arrow to appear without having to click on a column header? I have an app where I need the arrow to show after the data loads.

Reply

Yaniv February 28, 2008 at 6:34 am

Exactly my problem, Steve :)
I need it as well, Help us oh mighty guru ! :)

Reply

peterd February 28, 2008 at 5:42 pm

Steve/Yaniv,

For an example, see “Displaying the sort arrow in a Flex DataGrid control without having to click a column”.

Peter

PS: A big thanks to my coworker Rob for helping me with the solution. And by “help”, I mean I stole his solution. :)

Reply

A Visitor February 4, 2009 at 1:56 pm

How are you setting the thumb up icon?

Reply

Kevin November 3, 2009 at 6:40 am

How is the thumb-up icon being displayed?

Reply

Peter deHaan November 3, 2009 at 7:07 am

@Kevin/A Visitor,

Using the sortArrowSkin style. The sortArrowSkin skin is just flipped vertically depending on whether it is an ascending sort or descending sort.

<mx:DataGrid id="dataGrid" ... sortArrowSkin="@Embed('assets/thumb_down.png')" />

Peter

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: