Setting the selected index of a Flex LinkBar control

by Peter deHaan on January 20, 2008

in LinkBar

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>

{ 8 comments… read them below or add one }

1 Anome January 20, 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.

Reply

2 Zuhair March 16, 2008 at 5:57 am

Hi,

the actionscript code is not working:

Could you pleas check that

Thanks

Reply

3 Dom April 8, 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…

Reply

4 Sohail August 13, 2008 at 2:35 am

True this will not work “linkBar.selectedIndex = some_value”. To do this make your own custom linkbar let say call it myLinkBar

==========================================================================

==========================================================================

And then in the itemClick (itemClick="myFunc(event)") do the following:-

private function myFunc(event:ItemClickEvent):void
{
    mylinkbar.setIndex(event.index);
}

Reply

5 Alex September 22, 2008 at 10:20 am

You can try this code:

linkBar.dataProvider=null;
linkBar.dataProvider=arr;
linkBar.selectedIndex=event.index;

Reply

6 Flexman May 22, 2009 at 4:17 pm

This allows you to use the LinkBar without a viewstack when selecting an index…

Reply

7 Flexman May 22, 2009 at 4:19 pm

Helps is I use the right tags… ;)

&lt?xml version=”1.0″ encoding=”utf-8″?&gt
&ltmx:LinkBar xmlns:mx=”http://www.adobe.com/2006/mxml” itemClick=”myFunc(event)”&gt

&ltmx:Script&gt
&lt![CDATA[

import mx.events.ItemClickEvent;
import mx.controls.Button;

private function myFunc(event:ItemClickEvent):void
{
var n:int = this.numChildren;
for (var i:int = 0; i
&lt/mx:Script&gt

&lt/mx:LinkBar&gt

Reply

8 Drew October 17, 2009 at 8:59 am

I’m trying to remove all spacing between linkbar link buttons, I have a design that calls for the links to be stacked vertically with 5px of space between them, I’ve cleared out all padding, for the linkbuttonstyles, and removed vertical gap, and set separator width to zero. What am I missing it seems like this shouldn’t be that hard? Any help is much appreciated thanks!

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

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: