Home > mx Examples > mx Vertical Scroll Position

mx Vertical Scroll Position

In regards to my post about maintaining vertical scroll position, James asked how the same would be done for mx based components. Here is an example using an mx:DataGrid.

Here is an example of the problem:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="onCreationComplete(event);">
	<mx:Script>
		<![CDATA[
			import mx.events.FlexEvent;
			
			private function onCreationComplete(event:FlexEvent):void
			{
				var t:Timer = new Timer(5000);
				t.addEventListener(TimerEvent.TIMER, onTimer);
				t.start();
			}
			
			private function onTimer(e:TimerEvent):void
			{
				_position = dg.verticalScrollPosition;
				
				var dp:Array = [{label: "1"}, {label: "4"},
								{label: "7"}, {label: "10"},
								{label: "13"}, {label: "2"},
								{label: "5"}, {label: "8"},
								{label: "11"}, {label: "14"},
								{label: "3"}, {label: "6"},
								{label: "9"}, {label: "12"},
								{label: "15"}];
								
				dg.dataProvider = dp;
			}
		]]>
	</mx:Script>
	<mx:DataGrid id="dg"/>
</mx:Application>

This is one way to solve the lost scroll position.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="onCreationComplete(event);">
	<mx:Script>
		<![CDATA[
			import mx.events.FlexEvent;
			
			private var _position:int;
			
			private function onCreationComplete(event:FlexEvent):void
			{
				var t:Timer = new Timer(5000);
				t.addEventListener(TimerEvent.TIMER, onTimer);
				t.start();
			}
			
			private function onTimer(e:TimerEvent):void
			{
				_position = dg.verticalScrollPosition;
				
				var dp:Array = [{label: "1"}, {label: "4"},
								{label: "7"}, {label: "10"},
								{label: "13"}, {label: "2"},
								{label: "5"}, {label: "8"},
								{label: "11"}, {label: "14"},
								{label: "3"}, {label: "6"},
								{label: "9"}, {label: "12"},
								{label: "15"}];
								
				dg.dataProvider = dp;
			}
			
			private function onDgUpdateComplete(e:FlexEvent):void
			{
				dg.verticalScrollPosition = _position;
			}
		]]>
	</mx:Script>
	<mx:DataGrid id="dg" updateComplete="onDgUpdateComplete(event);"/>
</mx:Application>
Advertisements
  1. August 5, 2011 at 1:56 PM

    How do I return to the same vertical position here?

    • roustalski
      August 9, 2011 at 6:31 AM

      I’m not sure what you’re asking.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: