Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Column auto sort in Flex 2 DataGrid

former_member583013
Active Contributor
0 Kudos

Hi:

I'm using a DataGrid that feeds from a XML generated on PHP...It works just great...The problem is that columns get auto sorted alphabetically when the DataGrid shows up...How can I prevent that from happening???

FYI, I'm updating my Flex/PHP/SAP SE16 emulator...I got all columns but not in the same position that I'm sending them...I'm of course not specifiying COLUMNS...Just letting the DataGrid generate them for me -:)

Greetings,

Blag.

1 ACCEPTED SOLUTION

abesh
Contributor
0 Kudos

Hey Blag,

Check this out : [Sorting data in DataGrid controls|http://livedocs.adobe.com/flex/201/html/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Book_Parts&file=dpcontrols_062_18.html]

I think this might help you. Just check the region to disable datagrid sorting on a column

Abesh

6 REPLIES 6

athavanraja
Active Contributor
0 Kudos

are you doing it thru actionscript? how are you setting up the dataprovider? probably if you can post the code i can play around and see whats going wrong.

Raja

abesh
Contributor
0 Kudos

Hey Blag,

Check this out : [Sorting data in DataGrid controls|http://livedocs.adobe.com/flex/201/html/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Book_Parts&file=dpcontrols_062_18.html]

I think this might help you. Just check the region to disable datagrid sorting on a column

Abesh

athavanraja
Active Contributor
0 Kudos

Abesh,

I guess Blag is talking about column positioning and not about sorting the data within the column.

Blag: correct me if i am wrong.

Raja

0 Kudos

@Raja:

Your 100% percent correct -:) For example...My DD Table got this fields...


MANDT
NAME
SURNAME
AGE

But when the table is showed on the DataGrid it goes like...


AGE
MANDT
NAME
SURNAME

So, it sorted alphabetically...And of course I don't want that -:( Can help??? -:D

@Abesh:

Thanks anyway dude...Link was great and it's going to useful for next projects -;)

Greetings,

Blag.

athavanraja
Active Contributor
0 Kudos

i need to understand how you are managing the xml and how you are building the datagrid.

DataGrid that feeds from a XML generated on PHP

did you check that the xml is in proper order?

if you could post the piece of code where you received the xml and build the datagrid, it will be easier to crack.

Raja

0 Kudos

Yup DataGrid auto sorts the columns alphabetically unless you specify their order, from the docs:

An array of DataGridColumn objects, one for each column that can be displayed. If not explicitly set, the DataGrid control attempts to examine the first data provider item to determine the set of properties and display those properties in alphabetic order.

[DataGrid Flex 2 Documentation|http://livedocs.adobe.com/flex/2/langref/mx/controls/DataGrid.html#columns]

So... here is a nifty way to do it, that will preserve the order of the dataProvider that the datagrid is linked to ( which I believe is what you want it todo ) One side benefit is you can pass headerText in the XML it will also get mapped, FTW!

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
	<mx:XMLList id="SAPData" xmlns="">
		<row><mandt headerText="Client">000</mandt><name>Foo</name><surname>Bar</surname><age>66</age></row>
		<row><mandt>000</mandt><name>Dan</name><surname>Mcweeney</surname><age>28</age></row>
	</mx:XMLList>
	<mx:XMLList id="rowData" xmlns="">
		<row mandt="000" name ="Foo" surname="Bar" age="66"/>
		<row mandt="000" name ="Dan" surname="McWee" age="28"/>
	</mx:XMLList>
	<mx:Script>
		<![CDATA[
			import mx.controls.dataGridClasses.DataGridColumn;
			import mx.controls.DataGrid;
			private static const HeaderAttrib:String = "headerText";
			private function setupColumns(event:Event):void{
				var dg:DataGrid = event.currentTarget as DataGrid;
				if(dg == null)
					return;
				var columns:Array = dg.columns;
				columns.slice(0,columns.length);
				try{
					var xml:XML = dg.dataProvider[0];
				}
				catch(error:RangeError){
					return;
				}
				var col:DataGridColumn;
//  Normally data is returned nested as in the SAPData Example XMLList:  
				if(xml.attributes().length() == 0){
					for each(var item:XML in xml.children()){
						col = new DataGridColumn();
						col.dataField = item.name();
						if(item.attribute(HeaderAttrib).toString() == "")
							col.headerText = col.dataField;
						else
							col.headerText = item.attribute(HeaderAttrib);
						columns.push(col);
					}
				}				
				else{
// Don't think SAP returns data like this, but for completeness here is how it would work:
					for each(var attrib:XML in xml.attributes()){
						col = new DataGridColumn();
						col.dataField = "@" + attrib.name();
						col.headerText = attrib.name();
						columns.push(col);
					}		
				}
				dg.columns = columns;
			}
		
	
		
	
]]>