Solved

Sort a datagrid?

Posted on 2010-08-22
15
377 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
Comment Utility
0
 

Author Comment

by:druplash
Comment Utility
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
Comment Utility
Please can you show me, how the caller function has to look like?

Thanks
0
 
LVL 39

Expert Comment

by:blue-genie
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 1

Expert Comment

by:jth_92
Comment Utility
0
 

Author Comment

by:druplash
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you very much andreMariano. This works perfect.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now