20
Jan
08

Setting the selected index of a Flex LinkBar control

The following example shows you how you can set the selected index of a LinkBar control in Flex by setting the selectedIndex property using MXML or ActionScript.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/01/20/setting-the-selected-index-of-a-flex-linkbar-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Array id="arr">
        <mx:Object label="One" />
        <mx:Object label="Two" />
        <mx:Object label="Three" />
        <mx:Object label="Four" />
    </mx:Array>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="selectedIndex:">
                <mx:HSlider id="slider"
                        minimum="0"
                        maximum="{linkBar.dataProvider.length - 1}"
                        liveDragging="true"
                        snapInterval="1"
                        tickInterval="1"
                        dataTipPrecision="0" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:LinkBar id="linkBar"
            dataProvider="{arr}"
            selectedIndex="{slider.value}" />

</mx:Application>

View source is enabled in the following example.

You can also set the selectedIndex property in ActionScript, as shown in the following snippet:

<mx:Script>
    <![CDATA[
        import mx.events.SliderEvent;

        private function slider_change(evt:SliderEvent):void {
            linkBar.selectedIndex = evt.value;
        }
    ]]>
</mx:Script>

3 Responses to “Setting the selected index of a Flex LinkBar control”


  1. 1 Anome Jan 20th, 2008 at 12:11 pm

    There is a bug, try to click on the LinkBar’s items then use the slider : several items can be selected !
    to be honnest, i fixed the problem by setting the ‘enabled’ property of each LinkButton to false, except the one that should be selected.

  2. 2 Zuhair Mar 16th, 2008 at 5:57 am

    Hi,

    the actionscript code is not working:

    Could you pleas check that

    Thanks

  3. 3 Dom Apr 8th, 2008 at 6:42 am

    It seems that linkBar.selectedIndex = some_value is not supported if the linkbar is not paired with a viewstack, which is also the implicit dataprovider, thus the issue mentioned above.
    It’s a bit of a pain…

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