Sort a datagrid?

Hello,

i have a datagrid with 5 columns. When i start the application the datagrid sorts the data belonging to the id. But i want that the data is sorted in another column.

How can i solve this?

Thank you.

druplash
druplashAsked:
Who is Participating?
 
andreMarianoCommented:
anyway... if it is not working... you can do the 2nd method... dispatch the event...

gaesteDg.dispatchEvent ( new DataGridEvent(DataGridEvent.HEADER_RELEASE, false, true, 0, null, 0, null, null, 0));


private function getGaesteResult(e:ResultEvent):void
			{
				gaesteDg.dataProvider = e.result;
                                gaesteDg.dispatchEvent ( new DataGridEvent(DataGridEvent.HEADER_RELEASE, false, true, 0, null, 0, null, null, 0));
//the first 0 is the columnIndex and the second 0 is from witch row it starts
			}

Open in new window

0
 
blue-genieCommented:
0
 
druplashAuthor Commented:
Yes it is, but please can you give me an example with the column name "lastname". I´m not a developer.

Thank you!
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
druplashAuthor Commented:
Please can you show me, how the caller function has to look like?

Thanks
0
 
blue-genieCommented:
that post explains to you exactly what to do


this is the function ...
private function dataGridDefaultSort(dgName:Object, dgColumn:int):void{   .. . etc

so

dataGridDefaultSor(nameOfYourDG, columnID);
0
 
druplashAuthor Commented:
OK, but when i call the function i get an 1120 error:

private function dataGridDefaultSort(dgName:Object, dgColumn:int):void{
                        dgName.dispatchEvent(new DataGridEvent(DataGridEvent.HEADER_RELEASE,   false,true,dgColumn,null,0,null,null,0));
                  }

caller:
dataGridDefaultSort(gaesteDg, kartennummer);

Thanks
0
 
blue-genieCommented:
what's the specific error, check the location of the error?
what is kartennunmmer ?
will need to see specifics to see where /what the problem is.
0
 
druplashAuthor Commented:
kartennummer is the name of the column in the datagrid, that has to be sorted on start.



<mx:DataGrid id="gaesteDg" doubleClickEnabled="true" doubleClick="gaesteDg_clickHandler(event)" y="100" left="30" right="30" height="400" includeIn="GaesteStandard">
		<mx:columns>
			<mx:DataGridColumn headerText="Firma" dataField="firma" width="270"/>
			<mx:DataGridColumn headerText="Nachname" dataField="nachname" width="220"/>
			<mx:DataGridColumn headerText="Vorname" dataField="vorname"/>
			<mx:DataGridColumn headerText="Kategorie" dataField="kategorie" width="120"/>
			<mx:DataGridColumn headerText="Tisch Nr." dataField="tischnummer" width="80"/>
			<mx:DataGridColumn headerText="Karten Nr." dataField="kartennummer" width="80"/>
			<mx:DataGridColumn headerText="Counter " dataField="counter" width="80"/>
			<mx:DataGridColumn headerText="Status" dataField="status" width="110"/>
			<mx:DataGridColumn headerText="Checkin" dataField="checkin" itemRenderer="CheckBoxIR" visible="false"/>
		</mx:columns>
	</mx:DataGrid>

Open in new window

0
 
druplashAuthor Commented:
Thank you for the answer, but i get it not working. Please can somebody give me a full example with my code?

Thanks!
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
					   xmlns:s="library://ns.adobe.com/flex/spark" 
					   xmlns:mx="library://ns.adobe.com/flex/mx"
					   creationComplete="gaeste_creationCompleteHandler(event)">
	
	<fx:Script>
		<![CDATA[
			import flash.utils.getQualifiedClassName;
			import mx.collections.ArrayCollection;
			import mx.controls.Alert;
			import mx.events.DataGridEvent;
			import mx.events.FlexEvent;
			import mx.events.ListEvent;
			import mx.events.ValidationResultEvent;
			import mx.rpc.events.ResultEvent;
			import mx.utils.ObjectUtil;
			
			private var currentId:int;
			// wird verwendet um die datagrid daten bei einer suche zwischenzuspeichern und nach löschen der suche wieder zuzuweisen
			private var data:Object;

			protected function gaeste_creationCompleteHandler(event:FlexEvent):void
			{
				// event listener für die checkbox im itemrenderer
				gaesteDg.addEventListener( CheckBoxIR.UPDATE_ITEM, onUpdateItem);
				
				// initiales laden der gäste
				gaesteServices.getGaeste()
				
			}
			
			private function onUpdateItem(event:ListEvent):void
			{
				gaesteServices.updateGaeste(
					gaesteDg.selectedItem.vorname, 
					gaesteDg.selectedItem.nachname, 
					gaesteDg.selectedItem.title, 
					gaesteDg.selectedItem.telefon, 
					gaesteDg.selectedItem.kartennummer, 
					gaesteDg.selectedItem.email, 
					gaesteDg.selectedItem.strasse,
					gaesteDg.selectedItem.ort,
					gaesteDg.selectedItem.kommentar,
					gaesteDg.selectedItem.plz,
					gaesteDg.selectedItem.firma,
					gaesteDg.selectedItem.anrede,
					gaesteDg.selectedItem.tischnummer,
					gaesteDg.selectedItem.kategorie,
					gaesteDg.selectedItem.status,
					gaesteDg.selectedItem.checkin,
					gaesteDg.selectedItem.counter,
					gaesteDg.selectedItem.id)
			}
			
			private function updateGaesteListener(event:Object):void
			{
				//Alert.show("Gast wurde eingecheckt!");		
				// datagrid aktualisieren
				gaesteServices.getGaeste()
				tiKartennummer.text = ""; 
				labelTischnummer.text = "";
				sucheBtn.visible = true;
				clearSearchBtn.visible = false;
				hlTischnummer.visible = false;
				
							
			}
			
			private function getGaesteResult(e:ResultEvent):void
			{
				gaesteDg.dataProvider = e.result;
			}
			
			protected function searchTableno(event:MouseEvent):void
			{
				var data:Object = gaesteDg.dataProvider;
				var newData:ArrayCollection = new ArrayCollection();
				for(var i:int = 0; i < data.length; i++){
					if(data[i].kartennummer == tiKartennummer.text){
						labelTischnummer.text = data[i].tischnummer;
						newData.addItem(data[i]);
					}
				}
				gaesteDg.dataProvider = newData;
				clearSearchBtn.visible = true;
				sucheBtn.visible = false;
				hlTischnummer.visible = true;
			}
			
			protected function clearSearchClick(event:MouseEvent):void
			{
				data = gaesteDg.dataProvider;
				tiKartennummer.text = ""; 
				labelTischnummer.text = "";
				clearSearchBtn.visible = false;
				sucheBtn.visible = true;
				gaesteServices.getGaeste()	
				hlTischnummer.visible = false;
			}

		]]>
	</fx:Script>
	
	<fx:Declarations>
	</fx:Declarations>
	
	<s:Panel horizontalCenter="0" verticalCenter="0" width="960" height="500">
		
		<mx:DataGrid id="gaesteDg" y="30" left="30" right="30" height="300" variableRowHeight="true">
			<mx:columns>
				<mx:DataGridColumn headerText="Firma" dataField="firma" width="200"/>
				<mx:DataGridColumn dataField="nachname" headerText="Nachname" width="150"/>
				<mx:DataGridColumn dataField="vorname" headerText="Vorname" width="100"/>
				<mx:DataGridColumn headerText="Tisch Nr." dataField="tischnummer"/>
				<mx:DataGridColumn headerText="Karten Nr." dataField="kartennummer"/>
				<mx:DataGridColumn headerText="Counter " dataField="counter"/>
				<mx:DataGridColumn dataField="kommentar" headerText="Kommentar" wordWrap="true"  width="150"/>
				<mx:DataGridColumn headerText="Checkin" dataField="checkin" itemRenderer="CheckBoxIR"/>
			</mx:columns>
		</mx:DataGrid>

	<mx:Form top="390" right="412" id="searchTischnummerForm" paddingBottom="0" paddingLeft="0" paddingRight="0" paddingTop="0" left="30">
		<s:HGroup verticalAlign="middle" width="235">
			<s:Label text="Kartennummer eingeben" verticalAlign="middle" fontWeight="bold"/>
			<s:TextInput id="tiKartennummer" change="labelTischnummer.text = ''" click="tiKartennummer.text = ''; labelTischnummer.text = ''" fontSize="18" width="81" textAlign="center"/>
			<s:Button label="Suchen" click="searchTableno(event)" id="sucheBtn" visible="true"/>
			<s:Button label="Neue Suche" click="clearSearchClick(event)" id="clearSearchBtn" visible="false" />
		</s:HGroup>
		
	</mx:Form>
		<s:HGroup verticalAlign="middle" right="30" top="380">
			<s:Label text="Tischnummer" verticalAlign="middle" fontWeight="bold" id="hlTischnummer" visible="false"/>
			<s:Label id="labelTischnummer"  fontSize="50" textAlign="right"/>
			
		</s:HGroup>

	
	</s:Panel>
</s:WindowedApplication>

Open in new window

0
 
andreMarianoCommented:
well, there are 2 ways of ordering your grid...
the first is setting the order in your dataProvider... but you will be able ONLY if it's an arrayCollection

the second one is dispatching a event in the dataProvider simulating a click but it's not the best way of doing this... (only if you are desperated)
so... I'll show you how to do the first method and you wanna
well in the dataProvider we have the property "sort", this property sorts the dataProvider...
so... you have to set there you sort field:

arColl.sort = new Sort();
arColl.sort.fields = [new SortField("NAME_OF_THE_FIELD",false,true)];
so try it out
what you can do is aways transform whatever you get in an arrayCollection;
let's do an example with your code:

private function getGaesteResult(e:ResultEvent):void
			{
				var data:* = e.result;
				var result:ArrayCollection = new ArrayCollection();
				for(var i:int = 0; i < data.length; i++){
					result.addItem(data[i]);
				}
				
				result.sort = new Sort();
				result.sort.fields = [new SortField("counter",false,true)];
				gaesteDg.dataProvider = e.result;
			}
			
			protected function searchTableno(event:MouseEvent):void
			{
				var data:Object = gaesteDg.dataProvider;
				var newData:ArrayCollection = new ArrayCollection();
				for(var i:int = 0; i < data.length; i++){
					if(data[i].kartennummer == tiKartennummer.text){
						labelTischnummer.text = data[i].tischnummer;
						newData.addItem(data[i]);
					}
				}
				newData.sort = new Sort();
				newData.sort.fields = [new SortField("counter",false,true)];
				gaesteDg.dataProvider = newData;
				clearSearchBtn.visible = true;
				sucheBtn.visible = false;
				hlTischnummer.visible = true;
			}

Open in new window

0
 
druplashAuthor Commented:
Thank you for your great example!
But i get an 1180 error. SOrry the error is in german, i will try to translate to english.

1180: calling of an undefined method SortField

private function getGaesteResult(e:ResultEvent):void
                  {
                        var data:* = e.result;
                        var result:ArrayCollection = new ArrayCollection();
                        for(var i:int = 0; i < data.length; i++){
                              result.addItem(data[i]);
                        }
                        
                        result.sort = new Sort();
                        result.sort.fields = [new SortField("kartennummer",false,true)];
                        gaesteDg.dataProvider = e.result;
}
0
 
andreMarianoCommented:
sorry... my mistake... this is the correct way
private function getGaesteResult(e:ResultEvent):void
                  {
                        var data:* = e.result;
                        var result:ArrayCollection = new ArrayCollection();
                        for(var i:int = 0; i < data.length; i++){
                              result.addItem(data[i]);
                        }
                       
                        result.sort = new Sort();
                        result.sort.fields = [new SortField("kartennummer",false,true)];
                        gaesteDg.dataProvider = result;
}

anyway... this mean you sortfield does not exist... can you show me what`s coming in your e.result and then your var result ( the arrayCollection)?
0
 
druplashAuthor Commented:
The error is still showing.
I hope that i have you understood correctly. With the e.result i count the number of entries in the column "kartennummer".

var kartenCount:int
                        for(var i:int = 0; i < e.result.length; i++){
                                if(e.result[i].kartennummer != ""){
                                    kartenCount++;
                              }
                        }
0
 
druplashAuthor Commented:
Thank you very much andreMariano. This works perfect.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.