27
Aug
08

Setting the horizontal scrollbar policy on a DataGrid control in Flex

The following example shows how you can set the horizontal scrollbar policy on a Flex DataGrid control by setting the horizontalScrollPolicy property to one of the constants in the mx.core.ScrollPolicy class.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/08/27/setting-the-horizontal-scrollbar-policy-on-a-datagrid-control-in-flex/ -->
<mx:Application name="DataGrid_horizontalScrollPolicy_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.core.ScrollPolicy;
        ]]>
    </mx:Script>

    <mx:XML id="xmlDP" source="data/dp.xml" />

    <mx:Array id="arr">
        <mx:String>{ScrollPolicy.AUTO}</mx:String>
        <mx:String>{ScrollPolicy.OFF}</mx:String>
        <mx:String>{ScrollPolicy.ON}</mx:String>
    </mx:Array>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="horizontalScrollPolicy:">
                <mx:ComboBox id="comboBox"
                        dataProvider="{arr}" />
            </mx:FormItem>
        </mx:Form>
        <mx:Spacer width="100%" />
        <mx:Button label="Set dataProvider"
                click="dataGrid.dataProvider = xmlDP.row;" />
        <mx:Button label="Clear dataProvider"
                click="dataGrid.dataProvider = [];" />
    </mx:ApplicationControlBar>

    <mx:DataGrid id="dataGrid"
            horizontalScrollPolicy="{comboBox.selectedItem}"
            width="100%"
            height="100%" />

</mx:Application>

View source is enabled in the following example.

You can also set the horizontalScrollPolicy property using ActionScript, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/08/27/setting-the-horizontal-scrollbar-policy-on-a-datagrid-control-in-flex/ -->
<mx:Application name="DataGrid_horizontalScrollPolicy_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.events.ListEvent;
            import mx.core.ScrollPolicy;

            private function comboBox_change(evt:ListEvent):void {
                var value:String = comboBox.selectedItem.toString();
                dataGrid.horizontalScrollPolicy = value;
            }
        ]]>
    </mx:Script>

    <mx:XML id="xmlDP" source="data/dp.xml" />

    <mx:Array id="arr">
        <mx:String>{ScrollPolicy.AUTO}</mx:String>
        <mx:String>{ScrollPolicy.OFF}</mx:String>
        <mx:String>{ScrollPolicy.ON}</mx:String>
    </mx:Array>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="horizontalScrollPolicy:">
                <mx:ComboBox id="comboBox"
                        dataProvider="{arr}"
                        selectedIndex="1"
                        change="comboBox_change(event);" />
            </mx:FormItem>
        </mx:Form>
        <mx:Spacer width="100%" />
        <mx:Button label="Set dataProvider"
                click="dataGrid.dataProvider = xmlDP.row;" />
        <mx:Button label="Clear dataProvider"
                click="dataGrid.dataProvider = [];" />
    </mx:ApplicationControlBar>

    <mx:DataGrid id="dataGrid"
            horizontalScrollPolicy="off"
            width="100%"
            height="100%" />

</mx:Application>

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/08/27/setting-the-horizontal-scrollbar-policy-on-a-datagrid-control-in-flex/ -->
<mx:Application name="DataGrid_horizontalScrollPolicy_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        initialize="init();">

    <mx:Script>
        <![CDATA[
            import mx.core.ScrollPolicy;
            import mx.containers.ApplicationControlBar;
            import mx.containers.Form;
            import mx.containers.FormItem;
            import mx.controls.Button;
            import mx.controls.ComboBox;
            import mx.controls.DataGrid;
            import mx.controls.Spacer;
            import mx.events.ListEvent;

            private var arr:Array;
            private var comboBox:ComboBox;
            private var setBtn:Button;
            private var clrBtn:Button;
            private var dataGrid:DataGrid;

            private function init():void {
                arr = [];
                arr.push(ScrollPolicy.AUTO);
                arr.push(ScrollPolicy.OFF);
                arr.push(ScrollPolicy.ON);

                comboBox = new ComboBox();
                comboBox.dataProvider = arr;
                comboBox.addEventListener(ListEvent.CHANGE, comboBox_change);

                setBtn = new Button();
                setBtn.label = "Set dataProvider";
                setBtn.addEventListener(MouseEvent.CLICK, setBtn_click);

                clrBtn = new Button();
                clrBtn.label = "Clear dataProvider";
                clrBtn.addEventListener(MouseEvent.CLICK, clrBtn_click);

                var spacer:Spacer = new Spacer();
                spacer.percentWidth = 100;

                var formItem:FormItem = new FormItem();
                formItem.label = "horizontalScrollPolicy:";
                formItem.addChild(comboBox);

                var form:Form = new Form();
                form.styleName = "plain";
                form.addChild(formItem);

                var appControlBar:ApplicationControlBar;
                appControlBar = new ApplicationControlBar();
                appControlBar.dock = true;
                appControlBar.addChild(form);
                appControlBar.addChild(spacer);
                appControlBar.addChild(setBtn);
                appControlBar.addChild(clrBtn);
                addChildAt(appControlBar, 0);

                dataGrid = new DataGrid();
                dataGrid.percentWidth = 100;
                dataGrid.percentHeight = 100;
                addChild(dataGrid);
            }

            private function comboBox_change(evt:ListEvent):void {
                var value:String = comboBox.selectedItem.toString();
                dataGrid.horizontalScrollPolicy = value;
            }

            private function setBtn_click(evt:MouseEvent):void {
                dataGrid.dataProvider = xmlDP.row;
            }

            private function clrBtn_click(evt:MouseEvent):void {
                dataGrid.dataProvider = [];
            }
        ]]>
    </mx:Script>

    <mx:XML id="xmlDP" source="data/dp.xml" />

</mx:Application>

4 Responses to “Setting the horizontal scrollbar policy on a DataGrid control in Flex”


  1. 1 Lynn Nov 12th, 2008 at 11:13 pm

    Hi, another great example you’ve got here

    However there is one question, is it possible to scroll within the datagrid control or within a panel when the horizontal Scroll Policy is set to “Off”?

    Thanks in Advance.

  2. 2 Sean Jan 6th, 2009 at 1:22 pm

    I notice that the scroll does not scroll smoothly, but “jumps” to keep a full field at the left edge of the view. Is there any way to have the scroll work smoothly over large fields? ie. A field could be only partly shown on the left side of the view?

    Love your site!

  3. 3 Joshua Stewardson Jan 15th, 2009 at 12:52 pm

    Is there any way to have a DataGrid display horizontally instead of vertically? Like I want to have my headers be the first row like normal and also the first column? I tried to check the DataGrid and the AdvancedDataGrid but couldn’t find anything?

    Thanks!

    .:Joshua

  4. 4 Joshua Stewardson Jan 15th, 2009 at 1:05 pm

    Example in HTML:

    <table>
    <tr>
    <th></th>
    <th>2008-11</th>
    <th>2008-12</th>
    <th>2009-01</th>
    </tr>
    <tr>
    <th>Dev</th>
    <td>5.50</td>
    <td>6.50</td>
    <td>7.50</td>
    </tr>
    <tr>
    <th>QA</th>
    <td>6.00</td>
    <td>6.00</td>
    <td>7.00</td>
    </tr>
    </table>

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;".




Badge Farm

  • Powered by Redoable 1.2
  • Cornify
  • Feeds burnt by Feedburner
  • Feed