05-20-2008 6:23 PM
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.
05-22-2008 3:50 PM
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
05-20-2008 9:57 PM
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
05-22-2008 3:50 PM
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
05-24-2008 7:43 AM
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
05-24-2008 3:13 PM
@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.
05-25-2008 7:25 AM
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
05-27-2008 9:16 PM
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;
}
]]>