Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: DataGrid Sort

  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    DataGrid Sort

    The default behavior of the Flex datagrid descending sort seems to be that a selected row remains in view, meaning that the view will scroll down to show the selected row. I would like to change this so that when doing a descending sort the view remains at the top instead of scrolling. I'm new to Flex, so if someone could give me an example, I would appreciate it, with either DataGrid or AdvancedDataGrid. Thanks.

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Add a callback to the sortChange event on your datagrid and then call ensureCellIsVisible() to force a scroll to the top:
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
    			   xmlns:s="library://ns.adobe.com/flex/spark"
    			   xmlns:mx="library://ns.adobe.com/flex/mx"
    			   width="300" height="300">
    	<fx:Script>
    		<![CDATA[
    			import spark.events.GridSortEvent;
    						
    			protected function myDG_sortChangeHandler(event:GridSortEvent):void
    			{
    				myDG.ensureCellIsVisible(0);
    			}			
    		]]>
    	</fx:Script>
    	<fx:Declarations>
    		<!-- Place non-visual elements (e.g., services, value objects) here -->
    	</fx:Declarations>
    	
    	<s:DataGrid id="myDG" width="250" height="250" sortChange="myDG_sortChangeHandler(event)">
    		<s:columns>
    			<s:ArrayList>
    				<s:GridColumn dataField="@label" headerText="Label" />
    				<s:GridColumn dataField="@data" headerText="Data" />
    			</s:ArrayList>
    		</s:columns>
    		<s:dataProvider>
    			<s:XMLListCollection id="dp">
    				<s:source>
    					<fx:XMLList>
    						<product label="Product 1" data="3" />
    						<product label="Product 2" data="1" />
    						<product label="Product 3" data="4" />
    						<product label="Product 4" data="1" />
    						<product label="Product 5" data="5" />
    						<product label="Product 6" data="9" />
    						<product label="Product 1" data="3" />
    						<product label="Product 2" data="1" />
    						<product label="Product 3" data="4" />
    						<product label="Product 4" data="1" />
    						<product label="Product 5" data="5" />
    						<product label="Product 6" data="9" />
    					</fx:XMLList>
    				</s:source>
    			</s:XMLListCollection>
    		</s:dataProvider>
    	</s:DataGrid>
    	
    </s:Application>

  • Users who have thanked Inigoesdr for this post:

    DevilRay (09-18-2011)

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    This is exactly what I was looking for. It works perfectly. Thanks! I really appreciate it.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •