23
Feb
08

Changing the default sort arrow skin on a Flex DataGrid control

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>

3 Responses to “Changing the default sort arrow skin on a Flex DataGrid control”


  1. 1 Steve Feb 25th, 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.

  2. 2 Yaniv Feb 28th, 2008 at 6:34 am

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

  3. 3 peterd Feb 28th, 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. :)

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".