Solved

Sort a datagrid?

Posted on 2010-08-22
15
387 Views
Last Modified: 2013-11-11
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
0
Comment
Question by:druplash
  • 8
  • 3
  • 3
  • +1
15 Comments
 
LVL 39

Expert Comment

by:blue-genie
ID: 33498764
0
 

Author Comment

by:druplash
ID: 33498780
Yes it is, but please can you give me an example with the column name "lastname". I´m not a developer.

Thank you!
0
 

Author Comment

by:druplash
ID: 33499920
Please can you show me, how the caller function has to look like?

Thanks
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 39

Expert Comment

by:blue-genie
ID: 33500258
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
 

Author Comment

by:druplash
ID: 33500337
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
 
LVL 39

Expert Comment

by:blue-genie
ID: 33500430
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
 

Author Comment

by:druplash
ID: 33500448
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
 
LVL 1

Expert Comment

by:jth_92
ID: 33505896
0
 

Author Comment

by:druplash
ID: 33508335
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
 
LVL 4

Expert Comment

by:andreMariano
ID: 33521363
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
 

Author Comment

by:druplash
ID: 33521510
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
 
LVL 4

Expert Comment

by:andreMariano
ID: 33521956
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
 

Author Comment

by:druplash
ID: 33522013
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
 
LVL 4

Accepted Solution

by:
andreMariano earned 500 total points
ID: 33522132
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
 

Author Closing Comment

by:druplash
ID: 33522283
Thank you very much andreMariano. This works perfect.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a very simple example to help those of you who are still migrating from AS2 to AS3 understand the redesigned event model in AS3. In AS2.0, event functions (that is, the function to be performed when an event is fired) were stored as a pro…
In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two. Here are s…
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question