Preventing TabNavigator from selecting tabs.
I’ve been working for couple of weeks now on a large Flex 3 based project at the company I work for. It’s a multi-user publishing system for various flash based creative types, that aids and alleviates the whole creative from concept creation through approval cycles to deployment on various platforms.
In this project I had a page that contained a tab navigator with a custom editors inside of it. It all worked fine, before I needed to make sure the data on the page is saved before going onto another tab. To make this work I had to essentialy force tab navigator to cancel select child action after user click on the other tab and the data on the current weren’t saved.
This proved to be a problem.
After a bit of poking around I discovered there are 2 viable solutions to this problem.
1. to use IndexChangedEvent.CHANGE event listener.
This solution involved storing current selected child index in a variable , assigning the IndexChangedEvent.CHANGE listener via actionscript to the tab navigator and then checking whether page was saved and if it wasn’t setting the selectedIndex back to the stored index.
Problem with this solution was that it allowed tab navigator to change momentarily to that tab I was clicking on and then changing back. I needed a solution where the tab wouldn’t render at all…
2. to use click event.
Nice things about Mouse Events is that they can be prevented from propagation. So even though the initial click event is captured, with stopImmediatePropagation it can be stopped before the components does it’s implementation.
With this solution I had to check that I will stop only the actions that happened when user clicked on the tab bar and not on the tab content. Otherwise nothing inside the tab container would respond to mouse events which would be rather undesirable effect..
This example illustrates this technique and all the code is pasted bellow.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="500"
height="300" layout="vertical" creationComplete="init()" verticalAlign="middle">
<mx:Script>
<![CDATA[
import mx.controls.tabBarClasses.*;
private function initHandlers():void
{
tabNav.addEventListener(MouseEvent.CLICK, onTabNavClicked, true);
}
private function onTabNavClicked(event:MouseEvent):void
{
if (event.target is mx.controls.tabBarClasses.Tab )
{
var vBox:VBox = tabNav.getChildAt(tabNav.selectedIndex) as VBox;
if (vBox.data == "unsaved") event.stopImmediatePropagation();
}
}
]]>
</mx:Script>
<mx:TabNavigator id="tabNav" width="100%" height="100%"
creationComplete="initHandlers()" tabHeight="18">
<mx:VBox id="tabA_vbx" width="100%" height="100%"
label="A" horizontalAlign="center" verticalAlign="middle">
<mx:Label text="TAB A" fontSize="36" width="80%" textAlign="center"/>
<mx:Text text="Choose whether form is saved or unsaved. Unless its saved ,
tab navigator will not switch to the other tabs." width="80%"/>
<mx:Form width="100%" height="100%">
<mx:RadioButton label="Mark form as ’saved’" selected="true"
click="{tabA_vbx.data=’saved’}; tabA_vbx.label=’A'"/>
<mx:RadioButton label="Mark form as ‘not saved’"
click="{tabA_vbx.data=’unsaved’}; tabA_vbx.label=’A*’" />
</mx:Form>
</mx:VBox>
<mx:VBox width="100%" height="100%" label="B"
horizontalAlign="center" verticalAlign="middle">
<mx:Label text="TAB B" fontSize="36" width="80%" textAlign="center"/>
</mx:VBox>
<mx:VBox id="tabC_vbox" width="100%" height="100%"
label="C" horizontalAlign="center" verticalAlign="middle">
<mx:Label text="TAB C" fontSize="36" width="80%" textAlign="center"/>
<mx:Form width="100%" height="100%">
<mx:RadioButton label="Mark form as ’saved’" selected="true"
click="{tabC_vbox.data=’saved’}; tabC_vbox.label=’C'"/>
<mx:RadioButton label="Mark form as ‘not saved’"
click="{tabC_vbox.data=’unsaved’}; tabC_vbox.label=’C*’" />
</mx:Form>
</mx:VBox>
</mx:TabNavigator>
</mx:Application>
Dowbload Flex 3 Project


September 25th, 2008 at 6:39 am
good site lgyaib
October 10th, 2008 at 1:25 pm
Thanks for sharing this Slav! It really came in handy in my current project. Keep up the good work
October 22nd, 2008 at 6:30 pm
looking forward for more information about this. thanks for sharing. Eugene
October 29th, 2008 at 6:48 am
Thanks for this solution.
November 11th, 2009 at 1:00 pm
Sweet, thanks for this, saved me a few hours I think.
June 22nd, 2010 at 12:11 pm
Hi,
I have similar requirement with some additions. i need to show alert message asking user that data is not saved and whether he would like to proceed to next selected tab or not. If user clicks OK then stop the propogation. But if user click cancel on alert message, i dont want to navigate to the next selected tab. And also every time i visit any tab, i want it to be refreshed.
As in your case, if i select ‘data saved’ on tab A, then go to tab C (dont select any radio button on tab C) then again select tab A. Now i see that ‘data saved’ is already selected on tab A, which i dont want. I dont want any of the radio buttons selected once i come back from tab B or C (without selecting anyting on tab B or C). Can you show how to do that ?
Thanks.